標籤:net stat logs ringbuf 熱點 hardware getname sse nts
需要指出的是: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. 留個未來…
擷取Android裝置WIFI的MAC地址 “MAC地址”