需要指出的是:wifi状态和wifi AP状态是互斥的状态;也就是一旦发现WIFI AP打开,WIFI是不能被打开的。
获取Android设备的WIFI MAC地址,首先需要将设备中的WIFI个人热点(AP)关闭;WIFI状态和WIFI AP状态是互斥的两种状态。也就是说:在WIFI AP打开的状态下,WIFI是不能被正常打开的。
android系统获取MAC地址的多种方式遍历。
方法一:使用NetworkInterface
方法二:
private static String getIpAndMacAddress() { String ip = ""; boolean isBreak = false; String name = ""; try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); name = intf.getName(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { ip = inetAddress.getHostAddress().toString(); isBreak = true; break; } } if (isBreak) { break; } } } catch (Exception e) { e.printStackTrace(); } String mac = ""; if (!TextUtils.isEmpty(name)) { try { byte[] address = NetworkInterface.getByName(name) .getHardwareAddress(); if (address != null) { mac = byte2hex(address, address.length); } } catch (SocketException e) { e.printStackTrace(); } } if (!TextUtils.isEmpty(mac) && !TextUtils.isEmpty(ip)) { return ip + "_" + mac; } return ""; } private static String byte2hex(byte[] b, int length) { StringBuffer hs = new StringBuffer(length); String stmp = ""; int len = length; for (int n = 0; n < len; n++) { stmp = Integer.toHexString(b[n] & 0xFF); if (stmp.length() == 1) hs = hs.append("0").append(stmp); else { hs = hs.append(stmp); } if (n != len - 1) { hs.append(":"); } } return String.valueOf(hs); }
疑问:
1. 上述两种方式哪种更加高效?
2. 两种方式是否都有局限?
3. 留个未来…