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" />
kaki_hary
  • 191
  • 1
  • 1
  • 4
  • Is the app connected with wifi? – Tim Oct 13 '15 at 13:27
  • 1
    Marshmallow has disabled the MAC address access though you can look here http://stackoverflow.com/questions/31329733/how-to-get-the-missing-wifi-mac-address-on-android-m-preview 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 http://arstechnica.com/gadgets/2015/10/android-6-0-marshmallow-thoroughly-reviewed/5/ – Android Team Oct 13 '15 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. – showp1984 Oct 13 '15 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? – kaki_hary Oct 13 '15 at 14:40
  • 1
    Please check this solution, it works for me http://stackoverflow.com/questions/31329733/how-to-get-the-missing-wifi-mac-address-on-android-m-preview – Gorio Jan 28 '16 at 12:07
  • Possible duplicate of [Getting MAC address in Android 6.0](http://stackoverflow.com/questions/33159224/getting-mac-address-in-android-6-0) – mhdjazmati Dec 14 '16 at 13:37
  • duplicate question , however this is the complete right answer http://stackoverflow.com/a/41142980/3818437 – mhdjazmati Dec 20 '16 at 15:45
  • I posted here working solution https://stackoverflow.com/a/47789324/5330408 – Android Developer Dec 13 '17 at 11:32

2 Answers2

59

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

Tim
  • 38,263
  • 17
  • 115
  • 131
Rob Anderson
  • 2,247
  • 3
  • 17
  • 31
  • Perhaps easier to convert to hex like so? http://stackoverflow.com/questions/19493873/java-convert-binary-string-to-hex-string – Dima Tisnek Mar 10 '16 at 08:36
  • does this work in all versions of android or only in version 6? thanks – Nerdy Bunz Jun 20 '16 at 11:48
  • Also, any idea if it still requires wifi to be turned on on some devices in order to get the value? Thanks. – Nerdy Bunz Jun 20 '16 at 11:55
  • 1
    @BooberBunz I've checked it on android 5.0, it works. And it also works with wifi turned off. – Paul Chernenko Jun 28 '16 at 10:28
  • Also converting bytes to hex works incorrectly. I have :05 in my mac address and it gives me :5. So, I use String.format("%02x", b) to convert. – Paul Chernenko Jun 28 '16 at 10:35
  • 1
    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 – peterchaula Oct 09 '16 at 11:35
  • 1
    I would also change the `return "";` to `continue` to allow to check for any further `NetworkInterface`s – Secret Squirrel Oct 13 '16 at 14:20
  • does anyone know a solution for appcelerator? – Bish25 Dec 09 '16 at 14:28
  • @raypixar You didn't have `wlan0`. Does that mean that it is not necessary for an Android device to have an interface with that name? – Shobhit Puri Jan 09 '17 at 23:49
  • but it always return static mac for all devices 02:00:00 and so on . – Sumit Kumar Feb 02 '17 at 10:17
  • The above solution is working in 6.0, but after update of 7.0 this function will return "02:00:00:00:00:00". – beginner Jan 31 '18 at 18:30
  • @beginner Strange, It works on my Android 7.0 device – cesargastonec Nov 14 '20 at 22:51
  • 1
    @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. – beginner Nov 16 '20 at 12:55
0

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?