蓝牙 GATT - onServicesDiscovered(BluetoothGatt gatt, int status) 不包含所有服务

Bluetooth GATT - onServicesDiscovered(BluetoothGatt gatt, int status) does not contain all Services

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

代码

上有效

代码

上无效

在发生故障的设备上,它通常会发现一个(两个)设备上的所有 BLE 服务,这些设备通告相同的 BLE 服务。重新启动 Android 设备 and/or 摆弄 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) returns 所有 BLE 服务。下层是否存在某种资源泄漏?

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

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

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

此问题未发生在 Nexus 5 4.4.2 或 5.1 和 Nexus 4 4.4 上,但发生在 Samsung S5 (4.4.2) 上(至少)。

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

希望能帮到别人。