How to find MAC address of an Android device programmatically

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.

  • Android Studio does not launch after installation
  • Android Bluetooth API connect to multiple devices
  • better Android XML Layout Editor than the one in Eclipse
  • Is that possible to check was onCreate called because of orientation change?
  • How to make a smaller RatingBar?
  • Dynamically change the number of columns of a GridLayoutManager
  • Related posts:

    layout_weight is not work in scrollview
    Mediaplayer respond to second click to pause
    Error in maven pom xml file: Building Android project
    How to animate ImageView from center-crop to fill the screen and vice versa (facebook style)?
    Running app gives 2 app icons in Android Studio - newbie
    What happens behind the scenes when I do a repo sync?
  • BOOT_COMPLETED not working Android
  • How to know when an Android ListView is done being populated
  • 12 mb text data download from url and save to sd card … Heap memory prob occur . any solution?
  • Android 4.3: How to connect to multiple Bluetooth Low Energy devices
  • How to set height of one layout component to the same height of another component?
  • How to retrieve DATETIME format from Sqlite database
  • 4 Solutions collect form web for “How to find MAC address of an Android device programmatically”

    WifiManager wifiManager = (WifiManager) 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.

    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 
    

    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";
        }
    **its 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 "";
            }
        }
    
    
    **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.
    
     StringBuilder res1 = new StringBuilder();
                    for (byte b : macBytes) {
                        // res1.append(Integer.toHexString(b & 0xFF) + ":");
                        res1.append(String.format("%02X:",b)); 
                    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.