如何获取我的设备的蓝牙ID。我尝试获取,但它们始终返回02:00:00:00:00:00

问题描述 投票:0回答:1

您好,我尝试获取我的蓝牙设备ID,但这不起作用。我尝试下面的代码,但没有得到它,但现在可以正常工作。这样您可以检查我犯了哪个错误。

public static String getBluetoothMacAddress(Context context) {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    String bluetoothMacAddress = "0";
    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);

            Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
        }
     bluetoothMacAddress=   android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
    } catch (Exception e) {
        Utils.printLog("bluetoothMacAddress1 ", e.getMessage());
    }

    if (bluetoothMacAddress == null || bluetoothMacAddress.equals("") || bluetoothMacAddress.equals("02:00:00:00:00:00")) {
        bluetoothMacAddress = "0";
    }
    Utils.printLog("bluetoothMacAddress ", bluetoothMacAddress);
    return bluetoothMacAddress;
}
android android-bluetooth android-service-binding
1个回答
0
投票

您的mac地址实际上是一种ID,但在网络层上,因此网络可以识别您的设备。对于特定设备,您应该始终获得相同的mac地址,尽管制造中会有一些例外情况,即多个设备可能具有相同的mac(例如Arduino控制器)。但是,这是EXCEPTIONAL,而不是一般的

© www.soinside.com 2019 - 2024. All rights reserved.