kamal
kamal

Reputation: 96

Broadcast Receiver get notified when connecting to another wifi network

I'm trying to get notified when my phone connects to a new Wifi network.

This is my BroadcastReceiver's logic:

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            for (String key : extras.keySet()) {
                if(key.equals("networkInfo")){
                    NetworkInfo info = (NetworkInfo) extras.get(key);
                    if(info.getType() == ConnectivityManager.TYPE_WIFI){
                        if(info.getState().toString().equals("CONNECTED")){
                            //do stuff
                        }
                    }
                }
            }
        }
    }
}

This works fine when I turn on my Wifi and connect to a new network. But my problem is that when I'm already connected to a Wifi network and then connect to another one then nothing happens.

What is the right Actoin that I should add to my IntentFilter in order to achieve that?

EDIT

This code works fine. The problem was that the WifiReceiver was being unregistered.

Upvotes: 0

Views: 420

Answers (1)

Aritra Roy
Aritra Roy

Reputation: 15615

Please try this solution,

BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
context.registerReceiver(broadcastReceiver, intentFilter);

You need to check the MAC addresses of the routers to check if there is a change in the Wifi connection.

public class WifiBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION .equals(action)) {
            SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
            if (SupplicantState.isValidState(state) 
                    && state == SupplicantState.COMPLETED) {
                boolean changed = checkWifiChanged();
            }
        }
    }

    /** Detect if WiFi is changed. */
    private boolean checkWifiChanged() {
        boolean changed = false;

        // You can store the previous MAC address in Shared Preferences and fetch it here
        String previousMacAddress = getPreviousMacAddress();

        WifiManager wifiManager = 
            (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        WifiInfo wifi = wifiManager.getConnectionInfo();
        if (wifi != null) {
            // Get current router MAC address
            String bssid = wifi.getBSSID();
            changed = !previousMacAddress.equals(bssid);
        }

        return changed;
    }
}

You can store the previous MAC address in a database or Shared Preference and check the new connected MAC address with the previous one. If they are different, then the WiFi connection has changed otherwise not.

Upvotes: 2

Related Questions