0

I have problem with WiFi connection detection. My goal is to detect when user is switching between different WiFis. I found this, but it only detects when WiFi was established. In my case I need to know when one WiFi network changed to another on phone.

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
Serg Burlaka
  • 2,351
  • 24
  • 35

1 Answers1

1

You can use BroadcastReceiver

public class ConnectivityReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                if(netInfo.isConnected()) {
                    WifiManager wifiManager = (WifiManager) context.getAplicationContext().getSystemService (Context.WIFI_SERVICE);
                    WifiInfo info = wifiManager.getConnectionInfo ();
                    String ssid  = info.getSSID();
                    Log.d("Wifi Connected", "Wifi name is "+ info.getSSID());
                }
        }
    }
}
Serg Burlaka
  • 2,351
  • 24
  • 35
Eugen
  • 877
  • 6
  • 16