android 6.0及以下获取wifi mac地址

Android 获取wifi mac地址的方法

Android 6.0以下获取mac地址的方法如下:

 WifiManager mWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (mWifi.isWifiEnabled()) {
                WifiInfo wifiInfo = mWifi.getConnectionInfo();
                macAddress = wifiInfo != null ? wifiInfo.getMacAddress() : "";
            }

但是在6.0及以上设置了权限,上面的方法获取的wifi mac 为默认值 “02:00:00:00:00:02”,可以用如下的方法获取

String macAddress = null;
            StringBuffer buf = new StringBuffer();
            NetworkInterface networkInterface = null;
            try {
                networkInterface = NetworkInterface.getByName("wlan0");
                if (networkInterface == null) {
                    return "02:00:00:00:00:02";
                }
                byte[] addr = networkInterface.getHardwareAddress();
                for (byte b : addr) {
                    buf.append(String.format("%02X:", b));
                }
                if (buf.length() > 0) {
                    buf.deleteCharAt(buf.length() - 1);
                }
                macAddress = buf.toString();
            } catch (SocketException e) {
                e.printStackTrace();
                return "02:00:00:00:00:02";
            }

你可能感兴趣的:(移动开发,Android,wifi,mac,地址,6.0)