【问题标题】:Trying to connect multiple devices via Bluetooth LE to android device尝试通过蓝牙 LE 将多个设备连接到 android 设备
【发布时间】:2016-12-02 05:33:34
【问题描述】:

问题

我正在尝试将多个蓝牙 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 启用: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

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 启用: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

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


尝试

  • 我尝试在 SampleGattAttributes 类中添加一个新的 CLIENT_CHARACTERISTIC_CONFIG(称为 CLIENT_CHARACTERISTIC_CONFIG2)字符串,这在以下代码 sn-p(来自服务类)中起作用:

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


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

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

【问题讨论】:

  • 尽管您认为您应该能够在实践中独立处理这两个设备,但我发现让事情变得可靠的唯一方法是同步多个外围设备的代码,所以我只有一个一次对单个外围设备的单个请求。它使启动非常缓慢。

标签: java android bluetooth bluetooth-lowenergy


【解决方案1】:

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

因此,我不只是吐出通知,而是设置了一个条件(是 DisplayData() 方法),它说:“嘿,仅当这是我们在此处查找其输出的设备时才在此处输出。”

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-11
      • 1970-01-01
      • 2018-11-16
      • 2020-01-27
      • 1970-01-01
      • 2014-10-04
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多