SUPPLICANT_CONNECTION_CHANGE_ACTION not received

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.

+4
source share
1 answer

I ran into a similar problem and was able to solve it.

I am using NETWORK_STATE_CHANGED_ACTION instead of SUPPLICANT_CONNECTION_CHANGE_ACTION .

 registerReceiver(new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { Log.v(LOG_TAG, "onReceive"); if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { Log.d(LOG_TAG, "network state was changed"); NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (networkInfo.getState() == NetworkInfo.State.CONNECTED) { Log.d(LOG_TAG, "network connection has been established"); // the receiver is no longer needed, so unregist it immediately. unregisterReceiver(this); // do something... } } } }, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION)); 

Note. I do not set any additional permissions.

+5
source

Source: https://habr.com/ru/post/1271450/


All Articles