8

When I'm trying to get Bluetooth Address on Android O device by this way:

private String getBlutoothAddress(Context mContext){  
    // Check version API Android
    BluetoothAdapter myBluetoothAdapter;
       
    String macAddress;

    int currentApiVersion = android.os.Build.VERSION.SDK_INT;

    if (currentApiVersion >= android.os.Build.VERSION_CODES.M) {
        macAddress = Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_address"); 
    } else {
        // Do this for phones running an SDK before lollipop
        macAddress = myBluetoothAdapter.getAddress();            
    }
}

All code above working well until I use that code for Android O (8.0) it returns macAddress = null.

2 Answers2

9

Your app would need to hold the LOCAL_MAC_ADDRESS permission:

Settings.Secure.bluetooth_address: Device Bluetooth MAC address. In O, this is only available to apps holding the LOCAL_MAC_ADDRESS permission.

https://android-developers.googleblog.com/2017/04/changes-to-device-identifiers-in.html

However, the LOCAL_MAC_ADDRESS permission is a system permission so in practice your app cannot have it.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • 1
    I follow your way before asking but code not work because " The LOCAL_MAC_ADDRESS permission is a system permission so in practice your app cannot have it." like you say . So don't have any solution to get Bluetooth Address on Android O ? – Ngụy Thắng Aug 09 '17 at 02:16
  • What is the problem you're trying to solve by obtaining the address? – laalto Aug 09 '17 at 05:33
  • I use bluetooth address to identify my device in my app . You have any idea to solve this problem ? – Ngụy Thắng Aug 09 '17 at 07:04
  • android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address") return null in andriod 10 – Bolt UIX Aug 12 '20 at 11:15
1

Since you just want to identify your device, you can use getImei() from TelephonyManager API. Note that you will need to request READ_PHONE_STATE permission from the user since it has the dangerous protection level.

If such API proves to be not reliable on your tests, please look for alternative ways (which have their own drawbacks) from this other two questions: Unique Android Device ID and Serial Number of Android device.

Perazzo
  • 1,127
  • 13
  • 24