I want to be notified when a device switches networks, but for some reason, despite the documentation and various examples on SO, this particular action is never sent to my receiver.
Here is my code
RECEIVER:
BroadcastReceiver connectedToLocalWifiReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); Log.d(TAG, "ACTION='" + action +"'"); //<-- WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION never turns up here if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)){ //this block of code is never called } else { // ditto for this block } } } };
ACCOUNTING: In my code, right before I go to connect to the network, I register my receiver. I tried to register it with other actions, and they appear (e.g. ConnectivityManager.CONNECTIVITY_ACTION , for example), but for SUPPLICANT_CONNECTION_CHANGE_ACTION I get nothing.
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); registerReceiver(connectedToLocalWifiReceiver, intentFilter); wifiManager.enableNetwork(i.networkId, true); wifiManager.reconnect();
So, at the moment, Wi-Fi really disconnects from the current network and connects to the one I specify, but it does this without even transmitting anything.
I do not see anything clearly wrong, but I must miss something quite fundamental.
My manifest permissions are set this way, although I donβt see how, given that it actually switches to Wi-Fi correctly, permissions may have something to do with it:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
I would like to know what I'm missing.