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

  • 获取蓝牙适配器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'

猜你喜欢

转载自blog.csdn.net/mygod2008ok/article/details/80718950