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

时间:2016-07-28 02:00:59

标签: java android bluetooth bluetooth-lowenergy

问题

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

我基本上使用了this GitHub源代码来完成这个项目的大部分内容。在项目中,他们使用ONE服务(称为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:尝试使用现有的mBluetoothGatt进行连接。这个错误来自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 < / p>      

07-27 21:14:01.796 9062-10009 / com.example.android.aware I / BluetoothLeService2:与GATT服务器断开连接。

     

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:尝试使用现有的mBluetoothGatt进行连接。这个错误来自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 < / p>      

07-27 21:14:01.826 9062-10009 / com.example.android.aware I / BluetoothLeService:与GATT服务器断开连接。


的尝试

  • 我尝试在SampleGattAttributes类中添加一个新的CLIENT_CHARACTERISTIC_CONFIG(称为CLIENT_CHARACTERISTIC_CONFIG2)字符串,这可以在以下代码片段中播放(来自服务类):

      BluetoothGattDescriptor descriptor = characteristic
                    .getDescriptor(UUID
                            .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    


我认为问题,如控制台所说,与两个服务中的变量有关,称为mBluetoothGatt(这是一个BluetoothGatt类型的对象)。我的想法是这样的:

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

2 个答案:

答案 0 :(得分:0)

我没有看到你提到的源代码,但你是否存储了不同的BluetoothGattCallback或每个连接的返回值?我是说你是怎么判断这两种设备的? connectGatt将返回BluetoothGattCallback实例以通知状态,或者您可以使用此实例进行进一步操作,例如知道连接状态等。

答案 1 :(得分:0)

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

所以我不是只是吐出通知,而是说一个条件(就是DisplayData()方法)说,&#34;嘿,如果这是我们正在寻找其输出的设备,则只输出这里&#34;

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