当我尝试通过这种方式在 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) 它返回 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所以实际上你的应用程序不能拥有它。
关于java - Secure.getString(mContext.getContentResolver(), "bluetooth_address") 在 Android O 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45564118/