8

I need to obtain device's Bluetooth MAC address.
Before Android 6 it was easy as BluetoothAdapter.getDefaultAdapter().getAddress(). After that we had to use a simple workaround: String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");. But later(in Android 8 AFAIK) it was also closed, but another workaround was discovered:

    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();
    }

But starting from Android 8.1 trying to access that method throws exception:

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, which means that this method requires permission, available only for system-level apps.

So the question is if there is any workaround to get Bluetooth address in Android 8.1?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • 1
    Did you get any updates on this? I am also stuck on this. Not able to fetch bluetooth MAC address. – Deepak kaku Oct 03 '18 at 23:15
  • 1
    @Deepakkaku I am using very ugly workaround by asking user to enter his MAC address manually. It can be found in settings. You can open settings screen on some step, but not on the required page, so you also have to describe where the user can find that BT address. Unfortunately there is no other way so far. – Vladyslav Matviienko Oct 04 '18 at 17:06
  • Yeah, that is the only workaround that I am using too. Its painful – Deepak kaku Oct 04 '18 at 17:21
  • Maybe this will help Vladyslav , https://stackoverflow.com/a/52251086/6142219 – Deepak kaku Oct 08 '18 at 18:46
  • @Deepakkaku that's what I said: `Neither user 10141 nor current process has android.permission.LOCAL_MAC_ADDRESS, which means that this method requires permission, available only for system-level apps.`. My app is non-system-level app – Vladyslav Matviienko Jul 19 '19 at 08:45

0 Answers0