How to get Wi-Fi Mac address in Android Marshmallow
Asked Answered
U

2

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" />
Uninspired answered 13/10, 2015 at 13:20 Comment(8)
Is the app connected with wifi?Idaline
Marshmallow has disabled the MAC address access though you can look here #31330233 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/…Derrickderriey
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.Vallejo
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?Uninspired
Please check this solution, it works for me #31330233Squiggle
Possible duplicate of Getting MAC address in Android 6.0Wehrle
duplicate question , however this is the complete right answer https://mcmap.net/q/11128/-getting-mac-address-in-android-6-0Wehrle
I posted here working solution https://mcmap.net/q/11550/-programmatically-getting-the-mac-of-an-android-deviceMicroanalysis
B
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

Bonnell answered 6/3, 2016 at 17:54 Comment(13)
Perhaps easier to convert to hex like so? #19494373Heteroousian
does this work in all versions of android or only in version 6? thanksVolva
Also, any idea if it still requires wifi to be turned on on some devices in order to get the value? Thanks.Volva
@BooberBunz I've checked it on android 5.0, it works. And it also works with wifi turned off.Laughing
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.Laughing
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 AndroidSilicify
I would also change the return ""; to continue to allow to check for any further NetworkInterfacesEmory
does anyone know a solution for appcelerator?Aronoff
@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?Execratory
but it always return static mac for all devices 02:00:00 and so on .Secundines
The above solution is working in 6.0, but after update of 7.0 this function will return "02:00:00:00:00:00".Richelieu
@beginner Strange, It works on my Android 7.0 deviceYellowtail
@Yellowtail 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.Richelieu
S
-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?

Suannesuarez answered 14/12, 2016 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.