问题
When I'm trying to get Blutooth 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 still I use that code for Anroid O (8.0) it's return macAddress = null.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/45564118/secure-getstringmcontext-getcontentresolver-bluetooth-address-return-null