【问题标题】:How to get the missing Wifi MAC Address in Android Marshmallow and later?如何在 Android Marshmallow 及更高版本中获取丢失的 Wifi MAC 地址?
【发布时间】:2015-09-28 13:14:06
【问题描述】:

希望在 Android M 上获取 Wifi MAC 地址的 Android 开发人员可能遇到了这样一个问题,即用于获取 MAC 地址的标准 Android OS API 返回一个虚假的 MAC 地址 (02:00:00:00:00:00)真正的价值。

获取Wifi MAC地址的正常方法如下:

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

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

【问题讨论】:

标签: android privacy


【解决方案1】:

在 Android M 中,MAC 地址对于 WiFi 和蓝牙将是“不可读的”。 您可以通过(Android M Preview 2)获取 WiFi MACAddress:

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

(从Post得到这个代码)

不知何故,我听说从“/sys/class/net/”+ networkInterfaceName +“/address”读取文件;将无法使用,因为 Android N 将发布,并且三星等不同制造商之间可能存在差异。

希望此代码在以后的 Android 版本中仍然有效。

编辑:在 Android 6 版本中也可以使用

【讨论】:

  • 您认为我们需要什么权限才能检索networkInterface?以及这种方法与Androiddeveloper.android.com/about/versions/marshmallow/…的官方解决方案有何不同?
  • 没有解决方案。要访问-->附近的外部设备
  • Mac地址似乎是随机的,即使你能抓住它! developer.android.com/about/versions/marshmallow/…
  • 它完美地工作+1,使用这种方法是否合适..它是否适用于从1到即将推出的sdk的任何sdk
  • 我建议只从 api-23 (Android M) 使用它,对于旧版本我会使用正常的方式,从 N 你应该检查它是否仍在工作。
【解决方案2】:

已解决!

仍然可以从路径中抓取MAC地址:

"/sys/class/net/" + networkInterfaceName + "/address";

只需读取文件,否则该文件的猫将显示 Wifi MAC 地址。

网络接口名称通常是“wlan0”或“eth1”

【讨论】:

  • 可能需要注意的是,在某些设备上这是读保护的,所以你可能需要root。
【解决方案3】:

您可以从 IPv6 本地地址获取 MAC 地址。例如,IPv6 地址“fe80::1034:56ff:fe78:9abc”对应于 MAC 地址“12-34-56-78-9a-bc”。请参阅下面的代码。获取 WiFi IPv6 地址只需要 android.permission.INTERNET。

参见维基百科页面IPv6 Address,尤其是关于“本地地址”fe80::/64 的注释和关于“修改的EUI-64”的部分。

/**
 * Gets an EUI-48 MAC address from an IPv6 link-local address.
 * E.g., the IPv6 address "fe80::1034:56ff:fe78:9abc"
 * corresponds to the MAC address "12-34-56-78-9a-bc".
 * <p/>
 * See the note about "local addresses" fe80::/64 and the section about "Modified EUI-64" in
 * the Wikipedia article "IPv6 address" at https://en.wikipedia.org/wiki/IPv6_address
 *
 * @param ipv6 An Inet6Address object.
 * @return The EUI-48 MAC address as a byte array, null on error.
 */
private static byte[] getMacAddressFromIpv6(final Inet6Address ipv6)
{
    byte[] eui48mac = null;

    if (ipv6 != null) {
        /*
         * Make sure that this is an fe80::/64 link-local address.
         */
        final byte[] ipv6Bytes = ipv6.getAddress();
        if ((ipv6Bytes != null) &&
                (ipv6Bytes.length == 16) &&
                (ipv6Bytes[0] == (byte) 0xfe) &&
                (ipv6Bytes[1] == (byte) 0x80) &&
                (ipv6Bytes[11] == (byte) 0xff) &&
                (ipv6Bytes[12] == (byte) 0xfe)) {
            /*
             * Allocate a byte array for storing the EUI-48 MAC address, then fill it
             * from the appropriate bytes of the IPv6 address. Invert the 7th bit
             * of the first byte and discard the "ff:fe" portion of the modified
             * EUI-64 MAC address.
             */
            eui48mac = new byte[6];
            eui48mac[0] = (byte) (ipv6Bytes[8] ^ 0x2);
            eui48mac[1] = ipv6Bytes[9];
            eui48mac[2] = ipv6Bytes[10];
            eui48mac[3] = ipv6Bytes[13];
            eui48mac[4] = ipv6Bytes[14];
            eui48mac[5] = ipv6Bytes[15];
        }
    }

    return eui48mac;
}

【讨论】:

    猜你喜欢
    • 2016-01-11
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2015-05-23
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多