Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SUPPLICANT_CONNECTION_CHANGE_ACTION never received

I am looking to be notified when the device switches networks but for some oddball reason, in spite of the documentation and various examples on SO, this particular action never gets sent to my receiver.

Here's 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
                }
            }
        }
    };

REGISTERING: In my code right before I go to connect to the network I register my receiver. I have tried registering it with other actions and they do turn up (like ConnectivityManager.CONNECTIVITY_ACTION for instance) 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 this point the wifi does in fact disconnect from its current network and connect to the one I'm specifying, but it does so without ever broadcasting anything.

I can't see anything obviously wrong, but must be missing something pretty fundamental.

My manifest permissions are set thusly, though I don't see how, given that it actually does the wifi switching correctly, permissions could have anything to do with it anymore:

<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"/>

Would love to find out what I'm overlooking.

like image 586
Yevgeny Simkin Avatar asked Jan 15 '16 01:01

Yevgeny Simkin


1 Answers

I stuck with a similar problem and managed to solve it.

I'm 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 don't set any additional permissions.

like image 161
hata Avatar answered Nov 15 '22 07:11

hata