如何获取WiFi网络接口的MAC地址?
看来android的java.net.NetworkInterface实现没有
byte[] getHardwareAddress() 方法 http://developer.android.com/reference/java/net/NetworkInterface.html
我发现有几个论坛的人试图这样做,但没有明确的答案,我需要获得一个跨设备的 UUID,所以我不能依赖电话号码或 ANDROID_ID(可以被覆盖和我认为这取决于拥有谷歌帐户的用户) http://developer.android.com/reference/android/provider /Settings.Secure.html#ANDROID_ID
在linux中,您可以使用ifconfig或从/proc/net/arp读取,您可以轻松获取硬件地址。
android 中有我可以读取的文件吗?
必须有一种方法来获取此地址,因为它显示在手机的“设置 > 关于手机 > 状态”中。
It seems the java.net.NetworkInterface implementation of android does not have a
byte[] getHardwareAddress() method
http://developer.android.com/reference/java/net/NetworkInterface.html
I've found several forums of people trying to do this with no definitive answer, I need to get a somewhat cross-device UUID, so I can't rely on phone numbers or in ANDROID_ID (which can be overwritten and which I think depends on the user having a google account)
http://developer.android.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
In linux you can use ifconfig or read from /proc/net/arp and you can easily get the Hardware address.
Is there a file in android that I can read?
There has to be a way to get this address since it's shown in the "Settings > About Phone > Status" of the phone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
迟到的回答,但它可以帮助其他有同样“问题”的人。
答案非常简单:
上面的代码将为您提供 MAC 地址您的设备,请记住在获取地址时启用 wifi。此代码片段应该在您的活动中使用。
Late answer, but it can help others with the same "problem".
The answer is really straight forward:
The above code will get you the MAC address of your device, remember to have wifi enabled when grabbing the address. This code snippet should be used in your Activity.
这意味着,如果没有别的办法,你可以在 Android 开源代码中闲逛,或许可以使用 Google 代码搜索,找出它从哪里获取这些代码。
我自己做了一些推敲,看起来它正在使用
WifiInfo
中的getMacAddress()
。Which means, if nothing else, you can go putter around the Android open source code, perhaps using Google Code Search, to figure out where it pulls that from.
Doing a bit of puttering myself, it would appear it is using
getMacAddress()
fromWifiInfo
.更新:
从 Android 6.0 开始,上述 API 将为您提供所有设备的恒定 MAC 地址,即 02:00:00:00:00:00。有关详细信息,请参阅下面
http://developer.android。 com/about/versions/marshmallow/android-6.0-changes.html
发现另一篇文章声称可以在 6.0 中找到 MAC 地址,但未进行测试
如何在 Android Marshmallow 中获取 Wi-Fi Mac 地址
UPDATE:
Beginning Android 6.0, above API will give you constant MAC address for all the devices, which is 02:00:00:00:00:00. Refer below for details
http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
Found another post that claims to find MAC address in 6.0, not tested it though
How to get Wi-Fi Mac address in Android Marshmallow
在 Android Q 上,无法再访问 mac 地址。
WifiInfo.getMacAddress()
将始终返回02:00:00:00:00:00
。并且
WifiConfiguration.getRandomizedMacAddress()
将不再可用。On Android Q, there is no way to access mac address anymore.
WifiInfo.getMacAddress()
will always return02:00:00:00:00:00
.And
WifiConfiguration.getRandomizedMacAddress()
will not available anymore.这是我的代码,在 android 5 + 中运行良好。
this my code and work well in android 5 +.
添加以下权限。
WifiManager 在 onCreate 中初始化。
使用以下功能。
Add Following Permission.
WifiManager initialize in onCreate.
Use following function.