16

I am using WiFi MAC address as Unique id, from Marshmallow onwards returning fake MAC address(for security reason). With this my Android application behaves differently. How to get the actual MAC address of the Android device.

I am using the following code-snippet.

WifiManager wmgr = (WifiManager)getSystemService(Context.WIFI_SERVICE); String wifiId = wmgr.getConnectionInfo().getMacAddress(); 

Following permissions are added in Manifest file.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
8
  • Is the app connected with wifi? Commented Oct 13, 2015 at 13:27
  • 1
    Marshmallow has disabled the MAC address access though you can look here stackoverflow.com/questions/31329733/… may be helpful to try alternate way to get MAC address from network cat file and refer here for details of new permission changes for bluetooth and wifi MAC arstechnica.com/gadgets/2015/10/… Commented Oct 13, 2015 at 13:35
  • 2
    cat /sys/class/net/[IFACE]/address - where [IFACE] can be wlan0, wlan1, etc... However, it may need to be noted that on some devices this is read-protected, so you may need root. Commented Oct 13, 2015 at 13:40
  • Hi @Android WeblineIndia, App connected to WiFi. it is returning fake address 02:00:00:00:00:00. I tried the given link; getHardwareAddress from interface is not working & it is difficulty to get actual MAC address it is based on the 'interfaceName' (interface name are not constant across the devices). Any other options not-rooted device? Commented Oct 13, 2015 at 14:40
  • 1
    Please check this solution, it works for me stackoverflow.com/questions/31329733/… Commented Jan 28, 2016 at 12:07

2 Answers 2

60

There is a work-around to get the Mac address in Android 6.0.

First you need to add Internet user permission.

<uses-permission android:name="android.permission.INTERNET" /> 

Then you can find the mac over the NetworkInterfaces API.

public static String getMacAddr() { try { List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(String.format("%02X:",b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; } 

Source: http://robinhenniges.com/en/android6-get-mac-address-programmatically

Sign up to request clarification or add additional context in comments.

13 Comments

Perhaps easier to convert to hex like so? stackoverflow.com/questions/19493873/…
@BooberBunz I've checked it on android 5.0, it works. And it also works with wifi turned off.
I've seen interfaces being named differently on Linux depending on the driver being used for example; my WiFi adapter is ensp0. I don't know if this is the case with Android
I would also change the return ""; to continue to allow to check for any further NetworkInterfaces
@cesargastonec Based on the later updates in 7.0, this function is working promptly. The MAC address information is restricted to Device Owner, Profile Owner, and Carrier apps (which will only obtain addresses for configurations which they create). Other callers will receive a default "02:00:00:00:00:00" MAC address.
|
-2

I went to my Netgear router interface and looked for the tab listing connected devices which nicely produced the MAC ID.

If in doubt, turn off the device and view the list, then turn it on and view again. The new listing is your MAC ID. Edit the router listing to show that.

A bigger view, how could they really hide you MAC ID since it's one of primary identifiers for the world wide surveillance state?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.