Android 蓝牙,在 LG 手机上查找名称为空的设备

发布于 2024-11-29 23:56:17 字数 3505 浏览 1 评论 0原文

我编写了一个小型蓝牙接收器,它可以处理大部分 BT 状态。

接收器适用于大多数情况,我将其接通,但在某些情况下,找到的设备的名称为空字符串,我无法区分一个设备和另一个设备。

以下是该发现的代码片段和一段日志来支持它:

注意:在第二次检测之前,BT 适配器已关闭并打开。

另一个注意: 这种情况每隔一两次就会发生一次。第一次找到名称,第二次没有名称

public class BT_Receiver
        extends BroadcastReceiver {
...
    @Override
    public void onReceive(Context context, Intent intent) {
        if (deviceDetectionListener.allDevicesFound())
            return;
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            state = BT_State.getInstanceForState(newState);
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            if (state == BT_State.On && enableAndDiscover)
                startDiscoveringDevices();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            state = BT_State.Discovering;
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            state = BT_State.On;
            Toast.makeText(activity, "Finished discovery", Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            int discoveryDuration = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, -1);
            state = BT_State.Advertising;
            Toast.makeText(activity, state.name() + ", duration: " + discoveryDuration, Toast.LENGTH_SHORT).show();
        }
        if (!BluetoothDevice.ACTION_FOUND.equals(action))
            return;
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
(Look at the LOG RESULT)       
                    Log.i(TAG, "Bluetooth device found: " + device.getName() + ", " + device.getBluetoothClass() + ", " + device.getAddress());
        deviceDetectionListener.newDeviceDetected(device);
        if (deviceDetectionListener.allDevicesFound())
            detectionCompleted();
    }

...
}

带有设备名称的日志:

08-15 22:54:16.500: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [bbbb:Demo gizmo 2], 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:16.578: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [aaaa:Demo gizmo 1], 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:33.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: Zomaar Iemand, 5a0204, 20:21:A5:C0:CF:6F

另一个没有名称的日志:

08-15 22:54:34.304: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 00:26:E2:66:31:30
08-15 22:54:36.882: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:38.601: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:39.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 520204, 78:CA:04:83:45:CD

提前致谢,

Adam。

I've written a small BluetoothReceiver, which handles most of the BT states.

The receiver works for most of the scenarios, I put it through, but in some cases the Devices found has an empty string for a name, and I cannot tell one device from another.

Here is the code snippet of the finding and a piece of Log to back it up:

NOTE: The BT adapter is been turned off and on before the second detection.

ANOTHER NOTE: This scenario happens exactly every second time. The first time the names are found, the second times there are no names

public class BT_Receiver
        extends BroadcastReceiver {
...
    @Override
    public void onReceive(Context context, Intent intent) {
        if (deviceDetectionListener.allDevicesFound())
            return;
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            state = BT_State.getInstanceForState(newState);
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            if (state == BT_State.On && enableAndDiscover)
                startDiscoveringDevices();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            state = BT_State.Discovering;
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            state = BT_State.On;
            Toast.makeText(activity, "Finished discovery", Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            int discoveryDuration = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, -1);
            state = BT_State.Advertising;
            Toast.makeText(activity, state.name() + ", duration: " + discoveryDuration, Toast.LENGTH_SHORT).show();
        }
        if (!BluetoothDevice.ACTION_FOUND.equals(action))
            return;
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
(Look at the LOG RESULT)       
                    Log.i(TAG, "Bluetooth device found: " + device.getName() + ", " + device.getBluetoothClass() + ", " + device.getAddress());
        deviceDetectionListener.newDeviceDetected(device);
        if (deviceDetectionListener.allDevicesFound())
            detectionCompleted();
    }

...
}

The Log with the devices names:

08-15 22:54:16.500: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [bbbb:Demo gizmo 2], 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:16.578: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [aaaa:Demo gizmo 1], 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:33.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: Zomaar Iemand, 5a0204, 20:21:A5:C0:CF:6F

Another Log without the names:

08-15 22:54:34.304: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 00:26:E2:66:31:30
08-15 22:54:36.882: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:38.601: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:39.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 520204, 78:CA:04:83:45:CD

Thanks in advance,

Adam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-12-06 23:56:17

这可能是一个功能而不是错误,我找到了一个解决方法,如果设备没有名称,我不会保存它,如果没有找到设备,请再次运行查询。

This is probably a feature not a bug, I found a workaround for this, that in case the device does not have a name I don't save it, and if no device was found run the inquiry again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文