【问题标题】:Bluetooth GATT - onServicesDiscovered(BluetoothGatt gatt, int status) does not contain all Services蓝牙 GATT - onServicesDiscovered(BluetoothGatt gatt, int status) 不包含所有服务
【发布时间】:2015-06-22 19:56:06
【问题描述】:

我有代码连接到,然后尝试发现自定义 BLE 服务器设备提供的所有服务,广告自定义服务。

代码

上运行
  • Nexus 5 与 Android 5.1
  • 三星 SM-T320 安卓 4.4.2
  • 中兴 Blade Vec 4G 与 Android 4.4.2

代码不起作用

  • 三星 SM-T360 搭载 Android 4.4.4
  • 搭载 Android 4.4.2 的三星 GT-I9195

在失败的设备上,它通常会在一个(或两个)设备上发现所有的 BLE 服务,这些设备宣传相同的 BLE 服务。重新启动 Android 设备和/或摆弄 wifi/bt 会使其发现其他 BLE 设备上的所有服务,但不是第一个(!)。

有人知道三星设备在 BLE 服务发现方面存在一个突出的错误吗?

拥有所有服务:

04-16 13:28:00.999: V/ScanningState(9741): onServicesDiscovered()
04-16 13:28:00.999: I/ScanningState(9741): The device has service 00001800-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service 0000180a-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service 0000180f-0000-1000-8000-00805f9b34fb
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba0000-c6b5-11e2-8b8b-0800200c9a66
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba0100-c6b5-11e2-8b8b-0800200c9a66
04-16 13:28:00.999: I/ScanningState(9741): The device has service f0ba1100-c6b5-11e2-8b8b-0800200c9a66

缺少服务:

04-16 14:05:31.179: V/ScanningState(10710): onServicesDiscovered()
04-16 14:05:31.179: I/ScanningState(10710): The device has service 00001800-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service 0000180a-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service 0000180f-0000-1000-8000-00805f9b34fb
04-16 14:05:31.179: I/ScanningState(10710): The device has service f0ba0000-c6b5-11e2-8b8b-0800200c9a66
04-16 14:05:31.179: W/ScanningState(10710): Device is missing sensor service
04-16 14:05:31.179: W/ScanningState(10710): Device is missing storage service
04-16 14:05:31.179: I/ScanningState(10710): Candidate did not support required services

对于第二次(失败)扫描,我还看到以下日志:

04-16 14:24:26.310: D/BtGatt.GattService(2856): onGetCharacteristic() - address=00:07:80:13:14:33, status=133, charUuid=00000000-0000-0000-0000-000000000000, prop=0
04-16 14:24:26.310: D/BtGatt.btif(2856): btif_gattc_get_included_service
04-16 14:24:26.310: D/BtGatt.btif(2856): btgattc_handle_event: Event 1011
04-16 14:24:26.310: E/bt-btif(2856): No server cache available
04-16 14:24:26.310: E/BtGatt.btif(2856): bta_to_btif_uuid: Unknown UUID length 25104!

更新:

进一步调查表明,如果一次只有一个 BLE 服务设备存在,onServicesDiscovered(BluetoothGatt gatt, int status) 会返回所有 BLE 服务。低层的某种资源泄漏?

【问题讨论】:

    标签: android bluetooth-lowenergy android-bluetooth


    【解决方案1】:

    我尝试在 UI 上运行,有更多机会发现,但还不够(工作 1/10 次)。

    这个问题不会在 Nexus 5 4.4.2 或 5.1 和 Nexus 4 4.4 上发生,而是在三星 S5 (4.4.2) 上发生(至少)。

    诀窍(在许多线程中都有解释)是在调用mBluetoothGatt.discoverServices() 之后在我的服务中设置睡眠SystemClock.sleep(3000);。我之前尝试过,但没有任何改变。

    希望可以帮助别人。

    【讨论】:

    • 好吧。根本原因是BLE实现不是线程安全的。 sleep 可能会缓解问题,但要完全解决它,您需要通过 Handler 编组所有回调。
    • 线程安全与否,在主线程上运行的技巧通常被弃用,有利于添加大约。在停止扫描->连接->发现服务->开始使用特征的每次转换之间有 2000 毫秒的睡眠。
    【解决方案2】:

    好的,简短的回答是不要在除主线程之外的任何其他线程上调用connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback)

    如果我在连接到设备时收到onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord),我会缓存引用,然后在与第一个设备断开连接后在回调onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 中调用connectGatt()。这导致了这个问题中描述的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 2023-03-07
      相关资源
      最近更新 更多