首先要在AndroidManifest.xml加上权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
安卓6.0模拟器上,有效的代码:
private String getWifiSSID() {
String ssid = "";
try {
ConnectivityManager connManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
assert connManager != null;
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
if (networkInfo.isConnected()) {
if (networkInfo.getExtraInfo() != null) {
ssid = networkInfo.getExtraInfo().replace("\"", "");
}
}
}
catch (Exception e) {}
finally {
return ssid;
}
}