Amit Vaghela
Amit Vaghela

Reputation: 22945

How can I get the MAC address from an Android device with Android 11?

We are using NetworkInterface.getHardwareAddress() to get the MAC address, but as we can see from MAC address availability changes in Android 11, now it will not be used for Android 11.

Is there a solution to this or any guideline?

Upvotes: 5

Views: 19203

Answers (4)

BeardOverflow
BeardOverflow

Reputation: 1070

Your application must be privileged. There is no other long-term answer. Some workarounds might work in Android 11/12 with permissive selinux context or engineering/user-debug build variants (custom roms), but not with enforcing selinux context or user build variant (official roms).

Android policies pull you to develop your app as privileged app, does not matter which is your user-case.

A privileged app can query the real mac address using NetworkInterface class with android.permission.LOCAL_MAC_ADDRESS protected permission like previous Android versions.

// String.format approach
NetworkInterface iface = NetworkInterface.getByName(name);
byte[] bmac = iface.getHardwareAddress();
String smac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", bmac[0], bmac[1], bmac[2], bmac[3], bmac[4], bmac[5]);

// android.net.MacAddress approach
NetworkInterface iface = NetworkInterface.getByName(name);
byte[] bmac = iface.getHardwareAddress();
MacAddress cmac = MacAddress.fromBytes(bmac);
String smac = cmac.toString();

To gain privileged status:

  • Place your app into /system/priv-app location + privapp-permission.xml manifest + LOCAL_MAC_ADDRESS protected permission
  • LOCAL_MAC_ADDRESS protected permission + platform signature

And forget it for regular apps.

See:

Upvotes: 0

javdromero
javdromero

Reputation: 1937

Android guidelines enforces to stop using MAC addresses as identification. As the link you posted, there are other alternatives. If you want to use a MAC addresses in Android 11 is almost impossible to get the same MAC address, even with hafiza's code (you forgot to tell that there are some additional manifest permissions).

In my app, I used MAC address verification until Android 9.0 (Pie), but now I'm using the library FingerprintJS Android which is a really helpful solution to get some kind of id. I've tested on Android 9.0, Android 10 and Android 11 without any real problems or randomized values.

Upvotes: 3

Atakanunsl
Atakanunsl

Reputation: 1

With this code, I am able to get the MAC address in Android 11.

fun getEthMac(): String {
    var macAddress = "Not able to read the MAC address"
    var br: BufferedReader? = null
    try {
        br = BufferedReader(FileReader("/sys/class/net/eth0/address"))
        macAddress = br.readLine().uppercase()
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
        if (br != null) {
            try {
                br.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
    return macAddress
}

It is important to note that this file location might be different for different devices and different Android versions. So it's important to test this function on different devices and different Android versions to make sure that the file is accessible and the MAC address can be read correctly.

Upvotes: 0

Hafiza
Hafiza

Reputation: 860

In my Android project I was using the following code to get the MAC address working on all versions of Android.

But now it is no more... so now we cannot get the MAC address. See Don't work with MAC addresses.

The old way that we were using to get the MAC address:

public static String getMacAddress() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface networkInterface : all) {
            if (!networkInterface.getName().equalsIgnoreCase("wlan0"))
                continue;

            byte[] macBytes = networkInterface.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            Log.e("Get MAC address", "getMacAddr: " + res1.toString());
            for (byte b : macBytes) {
                // res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:", b));
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString().replace(":", "-");
        }
    } catch (Exception ex) {
        Log.e("TAG", "getMacAddr: ", ex);
    }
    return "";
}

Upvotes: 1

Related Questions