Android获取本机蓝牙地址
16lz
2021-01-23
从Android6.0开始,通过BluetoothAdapter.getDefaultAdapter().getAddress()获取的地址是一个固定值02:00:00:00:00:00,部分从低版本升级到6.0的手机也还是可以获取真实的MAC地址的。下面通过反射的方式
获取蓝牙地址:
public static String getBtAddressByReflection() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Field field = null; try { field = BluetoothAdapter.class.getDeclaredField("mService"); field.setAccessible(true); Object bluetoothManagerService = field.get(bluetoothAdapter); if (bluetoothManagerService == null) { return null; } Method method = bluetoothManagerService.getClass().getMethod("getAddress"); if(method != null) { Object obj = method.invoke(bluetoothManagerService); if(obj != null) { return obj.toString(); } } } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
更多相关文章
- android中基于蓝牙开发的demo
- Android 打开系统蓝牙设置
- 蓝牙原理Android代码实现
- android蓝牙开发入门到精通2---查找可见蓝牙并绑定
- Android获取有线和无线(wifi)的IP地址
- Android GPS 获得 经纬度 并得到该 坐标 精确地址
- android 获取网络IP地址