如何在Android棉花糖及更高版本中获取丢失的Wifi MAC地址? [英] How to get the missing Wifi MAC Address in Android Marshmallow and later?

查看:94
本文介绍了如何在Android棉花糖及更高版本中获取丢失的Wifi MAC地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在Android M上获取Wifi MAC地址的Android开发人员可能遇到了一个问题,即获取MAC地址的标准Android OS API返回了伪造的MAC地址(02:00:00:00:00:00)的真实价值.

Android developers looking to get the Wifi MAC Address on Android M may have experienced an issue where the standard Android OS API to get the MAC Address returns a fake MAC Address (02:00:00:00:00:00) instead of the real value.

以下是获取Wifi MAC地址的常规方法:

The normal way to get the Wifi MAC address is below:

final WifiManager wifiManager = (WifiManager) getApplication().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

final String wifiMACaddress = wifiManager.getConnectionInfo().getMacAddress();

推荐答案

在Android M中,对于WiFi和蓝牙,MAC地址将不可读". 您可以使用(Android M Preview 2)获取WiFi MACAddress:

In Android M the MACAddress will be "unreadable" for WiFi and Bluetooth. You can get the WiFi MACAddress with (Android M Preview 2):

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 "";
}

(从此发布中获取此代码a>)

(got this code from this Post)

以某种方式,我听说从"/sys/class/net/" + networkInterfaceName +"/address"中读取文件;由于将发布Android N,因此将无法正常工作,而且三星等不同制造商之间也可​​能会有差异.

Somehow I heared that reading the File from "/sys/class/net/" + networkInterfaceName + "/address"; will not work since Android N will be released and also there can be differences between the different manufacturers like Samsung etc.

希望该代码在更高的Android版本中仍然可以使用.

Hopefully this code will still work in later Android versions.

同样在Android 6版本中,此方法有效

Also in Android 6 release this works

这篇关于如何在Android棉花糖及更高版本中获取丢失的Wifi MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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