Created
April 6, 2016 07:19
-
-
Save unclechen/47cf4cc2c8d64a34a331c538dc136d36 to your computer and use it in GitHub Desktop.
getWifiSsid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取当前连接到的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