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

发布于 2024-08-27 19:04:08 字数 705 浏览 8 评论 0原文

看来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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨后咖啡店 2024-09-03 19:04:08

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

答案非常简单:

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

上面的代码将为您提供 MAC 地址您的设备,请记住在获取地址时启用 wifi。此代码片段应该在您的活动中使用。

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

The answer is 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.

说谎友 2024-09-03 19:04:08

一定有办法得到这个
地址,因为它显示在
“设置 > 关于手机 > 状态”
电话。

这意味着,如果没有别的办法,你可以在 Android 开源代码中闲逛,或许可以使用 Google 代码搜索,找出它从哪里获取这些代码。

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

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.

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

更新:

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

丑丑阿 2024-09-03 19:04:08

在 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 return 02:00:00:00:00:00.

And WifiConfiguration.getRandomizedMacAddress() will not available anymore.

天生の放荡 2024-09-03 19:04:08

这是我的代码,在 android 5 + 中运行良好。

public static String getMacAddress() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }
            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                // res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:",b));
            }
            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
        //handle exception
    }
    return "";
}

this my code and work well in android 5 +.

public static String getMacAddress() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }
            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                // res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:",b));
            }
            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
        //handle exception
    }
    return "";
}
小糖芽 2024-09-03 19:04:08

添加以下权限。

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

WifiManager 在 onCreate 中初始化。

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

使用以下功能。

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

Add Following Permission.

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

WifiManager initialize in 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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文