I am trying to retrieve the list of devices connected to my router (Internet service provider) along with their MAC addresses. I was able to obtain the MAC address using the NetworkInterface class for Android versions 10 and older, but for Android 11 and newer versions, I'm getting a null value. I have tried various methods to retrieve the MAC address, but none of them have been successful.Interestingly, I downloaded an application called Fing from the Play Store, which is able to provide the list of devices along with their MAC addresses, even on Android 11 and newer versions. I'm curious to know how they are able to retrieve the MAC address in those cases.
The following code brings Mac Address for android 10 and older versions.
public String getDeviceMacAddress(String ipAddress) {
try {
InetAddress localIP = InetAddress.getByName(ipAddress);
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localIP);
if (networkInterface == null) {
return "";
}
byte[] hardwareAddress = networkInterface.getHardwareAddress();
if (hardwareAddress == null) {
return "";
}
StringBuilder stringBuilder = new StringBuilder(18);
for (byte b : hardwareAddress) {
if (stringBuilder.length() > 0) {
stringBuilder.append(":");
}
stringBuilder.append(String.format("%02x", b));
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
After Android 10 only System app can use MAC address
Don't work with MAC addresses MAC addresses are globally unique, not user-resettable, and survive factory resets. For these reasons, to protect user privacy, on Android versions 6 and higher, access to MAC addresses is restricted to system apps. Third-party apps can't access them.
After Android 10 update MAC address is not available for third party app check official link