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.
From Android 11 Google is not allowing to access hardware-based unique id for apps.
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