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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With