Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 396
Ngụy Thắng Avatar asked Aug 08 '17 09:08

Ngụy Thắng


2 Answers

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.

like image 106
laalto Avatar answered Nov 17 '22 21:11

laalto


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.

like image 20
Perazzo Avatar answered Nov 17 '22 22:11

Perazzo