尝试通过蓝牙 LE 将多个设备连接到 android 台设备

Trying to connect multiple devices via Bluetooth LE to android device

问题

我正在尝试将多个蓝牙 LE 设备(心率监测器)连接到我的 android 设备。我能够扫描并连接到它们。它们流式传输得很好,只是当我输出通知时,它会在一个传感器的数据与另一个传感器的数据之间跳转。

我基本上使用 this GitHub 这个项目的大部分源代码。在项目中,他们使用一个服务(称为 BluetoothLeService)。在我的项目中,我创建了两个服务,除了一个处理一个用户的呼叫,另一个处理另一个用户的呼叫外,几乎彼此重复。

我正在将此输出发送到控制台:

07-27 21:14:01.786 9062-9062/com.example.android.aware D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a37-0000-1000-8000-00805f9b34fb enable: true

07-27 21:14:01.786 9062-9062/com.example.android.aware D/BluetoothLeService2: Trying to use an existing mBluetoothGatt for connection. THIS ERROR IS FROM BLS2

07-27 21:14:01.796 9062-10009/com.example.android.aware D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=7 device=E3:64:43:37:D2:AA

07-27 21:14:01.796 9062-10009/com.example.android.aware I/BluetoothLeService2: Disconnected from GATT server.

07-27 21:14:01.796 9062-9062/com.example.android.aware D/BluetoothGatt: setCharacteristicNotification() - uuid: 00002a37-0000-1000-8000-00805f9b34fb enable: true

07-27 21:14:01.816 9062-9062/com.example.android.aware D/BluetoothLeService: Trying to use an existing mBluetoothGatt for connection. THIS ERROR IS FROM BLS1

07-27 21:14:01.826 9062-10009/com.example.android.aware D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=00:22:D0:41:CA:B6

07-27 21:14:01.826 9062-10009/com.example.android.aware I/BluetoothLeService: Disconnected from GATT server.


尝试次数


我认为这个问题,就像控制台所说的那样,与两个服务中名为 mBluetoothGatt 的变量(这是一个 BluetoothGatt 类型的对象)有关。我的思路是这样的:

如果我有两个同步服务 运行,那么如果我在每个服务中创建两个不同的 BluetoothGatt 对象,为什么我会被告知正在使用同一个 BluetoothGatt 对象?

我没有看到您提到的源代码,但您是否为每个连接存储了不同的 BluetoothGattCallback 或 return 值?我的意思是你是如何判断这两个设备的? connectGatt 将 return 一个 BluetoothGattCallback 实例来通知状态,或者你可以使用这个实例来做进一步的操作,例如了解连接状态等

我通过根据设备地址输出数据解决了这个问题。

因此,我没有只是吐出通知,而是提出了一个条件(是 DisplayData() 方法),"Hey, only output here if this is the device whose output we're looking for here."

当然,这些设备没有时间同步,但是当您使用时间登录时,这会有所帮助。