可以通过编程获取运行Android 6.0+的设备的MAC地址吗? [英] Can one programmatically obtain the MAC address of a device running Android 6.0+?

查看:80
本文介绍了可以通过编程获取运行Android 6.0+的设备的MAC地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以通过编程方式获取运行Android 6.0+的设备的MAC地址吗?

Can one programmatically obtain the MAC address of a device running Android 6.0+?

根据

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

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

这是否意味着无法在Android 6.0+中获取设备的MAC地址?如果可以的话,您能告诉我如何在Android Studio中进行操作吗?

Does that mean it's impossible to get the device's MAC address in Android 6.0+? If it's possible, can you tell me how to do it in Android Studio?

此外,此答案仅适用于Android版本低于6.0的设备

Also, this answer only applies to devices with versions of Android below 6.0

推荐答案

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

You can use an alternative way to get the MAC addr on a Android 6.0 device.

首先将Internet用户权限添加到您的AndroidManifest.xml:

First add Internet User-Permission to your 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();
    }

这篇关于可以通过编程获取运行Android 6.0+的设备的MAC地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆