Unable to retrieve MAC address on Android 11 and above using getHardwareAddress()

1.6k Views Asked by At

I have been using the following code snippet to retrieve the MAC address on Android devices running 9 and 10 successfully: But this function is not working on android 11 and above

public String getMacAddressAndroid6Plus() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (!intf.getName().equalsIgnoreCase("wlan0")) {
                continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null) {
                Log.e("MACLogs", "MAC address is null");
                return null;
            }
            StringBuilder buf = new StringBuilder();
            for (byte aMac : mac) {
                buf.append(String.format("%02X:", aMac));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            String macAddress = buf.toString();
            Log.d("MACLogs", "MAC address: " + macAddress);
            return macAddress;
        }
    } catch (Exception e) {
        Log.e("MACLogs", "Error getting MAC address: " + e.getMessage());
    }
    return null;
}

However, I have noticed that this code is no longer working on devices running Android 11 and above. Can someone please suggest a solution to retrieve the MAC address on these devices?

I expected the function to retrieve the unique MAC address of the device as it did on devices running Android 9 and 10.

1

There are 1 best solutions below

2
Chirag Thummar On

From Android 11 Google is not allowing to access hardware-based unique id for apps.

Android 10 (API level 29) adds restrictions for non-resettable identifiers, which include both IMEI and serial number. Your app must be a device or profile owner app, have special carrier permissions, or have the READ_PRIVILEGED_PHONE_STATE privileged permission in order to access these identifiers

So you can not access MAC address. rather think using alternative from below link.

you can visit this link

https://developer.android.com/training/articles/user-data-ids