Android 获取连接wifi的mac地址

记录一下,直接调用系统的方法,实际测试可以获取,
注意:需要申请wifi相关权限,同时还有定位权限,Android 10中必须具有 ACCESS_FINE_LOCATION 权限才能使用

public static String getConnectedWifiMacAddress(Context context) {
    String connectedWifiMacAddress = null;
    WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    List wifiList;

    if (wifiManager != null) {
        wifiList = wifiManager.getScanResults();
        WifiInfo info = wifiManager.getConnectionInfo();
        if (wifiList != null && info != null) {
            for (int i = 0; i < wifiList.size(); i++) {
                ScanResult result = wifiList.get(i);
                if (info.getBSSID().equals(result.BSSID)) {
                    connectedWifiMacAddress = result.BSSID;
                }
            }
        }
    }
    return connectedWifiMacAddress;
}

你可能感兴趣的:(Android 获取连接wifi的mac地址)