在安卓8.1版本中,用 WifiInfo.getSSID() ,可能出现 unknown ssid 或者什么也没读出来
解决方案 :用ConnectivityManager 这个类解决。
代码如下:
/**
* 获取SSID
* @param
* @return WIFI 的SSID
*/
public String getWIFISSID() {
String ssid="unknown id11";
//android 8.1
if (Build.VERSION.SDK_INT==Build.VERSION_CODES.O_MR1) {
assert connManager != null;
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo!=null){
if (networkInfo.isConnected()) {
if (networkInfo.getExtraInfo()!=null){
return networkInfo.getExtraInfo().replace("\"","");
}
}
}else{
return ssid;
}
} else{
assert mWifiManager != null;
WifiInfo info = mWifiManager.getConnectionInfo();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return info.getSSID();
} else {
return info.getSSID().replace("\"", "");
}
}
return ssid;
}