我正在开发一个获取设备MAC地址的应用程序,但是因为Android 6.0我的代码不起作用,给我一个不正确的值 .
这是我的代码......
public String ObtenMAC()
{
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
return(info.getMacAddress().toUpperCase());
}
它返回一个奇怪的代码:“02:00:00:00:00:00”,而不是真正的MAC地址 .
有人可以帮我解决吗?
9 回答
答案大多是正确的,但请注意,android 7中有一个变化 . 你需要使用
DevicePolicyManager 和方法 getWifiMacAddress . 官方文档有拼写错误,这意味着你不应该从那里复制/粘贴它 .
参考:https://developer.android.com/about/versions/nougat/android-7.0-changes.html
Get Device mac adress in Android Nougat and O programmatically
请参阅Android 6.0 Changes .
使用以下代码在Android 6.0中获取Mac地址
我没有得到上述工作的答案,但偶然发现了另一个答案 .
这是获取IPv6地址然后从中获取mac地址的完整而简单的方法 .
How to get Wi-Fi Mac address in Android Marshmallow
已经测试过,它可以工作 . 非常感谢Rob Anderson!
这是在Marshmallow上成功获得它的完整2路代码,只需复制过去就可以了!
清单:
Summary : this code will try to get the MAC address first by Interface and if its failed it get it by file system.
Note:for file System way, you need to enable WIFI to access the file.
thnx对Sam的回答https://stackoverflow.com/a/39288868/3818437
您可以从IPv6本地地址获取MAC地址 . 例如,IPv6地址“fe80 :: 1034:56ff:fe78:9abc”对应于MAC地址“12-34-56-78-9a-bc” . 请参阅下面的代码 . 获取WiFi IPv6地址只需要android.permission.INTERNET .
请参阅维基百科页面IPv6 address,特别是关于"local addresses" fe80 :: / 64的注释和关于"Modified EUI-64"的部分 .
我尝试使用2种方法获取mac地址,首先是通过接口,如果失败,我通过文件系统得到它,但你需要启用wifi来访问该文件 .
将此行添加到清单中 .
我建议你像这里一样在你的偏好中保留你的mac地址
首先,您需要添加Internet用户权限 .
然后,您可以通过NetworkInterfaces API找到mac .
Its Perfectly Fine