Android7.0 获取手机蓝牙MAC地址

从Android6.0开始,通过BluetoothAdapter.getDefaultAdapter().getAddress()获取的地址是一个固定值02:00:00:00:00:00,部分从低版本升级到6.0的手机也还是可以获取真实的MAC地址的。下面的方法可以在高版本的Android系统上获取手机真实的蓝牙MAC地址,该方法摘自stackoverflow,查看原文点击这里:

private String getBtAddressViaReflection() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService");
    if (bluetoothManagerService == null) {
        Log.w(TAG, "couldn't find bluetoothManagerService");
        return null;
    }
    Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs();
    if (address != null && address instanceof String) {
        Log.w(TAG, "using reflection to get the BT MAC address: " + address);
        return (String) address;
    } else {
        return null;
    }
}

工程需要添加对net.vidageek:mirror的引用,如:

compile 'net.vidageek:mirror:1.6.1'

你可能感兴趣的:(android蓝牙笔记)