Secure.getString(mContext.getContentResolver(),"bluetooth_address")在Android O中返回null

Ngụ*_*ắng 6 java android android-permissions android-bluetooth android-8.0-oreo

当我试图通过这种方式在Android O设备上获取Blutooth地址时:

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();            
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的所有代码仍然运行良好我仍然使用该代码Anroid O(8.0)它返回macAddress = null.

laa*_*lto 7

您的应用需要持有LOCAL_MAC_ADDRESS权限:

Settings.Secure.bluetooth_address:设备蓝牙MAC地址.在O中,这仅适用于持有LOCAL_MAC_ADDRESS权限的应用.

https://android-developers.googleblog.com/2017/04/changes-to-device-identifiers-in.html

但是,LOCAL_MAC_ADDRESS权限是系统权限,因此在实践中您的应用无法拥有它.