WifiInfo getSSID return last SSID when disconnected

I create an app which need to deal with WiFi.
I have this piece of code

this.wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
String currentSSID = wifiManager.getConnectionInfo().getSSID();

The problem is that the current SSID is valid when I’m already connected to hotspot but when i’m disconnected it return the SSID of the last hotspot instead of something like null or equivalent.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

After some experiment i figured out that the wifiInfo don’t update when the connection to hospot is loss so to fix it you can use

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isConnected = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
this.wifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
String currentSSID = wifiManager.getConnectionInfo().getSSID();
/*
Surround your ssid with " when you compare it with the ssid of the wifimanager
because it will return your SSID surouded by quotes
*/
if(currentSSID.equals(""" + your_net_ssid + """) && isConnected){
 //You are realy connected to the hospot
}else{
 //The connection dont exist
}

Hope this will be usefull !