How to get mac Address from an Android Device with OS 11?

Viewed 7693
2 Answers

Android guidelines enforces to stop using mac addressess as identificators, as the link you posted there are other alternatives, if you want to use mac in android 11 is almost impossible to get the same mac, even with @hafiza code (you forgot to tell that there are some aditional manifest permissions) In my app i used mac verification until android 9, now I'm using this library https://github.com/fingerprintjs/fingerprint-android which is a really helpful solution to get some kind of id. I've tested on Android 9, 10 and 11 with no real problems or randomized values.

In my Android project I was using following Code to get Mac Address working on all Android OS

But now it is no more.. so now we cannot get Mac Address https://developer.android.com/training/articles/user-data-ids#mac-addresses

Old way that we was using to get 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", "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 "";
}
Related