Secure.getString (mContext.getContentResolver (), "bluetooth_address") returns null in Android O

When I try to get Blutooth address on Android O device like this:

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 the code above works well, but I am using this code for Anroid O (8.0), it returns macAddress = null.

+3


source to share


2 answers


Your app will need to hold the permission LOCAL_MAC_ADDRESS

:

Settings .Secure.bluetooth_address : Bluetooth device MAC address. In O, this is only available for applications that have LOCAL_MAC_ADDRESS permission.



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

However, permission LOCAL_MAC_ADDRESS

is a system permission
, so in practice your application cannot have it.

+3


source


Since you just want to identify your device, you can use getImei()

from the TelephonyManager API. Please note that you need request READ_PHONE_STATE

permission from the user as it has a dangerous security level.



If this API is vulnerable in your tests, look for alternative ways (which have their drawbacks) of the two other issues: The unique identifier of the Android device and the serial number of the device Android .

0


source







All Articles