6

I have an Android application that works on a single phone. In order to work on a single device, I need to get the MAC address or Android ID. I decided to get the MAC address because the app needs to connect on a specified WiFi network.

How to get the MAC address from an Android device?

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();

Currently I'm using this but it is not stable.

When the app restarts, it returns 02:00:00:00:00:00. and crashes.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
MicKSmith
  • 71
  • 1
  • 1
  • 2
  • are you using `` in AndroidManifest.xml –  Dec 17 '19 at 11:41
  • 1
    As mentioned [here](https://developer.android.com/training/articles/user-data-ids#mac-addresses) **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.** So you can get mac address of router for which you need permission as mentioned in same link. Hence this will not serve purpose of identifying device uniquely. You should read the entire article in link. – Shadow Droid Dec 17 '19 at 11:44
  • Please [edit] your question to provide a [mcve] that demonstrates the issue, as well as the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Dec 17 '19 at 21:46
  • 1
    Does this answer your question? [How to find MAC address of an Android device programmatically](https://stackoverflow.com/questions/10831578/how-to-find-mac-address-of-an-android-device-programmatically) – Josh Correia Aug 26 '20 at 19:53

4 Answers4

7
  //getting mac address from mobile
private 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 "";
}

Use these permission in manifest

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

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

Get this method's return value to a String variable and use it.

String MAC = getMacAddr();

Hope I helped

AllwiN
  • 693
  • 2
  • 12
  • 26
1

Try this code

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress(); 
Log.e(TAG,"MAC Address : " + macAddress);

Also, add below permission in your manifest file

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

and output is

MAC Address : 84:11:9E:B7:1E:D0

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
1

Using WifiManger, you will get MAC address

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String macAddress = wifiInfo.getMacAddress(); 

Don't forget to add wifi permission

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Akshay Raiyani
  • 1,243
  • 9
  • 21
0

use this for Android ID

   private String deviceId() {
    return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}