Getting MAC address in Android 6.0 Getting MAC address in Android 6.0 android android

Getting MAC address in Android 6.0


Please refer to Android 6.0 Changes.

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.


Use below code to get Mac address in Android 6.0

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(Integer.toHexString(b & 0xFF) + ":");            }            if (res1.length() > 0) {                res1.deleteCharAt(res1.length() - 1);            }            return res1.toString();        }    } catch (Exception ex) {        //handle exception    }    return "";}


I didn't get the above answer to work, but stumbled upon another answer.

Here is a complete and simple method on getting the IPv6 address and then getting the mac address from it.

How to get Wi-Fi Mac address in Android Marshmallow

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";}

Tested it already and it works. Many thanks to Rob Anderson!