Skip to content

Instantly share code, notes, and snippets.

@unclechen
Created April 6, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unclechen/47cf4cc2c8d64a34a331c538dc136d36 to your computer and use it in GitHub Desktop.
Save unclechen/47cf4cc2c8d64a34a331c538dc136d36 to your computer and use it in GitHub Desktop.
getWifiSsid
/**
* 获取当前连接到的WiFi名称
*
* 注意:当“APILevel >=17”时,返回的字符串会多一对双引号
* https://code.google.com/p/android/issues/detail?id=40144
* http://stackoverflow.com/questions/13563032/jelly-bean-issue-wifimanager-getconnectioninfo-getssid-extra
*/
public String getWifiSsid() {
String ssid = null;
try {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
ssid = wifiInfo.getSSID();
if (Build.VERSION.SDK_INT >= 17 && ssid != null && ssid.startsWith("\"")
&& ssid.endsWith("\"")) {
ssid = ssid.substring(1, ssid.length() - 1);
}
} catch (Exception e) {
}
return ssid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment