How to get the MAC address on Android

(A test project can be found on github)

Since you can easily get confused with the many permissions provided by google, let me show you two ways to get the mac address on android. The first approach would be to use the WifiManager.getMacAdress().

However there are Disadvantages:

  • Device should have Wi-Fi (where not all devices have Wi-Fi)
  • If Wi-Fi present in device should be turned on otherwise does not report the MAC address
  • And the suspicious permission ACCESS_WIFI_STATE

Another approach is the use the NetworkInterface.getHardwareAddress()

Disadvantage: uses permissions too, although they are required for your app to access the internet anyway and more likely commonly known to the end-user.

Conclusion:

Carring about permissions can be a pain in the butt there are even other ways if you just want identifying app installations, too.

Facebook comments:

comments

2 Comments

  1. Hi, I made two small changes to your code. On line 21 I continue the loop instead of returning from the method if the mac address is null. Between line 26 and line 27 I break from the loop now that I have a valid mac address. These two changes should improve reliability when passing null to the method instead of passing an interface name. If there are multiple interfaces the code as written will always return the MAC address for the last one unless any interface has a null MAC address, in which it will always return an empty string even if there are other valid interfaces to choose from.

Leave a Reply