16
votes

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" />
2
Is the app connected with wifi?Tim
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/…Android Team
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
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
Please check this solution, it works for me stackoverflow.com/questions/31329733/…Gorio

2 Answers

60
votes

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

-1
votes

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?