【问题标题】:Can one programmatically obtain the MAC address of a device running Android 6.0+?能否以编程方式获取运行 Android 6.0+ 的设备的 MAC 地址?
【发布时间】:2016-07-11 07:48:17
【问题描述】:

能否以编程方式获取运行 Android 6.0+ 的设备的 MAC 地址?

根据this

为了给用户提供更好的数据保护,从这个开始 发布后,Android 删除了对设备本地的编程访问 使用 Wi-Fi 和蓝牙 API 的应用程序的硬件标识符。这 WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法 现在返回 02:00:00:00:00:00 的常量值。

这是否意味着在 Android 6.0+ 中无法获取设备的 MAC 地址?如果可能的话,你能告诉我如何在 Android Studio 中做到这一点吗?

另外,this answer 仅适用于 Android 版本低于 6.0 的设备

【问题讨论】:

  • 停止添加android studio标签。你的问题与android studio无关
  • @TimCastelijns 请停止编辑我的问题。我想使用 Android Studio。
  • 这很好,但这完全无关紧要。您的问题也适用于使用 eclipse 的人。你的问题是关于 IDE 的吗?不?不要使用标签
  • 嗨,戴克。我同意@TimCastelijns。您的问题与 Android API 有关,与 IDE 无关。
  • 我在这里发布了工作解决方案stackoverflow.com/a/47789324/5330408

标签: android mac-address


【解决方案1】:

您可以使用其他方法在 Android 6.0 设备上获取 MAC 地址。

首先将 Internet User-Permission 添加到您的 AndroidManifest.xml:

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

其次,

try {
        // get all the interfaces
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        //find network interface wlan0  
        for (NetworkInterface networkInterface : all) {
            if (!networkInterface.getName().equalsIgnoreCase("wlan0")) continue;
        //get the hardware address (MAC) of the interface    
            byte[] macBytes = networkInterface.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }


            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                //gets the last byte of b
                res1.append(Integer.toHexString(b & 0xFF) + ":");
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
           ex.printStackTrace();
    }

【讨论】:

  • 感谢您的回答!您能否评论一下您的代码逐步执行的操作?我是一个新程序员。 :)
  • 已编辑。希望这有帮助:)
  • 我们可以使用您的代码修改设备的 MAC 地址吗?我想我应该为此再问一个问题。
  • 您能否进一步解释一下您的代码? (+1) 另外,macBytesres1 之间的区别。
  • 我想要更多的解释。
猜你喜欢
  • 1970-01-01
  • 2011-05-07
  • 2013-01-27
  • 2012-07-27
  • 1970-01-01
  • 2012-06-05
  • 2020-05-06
  • 2023-03-27
  • 2016-01-14
相关资源
最近更新 更多