如何在Android Marshmallow中获取缺少的Wifi MAC地址?
想要获得Android M上的Wifi MAC地址的Android开发人员可能遇到了以下问题:获取MAC地址的标准Android OS API返回假MAC地址(02:00:00:00:00:00)而不是真正的价值。如何在Android Marshmallow中获取缺少的Wifi MAC地址?
正常方式获得了WIFI MAC地址是如下:
final WifiManager wifiManager = (WifiManager) getApplication().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
final String wifiMACaddress = wifiManager.getConnectionInfo().getMacAddress();
解决!
的MAC地址仍然可以从路径抓起:
"/sys/class/net/" + networkInterfaceName + "/address";
简单地做一个文件的读取,或该文件的猫会显示了WIFI MAC地址。
网络接口名称通常是沿着“为wlan0”或“eth1的”
可能需要注意的是,在某些设备这是读保护的,所以你可能需要root权限。 – showp1984
在版本的Android M的MACADDRESS将是WiFi和蓝牙“不可读”的线。 你可以得到的WiFi MACADDRESS用(的Android M预览版2):
public static String getWifiMacAddress() {
try {
String interfaceName = "wlan0";
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (!intf.getName().equalsIgnoreCase(interfaceName)){
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac==null){
return "";
}
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) {
buf.append(String.format("%02X:", aMac));
}
if (buf.length()>0) {
buf.deleteCharAt(buf.length() - 1);
}
return buf.toString();
}
} catch (Exception ex) { } // for now eat exceptions
return "";
}
(得到了这个Post这个代码)
不知怎的,我heared从“读取文件/ SYS /班/网/ “+ networkInterfaceName +”/ address“;将不会工作,因为Android N将被发布,也可以有不同的制造商像三星等差异。
希望这个代码仍然可以在以后的Android版本。
编辑:另外在Android的6发布这个作品
你认为我们需要什么权限来检索'networkInterface'?以及这种方法与Android的官方解决方案有何不同?http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id? –
没有解决方案。要访问 - >附近外部设备的硬件标识符 Informatic0re
一起消失。看起来Mac地址是随机的,即使你可以抓住它! https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id –
您可以从IPv6本地地址获取MAC地址。例如,IPv6地址“fe80 :: 1034:56ff:fe78:9abc”对应于MAC地址“12-34-56-78-9a-bc”。请参阅下面的代码。获取WiFi IPv6地址只需要android.permission.INTERNET。
请参阅维基百科页面IPv6 Address,特别是关于“本地地址”fe80 ::/64的注释和关于“修改的EUI-64”的章节。
/**
* Gets an EUI-48 MAC address from an IPv6 link-local address.
* E.g., the IPv6 address "fe80::1034:56ff:fe78:9abc"
* corresponds to the MAC address "12-34-56-78-9a-bc".
* <p/>
* See the note about "local addresses" fe80::/64 and the section about "Modified EUI-64" in
* the Wikipedia article "IPv6 address" at https://en.wikipedia.org/wiki/IPv6_address
*
* @param ipv6 An Inet6Address object.
* @return The EUI-48 MAC address as a byte array, null on error.
*/
private static byte[] getMacAddressFromIpv6(final Inet6Address ipv6)
{
byte[] eui48mac = null;
if (ipv6 != null) {
/*
* Make sure that this is an fe80::/64 link-local address.
*/
final byte[] ipv6Bytes = ipv6.getAddress();
if ((ipv6Bytes != null) &&
(ipv6Bytes.length == 16) &&
(ipv6Bytes[0] == (byte) 0xfe) &&
(ipv6Bytes[1] == (byte) 0x80) &&
(ipv6Bytes[11] == (byte) 0xff) &&
(ipv6Bytes[12] == (byte) 0xfe)) {
/*
* Allocate a byte array for storing the EUI-48 MAC address, then fill it
* from the appropriate bytes of the IPv6 address. Invert the 7th bit
* of the first byte and discard the "ff:fe" portion of the modified
* EUI-64 MAC address.
*/
eui48mac = new byte[6];
eui48mac[0] = (byte) (ipv6Bytes[8]^0x2);
eui48mac[1] = ipv6Bytes[9];
eui48mac[2] = ipv6Bytes[10];
eui48mac[3] = ipv6Bytes[13];
eui48mac[4] = ipv6Bytes[14];
eui48mac[5] = ipv6Bytes[15];
}
}
return eui48mac;
}
堆栈溢出用于编程问题。你的问题是什么?如果您尝试提供某种常见问题解答条目,请[请按照网站上的说明](https://stackoverflow.com/help/self-answer)提出问题,然后提供您自己的问题答案。 – CommonsWare
看来,Mac地址是随机的,即使你可以抓住它! https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id –
[在Android 6.0中获取MAC地址]的可能重复(http://stackoverflow.com/questions-33159224/getting-mac-address-in-android-6-0) – mhdjazmati