今天踩了个坑,可能也是自己看文档不够认真,产生了一点点SSID,BSSID的认知误差,当时百度查看了一篇文档,在文档中提取了统称SSID之类的字眼,在脑海中生成了错误的信息既SSID内包含了BSSID,小记一下。
SSID:WIFI名称
BSSID:MAC物理地址
安卓如何得到这些。如下,其WiFi信息大致都在,WiFiinfo这个对象中。
/**
* 获取当前连接WIFI的SSID
*/
public static String getSSID(Context context) {
WifiManager wm = (WifiManager) context.getSystemService(WIFI_SERVICE);
if (wm != null) {
WifiInfo winfo = wm.getConnectionInfo();
if (winfo != null) {
String s = winfo.getSSID();
if (s.length() > 2 && s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
return s.substring(1, s.length() - 1);
}
}
}
return "";
}
public static WifiInfo getWifiInfo(Context context) {
WifiManager wm = (WifiManager) context.getSystemService(WIFI_SERVICE);
if (wm != null) {
WifiInfo winfo = wm.getConnectionInfo();
return winfo;
}
return null;
}
public static String getWifiBSSID(Context context) {
WifiManager wm = (WifiManager) context.getSystemService(WIFI_SERVICE);
if (wm != null) {
WifiInfo winfo = wm.getConnectionInfo();
return winfo.getBSSID();
}
return null;
}
2019.3.26 补充
安卓9.0需要添加gps权限