[Bluetooth] Android 8.1 obtém endereço de dispositivo Bluetooth inválido; 02:00:00:00:00:00

Preâmbulo:

对于这个问题, 谷歌是这样说的:
	为了向用户提供更好的数据保护,从此版本开始,Android删除了使用Wi-Fi和蓝牙API对应用程序的设备本地硬件标识符的编程访问。

Endereço do link: https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-hardware-id

Ok, até agora já sabemos porque não conseguimos o endereço mac relevante, mas não se preocupe, deve haver uma estrada quando o carro chegar à montanha, e o barco estará reto quando chegar à ponte. Permita-me para fazer barulho primeiro.

Guan Ziwu é dinheiro rápido, você precisa conversar comigo em particular Q 7641436

A próxima solução é usar a reflexão. Embora não seja bom, pode resolver problemas práticos:

/**
	获取蓝牙地址
*/
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;
    }

Isso não é recomendado, mas só tenho essa maneira.Se houver uma maneira melhor, continuarei atualizando este artigo.

Supongo que te gusta

Origin blog.csdn.net/a_Chaon/article/details/85616377
Recomendado
Clasificación