Secure.getString(mContext.getContentResolver(), "bluetooth_address") return 在 Android 中为空 O
Secure.getString(mContext.getContentResolver(), "bluetooth_address") return null in Android O
当我尝试通过这种方式在 Android O 设备上获取蓝牙地址时:
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();
}
}
上面的所有代码都运行良好,直到我将该代码用于 Android O (8.0) it returns macAddress = null.
您的应用需要持有 LOCAL_MAC_ADDRESS
权限:
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
但是,LOCAL_MAC_ADDRESS
permission is a system permission 所以实际上您的应用程序不能拥有它。
由于您只想识别您的设备,您可以使用 getImei()
来自 TelephonyManager API. Note that you will need to request READ_PHONE_STATE
permission 的用户,因为它具有危险保护级别。
如果这样的 API 在您的测试中被证明不可靠,请从另外两个问题 Unique Android Device ID and Serial Number of Android device 中寻找替代方法(它们有其自身的缺点)。
当我尝试通过这种方式在 Android O 设备上获取蓝牙地址时:
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();
}
}
上面的所有代码都运行良好,直到我将该代码用于 Android O (8.0) it returns macAddress = null.
您的应用需要持有 LOCAL_MAC_ADDRESS
权限:
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
但是,LOCAL_MAC_ADDRESS
permission is a system permission 所以实际上您的应用程序不能拥有它。
由于您只想识别您的设备,您可以使用 getImei()
来自 TelephonyManager API. Note that you will need to request READ_PHONE_STATE
permission 的用户,因为它具有危险保护级别。
如果这样的 API 在您的测试中被证明不可靠,请从另外两个问题 Unique Android Device ID and Serial Number of Android device 中寻找替代方法(它们有其自身的缺点)。