WifiInfo getSSID return 断开连接时的最后一个 SSID

WifiInfo getSSID return last SSID when disconnected

我创建了一个需要处理 WiFi 的应用程序。 我有这段代码

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

问题是当我已经连接到热点时当前的 SSID 是有效的但是当我断开它时 return 最后一个热点的 SSID 而不是像 nullequivalent.

经过一些实验后,我发现当与 hospot 的连接丢失时,wifiInfo 不会更新,因此要修复它,您可以使用

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
}

希望这会很有用!