32

Pre Marshmallow my app would obtain it's device MAC address via BluetoothAdapter.getDefaultAdapter().getAddress().

Now with Marshmallow Android is returning 02:00:00:00:00:00.

I saw some link(sorry not sure where now) that said you need to add the additional permission

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

to be able to get it. However it isn't working for me.

Is there some additional permission needed to get the mac address?

I am not sure it is pertinent here but the manifest also includes

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

So is there a way to get the local bluetooth mac address?

9 Answers 9

42

zmarties is right but you can still get the mac address via reflection or Settings.Secure:

  String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
4
  • 1
    what about wifi address ? , can you handle for mac address ? Dec 27, 2015 at 9:11
  • 1
    This is giving different bdaddr(but a valid mac). Reflection method is giving correct bdaddr.
    – Rilwan
    Feb 3, 2017 at 10:01
  • 2
    @Rilwan using reflection for hidden/blacklisted API will crash your app with next OS update and it is never correct solution, just temporal workaround.
    – Ewoks
    Aug 1, 2018 at 9:11
  • 5
    android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address") return null in Android 10
    – Bolt UIX
    Aug 12, 2020 at 11:06
14

Access to the mac address has been deliberately removed:

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.

(from Android 6.0 Changes)

2
  • 2
    Yes. I had read that. But the API 23 BluetoothAdapter has the following: /** * Default MAC address reported to a client that does not have the * android.permission.LOCAL_MAC_ADDRESS permission. * * @hide */ public static final String DEFAULT_MAC_ADDRESS = "02:00:00:00:00:00"; So that implies there is meant to be some way to get it. (Perhaps not implemented yet? I cling to hope.)
    – Eric
    Oct 28, 2015 at 13:03
  • 3
    The LOCAL_MAC_ADDRESS permission can only be used by system apps and it's unlikely that Google is going to change that. Jan 31, 2016 at 16:43
6

You can access Mac address from the file "/sys/class/net/" + networkInterfaceName+ "/address" ,where networkInterfaceName can be wlan0 or eth1.But Its permission may be read-protected,so it may not work in some devices. I am also attaching the code part which i got from SO.

public static String getWifiMacAddress() {
        try {
            String interfaceName = "wlan0";
            List<NetworkInterface> interfaces = Collections
                    .list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {
                if (!intf.getName().equalsIgnoreCase(interfaceName)) {
                    continue;
                }

                byte[] mac = intf.getHardwareAddress();
                if (mac == null) {
                    return "";
                }

                StringBuilder buf = new StringBuilder();
                for (byte aMac : mac) {
                    buf.append(String.format("%02X:", aMac));
                }
                if (buf.length() > 0) {
                    buf.deleteCharAt(buf.length() - 1);
                }
                return buf.toString();
            }
        } catch (Exception exp) {

            exp.printStackTrace();
        } 
        return "";
    }
4
  • Thank you. It works on the Nexus tablets that I tried. Feb 4, 2016 at 14:57
  • 6
    Android N and newer will return a false MAC address using this method, as well. Feb 29, 2016 at 19:20
  • Both 2 devices I tested had two, even slightly, different WiFi and Bluetooth MAC addresses... Jun 27, 2019 at 12:29
  • 1
    I tried this method on android 10 and it worked, but as you mentioned this is local wifi mac address, is there anyway to get bluetooth mac address using some method like this? Aug 12, 2020 at 7:33
6

First the following permissions have to be added to Manifest;

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />

Then,

public static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS = "bluetooth_address";

String macAddress = Settings.Secure.getString(getContentResolver(), SECURE_SETTINGS_BLUETOOTH_ADDRESS);

After that the application has to be signed with OEM / System key. Tested and verified on Android 8.1.0.

6
  • In Android 10, the permission <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> is also needed.
    – Samantha
    Dec 5, 2019 at 7:36
  • @Kotdroid Did you sign the APK with OEM / System key ?
    – Samantha
    Jul 1, 2020 at 15:06
  • how to do it? Can you please let me know in detail. I have used this in debug build btw.
    – Kotdroid
    Jul 1, 2020 at 23:17
  • @Kotdroid The signing depends on the OEM. Please consult your System team if you are an OEM. Normal app developers do not have access to OEM keys. Thank you.
    – Samantha
    Jul 2, 2020 at 12:07
  • LOCAL_MAC_ADDRESS - it shows warning manifest like permission only grant by system app
    – Bolt UIX
    Aug 12, 2020 at 8:15
4

Please use the below code to get the bluetooth mac address. let me know if any issues.

private String getBluetoothMacAddress() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
        try {
            Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
            mServiceField.setAccessible(true);

            Object btManagerService = mServiceField.get(bluetoothAdapter);

            if (btManagerService != null) {
                bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
            }
        } catch (NoSuchFieldException e) {

        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {

        }
    } else {
        bluetoothMacAddress = bluetoothAdapter.getAddress();
    }
    return bluetoothMacAddress;
}
1
  • 4
    tip: reflection for using blacklisted API is never good solution and it might hit you in the face with next Android update
    – Ewoks
    Aug 1, 2018 at 9:07
3

Getting the MAC address via reflection can look like this:

private static String getBtAddressViaReflection() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
    if (bluetoothManagerService == null) {
        Log.w(TAG, "couldn't find bluetoothManagerService");
        return null;
    }
    Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
    if (address != null && address instanceof String) {
        Log.w(TAG, "using reflection to get the BT MAC address: " + address);
        return (String) address;
    } else {
        return null;
    }
}

using a reflection library (net.vidageek:mirror) but you'll get the idea.

6
  • Code does not work. With the lib it always return null on serveral devices with different API levels
    – glethien
    Feb 23, 2017 at 13:28
  • 1
    I just tried the code on a few devices and always got a mac address back (tested with targetSDK 22 on devices with 4.4, 5.0, 5.1 and 6.0)
    – p2pkit
    Mar 7, 2017 at 10:56
  • 6
    On the Pixel 2 version of Android 8.0 Oreo this method won't work any more. I'm getting java.lang.reflect.InvocationTargetException Caused by: java.lang.SecurityException: Need LOCAL_MAC_ADDRESS permission: Neither user 10141 nor current process has android.permission.LOCAL_MAC_ADDRESS.
    – Andreas
    Oct 19, 2017 at 23:36
  • 1
    Google seems to have rolled out the 'fix' to any device that's getting Android 8.1. A Nexus 5X updated to the preview of 8.1 also gets the exception. As a small consolation, Google has started to show the own Bluetooth MAC in the Bluetooth Settings UI, so at least users could manually copy it into an app that needs it.
    – Andreas
    Nov 2, 2017 at 15:50
  • I can confirm that manually entering and then using the MAC address works for my application to establish a pair-free connection. Mar 18, 2018 at 23:25
1

Since below method return null for android O.

String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");

I found new way to get Bluetooth Mac address, you can try by using below command line.

su strings /data/misc/bluedroid/bt_config.conf | grep Address

NOTE: In my case, i was working with root device so my app has super user permission.

0

As it turns out, I ended up not getting the MAC address from Android. The bluetooth device ended up providing the Android device MAC address, which was stored and then used when needed. Yeah it seems a little funky but on the project I was on, the bluetooth device software was also being developed and this turned out to be the best way to deal with the situation.

-1

Worked great

 private String getBluetoothMacAddress() {
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        String bluetoothMacAddress = "";
        try {
            Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
            mServiceField.setAccessible(true);

            Object btManagerService = mServiceField.get(bluetoothAdapter);

            if (btManagerService != null) {
                bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
            }
        } catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {

        }
        return bluetoothMacAddress;
    }
2
  • What is the package name of Field @Vinayak I found there are plenty of Field classes. Jun 18, 2019 at 5:38
  • @Iqbalhossain it's java.lang.reflect.Field.
    – rhbvkleef
    Oct 29, 2021 at 13:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.