我需要获取设备的蓝牙MAC地址。
在 Android 6 之前,这很简单BluetoothAdapter.getDefaultAdapter().getAddress()
。之后我们不得不使用一个简单的解决方法:String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
。但后来(在 Android 8 AFAIK 中)它也被关闭了,但发现了另一种解决方法:
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) {
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
} else {
bluetoothMacAddress = bluetoothAdapter.getAddress();
}
但从 Android 8.1 开始尝试访问该方法会抛出异常:
java.lang.reflect.InvocationTargetException Caused by: java.lang.SecurityException: Need LOCAL_MAC_ADDRESS permission: Neither user 10141 nor current process has android.permission.LOCAL_MAC_ADDRESS
,这意味着该方法需要权限,仅适用于系统级应用程序。
那么问题是,是否有任何解决方法可以在 Android 8.1 中获取蓝牙地址?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)