2

当我试图通过这种方式来获得的Android O设备上Blutooth地址:Secure.getString(mContext.getContentResolver(), “bluetooth_address”)返回的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();    
    } 
} 

所有代码以上工作我仍然使用Anroid O(8.0)的代码,它返回macAddress = null。

回答

2

您的应用程序将需要保持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 permission is a system permission所以实际上您的应用程序不能拥有它。

+0

我按照你的方式提问之前,但代码不工作,因为“LOCAL_MAC_ADDRESS权限是一个系统权限,所以在实践中你的应用程序不能拥有它。”就像你说的 。那么,没有任何解决方案可以在Android O上获得蓝牙地址? –

+0

您想通过获取地址解决什么问题? – laalto

+0

我使用蓝牙地址在我的应用程序中识别我的设备。你有任何想法来解决这个问题? –