简体   繁体   English

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

[英]Trying to connect multiple devices via Bluetooth LE to android device

The Problem 问题

I am trying to connect multiple Bluetooth LE devices (heart rate monitors) to my android device. 我正在尝试将多个Bluetooth LE设备(心率监视器)连接到我的android设备。 I am able to scan, and connect to them. 我能够扫描并连接到它们。 They stream just fine, just that when I output the notifications it jumps between one sensor's data to another. 它们流式传输很好,只是当我输出通知时,它在一个传感器的数据与另一个传感器的数据之间跳转。

I basically used this GitHub source code for a majority of this project. 对于该项目的大部分内容,我基本上都使用 GitHub源代码。 In the project, they use ONE service (called BluetoothLeService). 在项目中,他们使用一项服务(称为BluetoothLeService)。 In my project I have created TWO services, pretty much duplicates of each other except one handles calls for one user, the other one handles for the other user. 在我的项目中,我创建了两个服务,彼此之间几乎是重复的,除了一个处理一个用户的呼叫,另一个处理另一个用户的呼叫。

I'm getting this output onto the console: 我正在将此输出输出到控制台:

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 / 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. 07-27 21:14:01.786 9062-9062 / com.example.android.aware D / BluetoothLeService2:尝试使用现有的mBluetoothGatt进行连接。 THIS ERROR IS FROM BLS2 该错误来自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 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-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.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. 07-27 21:14:01.816 9062-9062 / com.example.android.aware D / BluetoothLeService:尝试使用现有的mBluetoothGatt进行连接。 THIS ERROR IS FROM BLS1 该错误来自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 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. 07-27 21:14:01.826 9062-10009 / com.example.android.aware I / BluetoothLeService:已与GATT服务器断开连接。


Attempts 尝试次数

  • I've tried to add a new CLIENT_CHARACTERISTIC_CONFIG (called it CLIENT_CHARACTERISTIC_CONFIG2) string in the SampleGattAttributes class, this comes to play in the following code snippet (from the service classes): 我试图在SampleGattAttributes类中添加一个新的CLIENT_CHARACTERISTIC_CONFIG(称为CLIENT_CHARACTERISTIC_CONFIG2)字符串,这在以下代码片段(来自服务类)中起作用:

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


I think the problem, like the consoles says, has something to do with the variable in both Services called mBluetoothGatt (which is a BluetoothGatt type object). 我认为问题就像控制台所说的那样,与两个服务中称为mBluetoothGatt的变量(这是BluetoothGatt类型的对象)有关。 My thinking is like this: 我的想法是这样的:

If I have two services running synchronously, then if I create two different BluetoothGatt objects in each, why am I being told that the same BluetoothGatt object is being used? 如果我有两个服务同步运行,那么如果我在每个服务中创建两个不同的BluetoothGatt对象,为什么会被告知使用相同的BluetoothGatt对象?

I was not see the source code you referred but did you stored the different BluetoothGattCallback or return values for each connection? 我没有看到您引用的源代码,但是您是否为每个连接存储了不同的BluetoothGattCallback或返回值? I mean how did you judge the two devices? 我的意思是您如何判断这两种设备? The connectGatt will return a BluetoothGattCallback instance to notify the status or you can use this instance to do further operations, eg know the connect status etc. connectGatt将返回BluetoothGattCallback实例以通知状态,或者您可以使用该实例进行进一步的操作,例如,了解连接状态等。

I solved this problem by outputting data based on address of the device. 我通过基于设备地址输出数据解决了这个问题。

So instead of just spitting out the notifications, I put a condition (is the DisplayData() method) that said, "Hey, only output here if this is the device whose output we're looking for here." 因此,我不只是吐出通知,而是输入一个条件(是DisplayData()方法),说:“嘿,只有在这是我们要在这里寻找其输出的设备时,才在这里输出。”

Sure, the devices were not time-synched, but when you log using the time, that helps. 当然,这些设备不是时间同步的,但是当您使用时间记录日志时,会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM