Get the SSID of a disconnected Wi-Fi network in Android using BroadcastReceiver?

I have the following BroadcastRecevier:

public class WiFiConnectionEventsReceiver extends BroadcastReceiver { private static final String TAG = WiFiConnectionEventsReceiver.class.getSimpleName(); @Override public void onReceive(Context context, @NonNull Intent intent) { Log.v(TAG, "action: " + intent.getAction()); Log.v(TAG, "component: " + intent.getComponent()); Bundle extras = intent.getExtras(); if (extras != null) { for (String key : extras.keySet()) { Log.v(TAG, "key [" + key + "]: " + extras.get(key)); } } else { Log.v(TAG, "no extras"); } ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = conMan.getActiveNetworkInfo(); if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) { Log.d("NetworkInfo", "Have Wifi Connection"); Log.d("NetworkInfo", netInfo.getExtraInfo()); Log.d("NetworkInfo", netInfo.getTypeName()); } else { Log.d("NetworkInfo", "Don't have Wifi Connection"); Log.d("NetworkInfo", netInfo.getExtraInfo()); Log.d("NetworkInfo", netInfo.getTypeName()); } WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); Log.d("WifiInfo", wifiManager.getConnectionInfo().toString()); } } 

It receives network connection and disconnect events.

When it connects to the WiFi network, I can easily get the SSID of the network.

But I want to get the SSID of the WiFi network when the network is disconnected (without having to store previously connected networks, and then map them that way, etc.). Is it possible?

Logs when connecting WiFi:

 V/WiFiConnectionEventsReceiver: action: android.net.conn.CONNECTIVITY_CHANGE V/WiFiConnectionEventsReceiver: component: ComponentInfo{com.example.test/com.example.test.WiFiConnectionEventsReceiver} V/WiFiConnectionEventsReceiver: key [networkInfo]: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "SKY123", roaming: false, failover: false, isAvailable: true] V/WiFiConnectionEventsReceiver: key [networkType]: 1 V/WiFiConnectionEventsReceiver: key [inetCondition]: 100 V/WiFiConnectionEventsReceiver: key [extraInfo]: "SKY123" D/NetworkInfo: Have Wifi Connection D/NetworkInfo: "SKY123" D/NetworkInfo: WIFI D/WifiInfo: SSID: SKY123, BSSID: 10:40:03:ad:6x:c9, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -79, Link speed: 43Mbps, Frequency: 2412MHz, Net ID: 1, Metered hint: false, score: 60 

Logs when WiFi is disconnected (note the unknown SSID):

 V/WiFiConnectionEventsReceiver: action: android.net.conn.CONNECTIVITY_CHANGE V/WiFiConnectionEventsReceiver: component: ComponentInfo{com.example.test/com.example.test.WiFiConnectionEventsReceiver} V/WiFiConnectionEventsReceiver: key [networkInfo]: [type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: <unknown ssid>, roaming: false, failover: false, isAvailable: true] V/WiFiConnectionEventsReceiver: key [networkType]: 1 V/WiFiConnectionEventsReceiver: key [inetCondition]: 0 V/WiFiConnectionEventsReceiver: key [extraInfo]: <unknown ssid> V/WiFiConnectionEventsReceiver: key [noConnectivity]: true D/NetworkInfo: Don't have Wifi Connection D/NetworkInfo: id D/NetworkInfo: MOBILE D/WifiInfo: SSID: <unknown ssid>, BSSID: <none>, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -127, Link speed: -1Mbps, Frequency: -1MHz, Net ID: -1, Metered hint: false, score: 0 

The reason I need this information is because I want to track the user's Wi-Fi usage on each network - connection and disconnect times.

Without the SSID of the disconnected network, the only way I can do this is with something like:

 if (WiFi network ABC disconnected) if (if previous stored connection for WiFi network ABC has no disconnection time) set WiFI network ABC disconnection time to now 

However, the above approach seems flaky, for example, that if for some reason the Wi-Fi disconnect event is skipped, etc.

+7
android android wifi ssid
source share
3 answers

You can definitely do this using a different approach than what you tried.

  • You need to register a receiver that is listening to WifiManager.SCAN_RESULTS_AVAILABLE_ACTION .
  • Whenever you perform a Wi-Fi check, this receiver will be called with the test results, as part of these results, you will have any ssid of the router available (regardless of its connection status).
  • Then you need to match the disconnected Wi-Fi that you found in your code with the scan list.

Add these permissions to your manifest and don't forget that if you use marshmallows, request permission for the location .

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

This is MainActivty, the results are received in the broadcast receiver.

 public class MainActivity extends AppCompatActivity { private WifiManager mWifiManager; private List<ScanResult> mScanResults; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (mWifiManager.isWifiEnabled() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && mWifiManager.isScanAlwaysAvailable())) { registerReceiver(mReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equalsIgnoreCase(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { mScanResults = mWifiManager.getScanResults(); for (ScanResult result : mScanResults) { Log.i(getClass().getSimpleName(), "wifi Ssid : " + result.SSID); } } } }; @Override protected void onDestroy() { try { unregisterReceiver(mReceiver); } catch (Exception e) { e.printStackTrace(); } super.onDestroy(); } } 
+1
source share

It's impossible. When onReceive is onReceive , the connection is already lost, and current information indicates that there is no connection.

Also note that you can receive another onRecieve call immediately after receiving the disconnect information. From spec :

CONNECTIVITY_ACTION

[...]

If this is a connection that resulted from a disconnected network, then the logical add-on FAILOVER_CONNECTION is set to true.

To lose connectivity, if the connection manager tries to connect (or is already connected) to another network, NetworkInfo for the new network is also transferred as an additional one. This allows any broadcast receivers to know that they do not have to tell the user that no data traffic will be possible. Instead, the receiver should expect another broadcast in the near future, indicating either that the failure recovery attempt was successful (and therefore there is still a general possibility of connecting to the data), or that the failed failure attempt failed, which means that all connections were lost.

[...]

0
source share

Once a connection is lost, it is lost.

The only way you could do this is to register it at that moment or save it using something like general settings so that you can get the name of the last Wi-Fi connected.

Using the offer here does not guarantee that you see the last connected Wi-Fi, since there is no guarantee that Wi-Fi is still available, especially if the connection was lost.

0
source share

All Articles