Android6.0系统获getMacAddress()取Mac地址返回02:00:00:00:00:00解决办法
在做用户信息统计时,发现 安卓6.0系统用户返回的Mac地址都是02:00:00:00:00:00,被误判为作弊用户,实际上是谷歌在6.0及以后版本对获取wifi及蓝牙MacAddress 做的改动。
Most notably, Local WiFi and Bluetooth MAC addresses are no longer available. The getMacAddress() method of a WifiInfo object and the BluetoothAdapter.getDefaultAdapter().getAddress() method will both return 02:00:00:00:00:00 from now on.
也就是早先的:
WifiManager wifiManager=(WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo=wifiManager.getConnectionInfo(); String mac=wifiInfo.getMacAddress();
方法在安卓6.0系统上获取到的Mac 都是02:00:00:00:00:00。刚开始以为权限不够,但权限都给了之后还是获取到02:00信息,说明不是权限问题。查寻资料还可以通过获取设备网络设备信息的API—— NetworkInterface.getNetworkInterfaces() 去获取。
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iF = interfaces.nextElement(); byte[] addr = iF.getHardwareAddress(); if (addr == null || addr.length == 0) { continue; } StringBuilder buf = new StringBuilder(); for (byte b : addr) { buf.append(String.format("%02X:", b)); } if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); } String mac = buf.toString(); Log.d("---mac", "interfaceName="+iF.getName()+", mac="+mac); }
用 Genymotion 虚拟机运行的6.0系统,打印如下:
---mac: interfaceName=eth0, mac=08:00:27:75:9A:98
---mac: interfaceName=eth1, mac=08:00:27:DF:43:CA
---mac: interfaceName=ifb0, mac=BE:98:32:22:9A:51
---mac: interfaceName=ifb1, mac=7A:E0:26:3E:8D:D3
获取一个属性的Mac 地址如下:
</pre></p><pre name="code" class="java">public static String getMacAddress(Context context) { // 该方法在6.0 系统 不再开发,返回 02:00:00:00:00:00 if (isAccessWifiStateAuthorized(context)) { WifiManager wifiMgr = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); Log.i("----getMacAddress--0--", "" + wifiInfo.getMacAddress()); } else { } String macAddress = null; StringBuffer buf = new StringBuffer(); NetworkInterface networkInterface = null; try { networkInterface = NetworkInterface.getByName("eth1"); if (networkInterface == null) { 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"; } Log.i("----getMacAddress--1--", "" + buf.toString()); return mac<span style="font-family: Arial, Helvetica, sans-serif;">Address</span>; }
可以通过 运行各版本手机的方法 对比测试
networkInterface = NetworkInterface.getByName("wlan0");
来确定getByName 为哪个name 。
以上就是Android6.0系统获getMacAddress()取Mac地址返回02:00:00:00:00:00解决办法的全文介绍,希望对您学习mac开发和使用有所帮助.