How to find MAC address of an Android device programmatically
Asked Answered
S

8

64

How do i get Mac Id of android device programmatically. I have done with IMIE Code and I know how to check Mac id on device manually but have no idea how to find out programmatically.

Squid answered 31/5, 2012 at 10:12 Comment(2)
I posted here working solution https://mcmap.net/q/11550/-programmatically-getting-the-mac-of-an-android-deviceMorgue
Does this answer your question? Programmatically getting the MAC of an Android deviceBash
I
132
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress(); 

Also, add below permission in your manifest file

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

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.

Incapable answered 31/5, 2012 at 10:16 Comment(6)
You should also give the activity permission ACCESS_WIFI_STATE, otherwise it wont work.Spracklen
Word of caution: This will not work if WiFi is disabled in the SettingsBoiler
This used to work great, on my nexus 9 running marshmallow this returns 02:00:00:00:00:00 now.Xylem
@Xylem developer.android.com/about/versions/marshmallow/… says that this method WifiInfo.getMacAddress() will return constant value, so to get mac address in Android M you amy use this answer https://mcmap.net/q/11123/-how-to-get-the-missing-wifi-mac-address-in-android-marshmallow-and-later note that wifi must be onPrimitive
If you use this code as-is, you might get this error: The WIFI_SERVICE must be looked up on the Application context or memory will leak on devices < Android N. Try changing to .getApplicationContext() [WifiManagerLeak]. The following should work instead: WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);Hanes
What if I am using cellular data?Cockfight
Z
21

See this post where I have submitted Utils.java example to provide pure-java implementations and works without WifiManager. Some android devices may not have wifi available or are using ethernet wiring.

Utils.getMACAddress("wlan0");
Utils.getMACAddress("eth0");
Utils.getIPAddress(true); // IPv4
Utils.getIPAddress(false); // IPv6 
Zaneta answered 22/10, 2012 at 8:22 Comment(8)
Some devices may have one or more ethernet ports (wired or wireless). This is just a linux jargon naming communication ports. Usuallay Android devices have only one wireless wlan0 port available.Zaneta
how to see the name of device portToon
See source code in my post getMACAddress, it loops all network interfaces(device ports) you can print out names or do something else with them. #6065010Zaneta
Does this require some external libs?Schutzstaffel
@Hackjustu: This does not require any external libs, altought my original answer is using InetAddressUtils.isIPv4Address(sAddr) function, it was removed from Android SDK. Instead should use isIPv4=sAddr.indexOf(':')<0 trick.Zaneta
@Whome, when my phone is connected to 4G data, gives null for "wlan0", "eth0" using Utils.getMACAddress("networkInterface")? Is there different networkInterface for 4G or 3G mobile data connection?Peroration
@pcj Devices may use any interface name they want so I suggest you debug printout all the interfaces. See getMACAddress() how interfaces are looped. What happens if you call getMACAddress(null)?Zaneta
@Zaneta I get following the list of interface name-mac pairs, ip6tnl0-NA, tunl0-NA, wlan0-{VALID_MAC} I am not pasting the mac for security reasons, I have question irrespective of interface MAC is unique for all interfaces or is it unique per interface? Most of the times I am getting MAC using wlan0 interface but some times it returns empty string ( This has observed when 4G data is connected ). MacUtils.getMACAddress(null) also given empty string. Is there any way I will get MAC address always?Peroration
A
11

With this code you will be also able to get MacAddress in Android 6.0 also

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) + ":");
                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";
}

EDIT 1. This answer got a bug where a byte that in hex form got a single digit, will not appear with a "0" before it. The append to res1 has been changed to take care of it.

Aleut answered 6/5, 2017 at 13:22 Comment(0)
C
8

It's Working

    package com.keshav.fetchmacaddress;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Collections;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.e("keshav","getMacAddr -> " +getMacAddr());
        }
    
        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) + ":");
                        res1.append(String.format("%02X:",b)); 
                    }
    
                    if (res1.length() > 0) {
                        res1.deleteCharAt(res1.length() - 1);
                    }
                    return res1.toString();
                }
            } catch (Exception ex) {
                //handle exception
            }
            return "";
        }
    }

UPDATE 1

This answer got a bug where a byte that in hex form got a single digit, will not appear with a "0" before it. The append to res1 has been changed to take care of it.

 StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    // res1.append(Integer.toHexString(b & 0xFF) + ":");
                    res1.append(String.format("%02X:",b)); 
                }
Cuttlefish answered 15/6, 2017 at 12:5 Comment(0)
L
7

Recent update from Developer.Android.com

Don't work with MAC addresses MAC addresses are globally unique, not user-resettable, and survive factory resets. For these reasons, it's generally not recommended to use MAC address for any form of user identification. Devices running Android 10 (API level 29) and higher report randomized MAC addresses to all apps that aren't device owner apps.

Between Android 6.0 (API level 23) and Android 9 (API level 28), local device MAC addresses, such as Wi-Fi and Bluetooth, aren't available via third-party APIs. The WifiInfo.getMacAddress() method and the BluetoothAdapter.getDefaultAdapter().getAddress() method both return 02:00:00:00:00:00.

Additionally, between Android 6.0 and Android 9, you must hold the following permissions to access MAC addresses of nearby external devices available via Bluetooth and Wi-Fi scans:

Method/Property Permissions Required

ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION

Source: https://developer.android.com/training/articles/user-data-ids.html#version_specific_details_identifiers_in_m

Lophophore answered 17/3, 2020 at 18:31 Comment(0)
E
4

Here the Kotlin version of Arth Tilvas answer:

fun getMacAddr(): String {
    try {
        val all = Collections.list(NetworkInterface.getNetworkInterfaces())
        for (nif in all) {
            if (!nif.getName().equals("wlan0", ignoreCase=true)) continue

            val macBytes = nif.getHardwareAddress() ?: return ""

            val res1 = StringBuilder()
            for (b in macBytes) {
                //res1.append(Integer.toHexString(b & 0xFF) + ":");
                res1.append(String.format("%02X:", b))
            }

            if (res1.length > 0) {
                res1.deleteCharAt(res1.length - 1)
            }
            return res1.toString()
        }
    } catch (ex: Exception) {
    }

    return "02:00:00:00:00:00"
}
Enterprising answered 7/1, 2019 at 11:19 Comment(0)
R
3
private fun getMac(): String? =
        try {
            NetworkInterface.getNetworkInterfaces()
                    .toList()
                    .find { networkInterface -> networkInterface.name.equals("wlan0", ignoreCase = true) }
                    ?.hardwareAddress
                    ?.joinToString(separator = ":") { byte -> "%02X".format(byte) }
        } catch (ex: Exception) {
            ex.printStackTrace()
            null
        }
Rights answered 18/10, 2019 at 20:7 Comment(0)
H
-2

There is a simple way:

Android:

   String macAddress = 
android.provider.Settings.Secure.getString(this.getApplicationContext().getContentResolver(), "android_id");

Xamarin:

    Settings.Secure.GetString(this.ContentResolver, "android_id");
Hullo answered 1/5, 2019 at 12:23 Comment(1)
Hey I tried this, but it is giving different macAddress when the apk is built from different laptopAnxious

© 2022 - 2024 — McMap. All rights reserved.