如何在最新的Android设备上获取蓝牙MAC地址

时间:2018-09-18 05:53:00

标签: android bluetooth

我正在尝试使用以下代码从android设备获取蓝牙MAC地址

private String getBluetoothMacAddress() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
        try {
            Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService");
            mServiceField.setAccessible(true);

            Object btManagerService = mServiceField.get(bluetoothAdapter);

            if (btManagerService != null) {
                bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService);
            }
        } catch (NoSuchFieldException e) {

            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();

        } catch (IllegalAccessException e) {
            e.printStackTrace();

        } catch (InvocationTargetException e) {
            e.printStackTrace();

        }
    } else {
        bluetoothMacAddress = bluetoothAdapter.getAddress();
    }
    return bluetoothMacAddress;
}

但是在最新的android版本上,它说,

  

需要LOCAL_MAC_ADDRESS权限:用户10233或当前用户均不是   进程具有android.permission.LOCAL_MAC_ADDRESS

还有其他解决方案吗?

0 个答案:

没有答案
相关问题