Search code examples
androidandroid-wifi

Constantly check for wireless network change


I would like to constantly check whether the phone is connected to a specific wireless network. I thought of a service and the SSID of the network of course, but how?


Solution

  • You'll want

    <intent-filter>
    <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
    <action android:name="android.net.wifi.STATE_CHANGE"/>
    </intent-filter>
    

    In your receiver tag.

    Or if you want more control over it, before registering BroadcastReceiver set these up:

    final IntentFilter filters = new IntentFilter();
    filters.addAction("android.net.wifi.WIFI_STATE_CHANGED");
    filters.addAction("android.net.wifi.STATE_CHANGE");
    super.registerReceiver(yourReceiver, filters);
    

    WIFI_STATE_CHANGED

    Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.

    STATE_CHANGE

    Broadcast intent action indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, additional extras may provide the BSSID and WifiInfo of the access point. as a String

    Also, you'll need to specify the right permissions:

    <user-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <user-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    

    Whole Source Code: Download

    AndroidManifest.xml

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.temp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.temp.MyWiFiStateListener">
            <intent-filter >
                <action android:name="android.net.wifi.STATE_CHANGE"/>
            </intent-filter>
        </receiver>
    </application>
    

    MainActivity.java

    package com.example.temp;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    

    Broadcast Receiver:

    package com.example.temp;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.wifi.WifiInfo;
    import android.net.wifi.WifiManager;
    import android.util.Log;
    
    public class MyWiFiStateListener extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            Log.d("TEMP", action);
            if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if(info.getType() == ConnectivityManager.TYPE_WIFI){
                    WifiManager myWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
                    Log.d("TEMP","BSSID :: " +  wifiInfo.getBSSID() + "SSID :: " + wifiInfo.getSSID());
                }
            }
        }
    
    }