简体   繁体   English

安卓6.0及以上Mac地址显示为空

[英]Mac address for android 6.0 and above shows null

I am using this code to get MAC ADDRESS and display it in my app.我正在使用此代码获取 MAC 地址并将其显示在我的应用程序中。 The code works fine for all devices except most latest devices and ANDROID BOX.该代码适用于除最新设备和 ANDROID BOX 之外的所有设备。

it shows null for ANDROID BOX and other latest device.对于 ANDROID BOX 和其他最新设备,它显示为 null。

Here is code:这是代码:

   public static String getWifiMacAddress() {
    try {
        String interfaceName = "wlan0";
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (!intf.getName().equalsIgnoreCase(interfaceName)){
                continue;
            }

            byte[] mac = intf.getHardwareAddress();
            if (mac==null){
                return "";
            }

            StringBuilder buf = new StringBuilder();
            for (byte aMac : mac) {
                buf.append(String.format("%02X:", aMac));
            }
            if (buf.length()>0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            return buf.toString();
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}

I have written these permissions in manifest file我已经在清单文件中写入了这些权限

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Firstly u will check the permission is granted or not?首先你会检查许可是否被授予?

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
 String macAddress = wInfo.getMacAddress();

Also, add below permission in your manifest file另外,在清单文件中添加以下权限

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

Please refer this link for 6.0 marshmalow 请参阅此链接以获取 6.0 棉花糖

First you will need to add Internet user permission in AndroidManifest.xml.首先,您需要在 AndroidManifest.xml 中添加 Internet 用户权限。

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

and then Refer this to get mac address : http://robinhenniges.com/en/android6-get-mac-address-programmatically然后参考这个获取mac地址http : //robinhenniges.com/en/android6-get-mac-address-programmatically

And if it doesn't works then refer this Android 6.0 changes from this i have concluded that,如果它不起作用,请参考此Android 6.0 更改,我得出的结论是,

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.为了向用户提供更好的数据保护,从本版本开始,Android 删除了使用 Wi-Fi 和蓝牙 API 的应用程序对设备本地硬件标识符的编程访问。 The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00. WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法现在返回一个常量值 02:00:00:00:00:00。

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.要通过蓝牙和 Wi-Fi 扫描访问附近外部设备的硬件标识符,您的应用现在必须具有 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 权限。 Note That : you can't get your own MACs even having those permissions.需要注意的你不能让自己的MAC甚至有这些权限。 Read carefully, It is said that you can get other devices MACs having those permissions, but not your own .仔细阅读,据说您可以获得具有这些权限的其他设备MAC,但不是您自己的

As in 6.0 and above adding permission in Manifest alone wont work.在 6.0 及更高版本中,仅在 Manifest 中添加权限是行不通的。 You should have runtime permission and grant it if not granted.您应该拥有运行时权限,如果未授予,则授予它。

Check this link:检查此链接:

https://stackoverflow.com/a/30549756/3910281 https://stackoverflow.com/a/30549756/3910281

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM