这个问题困扰了我好几天。蓝牙连接代码在第一种设备(心率检测器)上与其他手机正常工作。对于第二种设备(也是心率检测器),它们可以很好地与HTC、华硕等移动设备配合使用。但是,大多数第二种设备不能很好地连接到sony xperia Z1~Z3,samsung N5(我不确定实际型号)。
观察 logcat,调用 gatt.discoverServices() 时发生错误。我在 BluetoothGattCallback -> onConnectionStateChange -> newState == STATE_CONNECTED 中调用此方法。
得到这样的服务很正常:
05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_upstreams_evt: Event 7
05-04 12:08:15.678: D/BtGatt.GattService(23132): onSearchResult() - address=F9:CE:5F:A3:69:CF, uuid=00001800-0000-1000-8000-00805f9b34fb
05-04 12:08:15.678: D/BluetoothGatt(22365): onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001800-0000-1000-8000-00805f9b34fb
但是在这里我得到了六项服务,在其他设备中我得到了七项服务。(丢一个自定义UUID)
服务:
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001800-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=00001801-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=0000180a-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=0000180d-0000-1000-8000-00805f9b34fb
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=6c721838-5bf1-4f64-9170-381c08ec57ee
onGetService() - Device=F9:CE:5F:A3:69:CF UUID=6c721550-5bf1-4f64-9170-381c08ec57ee
以下是我认为是错误结果的 logcat 的摘录:
05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_get_characteristic
05-04 12:08:15.678: D/BtGatt.btif(23132): btgattc_handle_event: Event 1007
05-04 12:08:15.678: E/bt-btif(23132): No server cache available
05-04 12:08:15.678: D/BtGatt.GattService(23132): onGetCharacteristic() - address=F9:CE:5F:A3:69:CF, status=133, charUuid=449e4528-7633-6ccc-8000-00805f9b34fb, prop=2
05-04 12:08:15.678: D/BtGatt.btif(23132): btif_gattc_get_included_service
05-04 12:08:15.678: D/BtGatt.btif(23132): btgattc_handle_event: Event 1011
05-04 12:08:15.678: E/bt-btif(23132): No server cache available
05-04 12:08:15.678: E/BtGatt.btif(23132): bta_to_btif_uuid: Unknown UUID length 30209!
05-04 12:08:15.688: D/BtGatt.GattService(23132): onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001800-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
05-04 12:08:15.688: D/BtGatt.GattService(23132): continueSearch() - connid=5, status=0
这些 logcat 在开始时对每个服务重复六次,具有相同的 UUID = 449e4528-7633-6ccc-8000-00805f9b34fb。并且这个UUID不会出现在其他成功案例中。
特点:(只是粘贴不同的部分)
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001800-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=00001801-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=0000180a-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=0000180d-0000-1000-8000-00805f9b34fb, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=6c721838-5bf1-4f64-9170-381c08ec57ee, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
onGetIncludedService() - address=F9:CE:5F:A3:69:CF, status=133, uuid=6c721550-5bf1-4f64-9170-381c08ec57ee, inclUuid=449e4528-7633-6ccc-8000-00805f9b34fb
我发现了一个类似的问题:onServiceDiscoverd does not contain all service
但是我在 onLeScan() 之后在 runOnUiThread 中调用了每个 device.connectGatt(),我还尝试在单击按钮时调用该方法的特定地址(一定要 runOnUiThread)。
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("F4:36:8C:9B:87:87");
device.connectGatt(this, false, mGattCallback);
此解决方案不起作用。
我还尝试关闭wifi并重新启动蓝牙。这适用于 mi2,但不适用于 xperia Z1。
有人有什么主意吗?非常感谢!:)