安卓获取手机本身的蓝牙MAC地址

匿名 (未验证) 提交于 2019-12-03 00:29:01

  • 获取蓝牙适配器BluetoothAdpater
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothAdapter adapter = (BluetoothAdapter) getApplicationContext().getSystemService(BLUETOOTH_SERVICE);

  • 安卓6以后的版本使用此方法拿不到真实的MAC地址

String macAddr = adapter.getAddress();

  • 需要采用以下镜像来获取

Object bluetoothManageService = new Mirror().on(adapter).get().field("mService"); if (bluetoothManageService == null)     return null; Object address = new Mirror().on(bluetoothManageService).invoke().method("getAddress").withoutArgs(); if (address != null && address instanceof String) {     return (String) address; } else {     return null; }

  • 最后别忘了添加依赖关系

implementation 'net.vidageek:mirror:1.6.1'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!