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

标签 java android android-permissions android-bluetooth android-8.0-oreo

当我尝试通过这种方式在 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/

相关文章:

Intellij IDEA 2020.3.3 中 Switch 的 Java 16 模式匹配问题(预期表达式)

java - 填充 Struts 2 选择菜单并重定向操作

java - EclipseLink 中的批量乐观锁定查询

java - 计算 myClickHandler 函数不起作用

android - 使用某些指定的发型图像模式在发型上应用滤色器的最佳方法是什么?

java - 如何覆盖android中的权限管理器

java - 基本数学运算

java - 客户端必须具有 ACCESS_FINE_LOCATION 权限才能请求 PRIORITY_HIGH_ACCURACY 位置运行时错误

android - 位置 : GPS/network permissions: wanted if available

java - 自定义列表开始新 Activity ?