android8.1获取蓝牙地址,[蓝牙] Android 8.1 获取蓝牙设备地址无效;02:00:00:00:00:00
序言:
對于這個問題, 谷歌是這樣說的:
為了向用戶提供更好的數據保護,從此版本開始,Android刪除了使用Wi-Fi和藍牙API對應用程序的設備本地硬件標識符的編程訪問。
ok、至此我們已經知道為什么獲取不了相關的mac地址,不過不要擔心, 車到山前必有路,船到橋頭自然直,容許我賣個關子先。
關子五快錢一個,需要私聊我Q 7641436
接下來是解決辦法利用反射,雖然不好, 但是可以解決實際問題:
/**
獲取藍牙地址
*/
private String getBluetoothMacAddress() {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
String bluetoothMacAddress = "";
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 | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) {
}
return bluetoothMacAddress;
}
不推薦這樣做,但是我只有這個辦法, 后續如果有更好的辦法,我會繼續更新這篇文章。
總結
以上是生活随笔為你收集整理的android8.1获取蓝牙地址,[蓝牙] Android 8.1 获取蓝牙设备地址无效;02:00:00:00:00:00的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 茅台迎宾酒价格53度多少钱一瓶
- 下一篇: android微博客户端下载,iBeeb