Secure.getString(mContext.getContentResolver(), “bluetooth_address”) return null in Android O

谁都会走 提交于 2019-12-12 10:45:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!