I need to know how I can detect a switch in Wi-Fi networks, albeit automatically or manually, it doesn't matter. Is there some kind of intent being broadcasted throughout the system when a switch is detected? Or do I have to manually check if a new network is selected by calling a method on a ConnectivityManager?
Asked
Active
Viewed 7,495 times
1
-
2possible duplicate of [Detect 3G or Wifi Network restoration](http://stackoverflow.com/questions/4503561/detect-3g-or-wifi-network-restoration) – Reno Jun 03 '11 at 08:07
-
That question seems to answer how to detect if a network is connected or disconnected in general. I need to know when a switch between networks happened. – ThaMe90 Jun 03 '11 at 08:18
-
You can use the same code. If the type is Wifi, use NetworkInfo to get [EXTRA_](http://developer.android.com/reference/android/net/wifi/WifiManager.html) data. For example BSSID should be different for various WiFi n/ws – Reno Jun 03 '11 at 08:36
-
On a quick notice, that would involve caching the BSSID of the current network and comparing it with the new one if I am not mistaken? If so, I'd rather not, as I only need the update of a network switch to retrieve a new IP-Address... I don't need to know the BSSID of the network. – ThaMe90 Jun 03 '11 at 08:41
-
Yes, but that's the easiest way to tell – Geobits Jun 03 '11 at 08:51
-
Either way, if there is a switch in WiFi networks CONNECTIVITY_ACTION should be broadcasted, call [this](http://stackoverflow.com/questions/5307992/get-the-ipaddress-using-java) to get the ip-address – Reno Jun 03 '11 at 08:53
-
I already know how to get the IP-Address, but thanks for the info though :) – ThaMe90 Jun 03 '11 at 08:59
-
Let me know if it works or if you find a different solution you can post it here. – Reno Jun 03 '11 at 09:03
1 Answers
7
At this point in time, I have fixed this like this (haven't fully tested it yet as I don't have a second network available at the moment):
I extended the BroadcastReceiver class
private class NetworkSwitcher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
return;
}
NetworkInfo networkInfo =
(NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Log.d(TAG, "Network type: " + networkInfo.getTypeName() +
" Network subtype: " + networkInfo.getSubtypeName());
getOwnIpAddress();
mClient.updateUnicastSocket(mOwnAddress, mUnicastPort);
}
}
else {
Log.e(TAG, "Network connection lost");
}
}
}
I register this class as a receiver with a filter set to the ConnectivityManager.CONNECTIVITY_ACTION
intent (setting it in onResume()
and releasing it in onPause()
). This ought to catch any automatic Wi-Fi network switch. The getOwnIpAddress
retrieves the device's IP address from the WifiManager
.
I've also found that it works when I return to the activity from another activity.

Peter Mortensen
- 30,738
- 21
- 105
- 131

ThaMe90
- 4,196
- 5
- 39
- 64
-
1I realize this is the accepted (by the OP) but I don't think it will work. The docs recommend using WifiManager rather then ConnectivityManager for the sort of thing you describe, so you would use WifiManager.NETWORK_STATE_CHANGED_ACTION, but I'm not sure if even that would do the trick: notifying you when your client roams from one BSSID to the next (if I understand the question correctly). – Tom Feb 07 '13 at 04:02
-
That actually might have been a better solution. Though I'm not working on this application any more (maybe again in the future though). If I had known of the WifiManager, I would have tried that as well. – ThaMe90 Feb 07 '13 at 07:41
-
`WifiManager.NETWORK_STATE_CHANGED_ACTION` will do the trick exactly as @Tom said and mentioned in [Documentation](http://developer.android.com/reference/android/net/wifi/WifiManager.html#NETWORK_STATE_CHANGED_ACTION) – Sdghasemi Dec 10 '15 at 19:34