Linux 如何获取WiFi网络接口的MAC地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2601124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:55:52  来源:igfitidea点击:

How to obtain MAC address of WiFi network interface?

javaandroidlinuxmacosnetworking

提问by Gubatron

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

似乎 android 的 java.net.NetworkInterface 实现没有
byte[] getHardwareAddress() 方法 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

我发现有几个论坛的人试图这样做但没有明确的答案,我需要获得一个有点跨设备的 UUID,所以我不能依赖电话号码或 ANDROID_ID(可以被覆盖,我认为这取决于在拥有谷歌帐户的用户上) 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.

在 linux 中,您可以使用 ifconfig 或从 /proc/net/arp 读取,您可以轻松获取硬件地址。

Is there a file in android that I can read?

android中是否有我可以读取的文件?

There has to be a way to get this address since it's shown in the "Settings > About Phone > Status" of the phone.

必须有一种方法来获取此地址,因为它显示在手机的“设置 > 关于手机 > 状态”中。

采纳答案by CommonsWare

There has to be a way to get this address since it's shown in the "Settings > About Phone > Status" of the phone.

必须有一种方法来获取此地址,因为它显示在手机的“设置 > 关于手机 > 状态”中。

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.

这意味着,如果不出意外的话,您可以在 Android 开源代码周围进行推敲,也许使用 Google 代码搜索,以找出它从哪里提取。

Doing a bit of puttering myself, it would appear it is using getMacAddress()from WifiInfo.

我自己做了一些推杆,看起来它正在使用getMacAddress()from WifiInfo

回答by Tobiaswk

Late answer, but it can help others with the same "problem".

迟到的答案,但它可以帮助其他有同样“问题”的人。

The answeris really straight forward:

回答实在是直截了当:

WifiManager wifiMan = (WifiManager) this.getSystemService(
                Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();

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.

上面的代码会得到你设备的MAC地址,抓取地址时记得开启wifi。此代码段应在您的活动中使用。

回答by Kaps

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 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 地址的帖子,但未对其进行测试
如何获取 Wi-Fi Mac Android Marshmallow 中的地址

回答by Quantum4U

Add Following Permission.

添加以下权限。

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

WifiManager initialize in onCreate.

WifiManager 在 onCreate 中初始化。

 WifiManager wifiMgr = (WifiManager) getContext().getSystemService(context.WIFI_SERVICE);

Use following function.

使用以下功能。

 public void WI-FI_MAC() {
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    String macAddress = wifiInfo.getMacAddress();
    }

回答by fawkes

On Android Q, there is no way to access mac address anymore.

在 Android Q 上,无法再访问 mac 地址。

WifiInfo.getMacAddress()will always return 02:00:00:00:00:00.

WifiInfo.getMacAddress()总会回来的02:00:00:00:00:00

And WifiConfiguration.getRandomizedMacAddress()will not available anymore.

并且WifiConfiguration.getRandomizedMacAddress()将不再可用。