1

我目前使用RxAndroidBle將固件寫入藍牙設備。它的工作方式是我必須在一個WRITE_CHARACTERISTIC_UUID上向藍牙設備發送數據塊(每塊580字節),然後在LISTEN_CHARACTERISTIC_UUID上監聽結果,以檢查藍牙設備是否已成功確認該塊。通知有時不會在RxAndroidBle中通知/寫入observable中調用

理想我想這樣寫,並通知鏈是所以我用從類似的堆棧溢出問題,此代碼(https://stackoverflow.com/a/41140523/734385)一個操作:

private byte[] writeData(byte[] _payload) { 
    return mBleConnection 
      .flatMap(// when the connection is available... 
        rxBleConnection -> rxBleConnection.setupNotification(LISTEN_UUID), // ... setup the notification... 
        (rxBleConnection, apScanDataNotificationObservable) -> Observable.combineLatest(// ... when the notification is setup... 
          rxBleConnection.createNewLongWriteBuilder().setBytes(_payload).setCharacteristicUuid(WRITE_UUID).setMaxBatchSize(20).build(), 
          apScanDataNotificationObservable.first(), // ... and observe for the first notification on the AP_SCAN_DATA 
          (writtenBytes, responseBytes) -> responseBytes // ... when both will appear return just the response bytes... 
        ) 
      ) 
      .flatMap(observable -> observable) // ... flatMap the result as it is Observable<byte[]>... 
      .toBlocking() 
      .first(); // ... and finish after first response is received to cleanup notifications 
} 

的問題是,這隻有前幾次,之後寫入操作成功,但我從未收到通知。 這是日誌的最後幾大塊之前停止:

02-23 08:46:33.765 2864-5256/com.company.android.ble I/BLE_FW_WRITE: [FW_CHUNK_24] starting 
02-23 08:46:33.772 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(182789785) 
02-23 08:46:33.772 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(182789785) 
02-23 08:46:33.773 2864-5256/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: true 
02-23 08:46:33.777 2864-5256/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(22527736) 
02-23 08:46:33.910 2864-2888/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:33.912 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(182789785) 
02-23 08:46:33.912 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(22527736) 
02-23 08:46:34.055 2864-3032/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:34.059 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicLongWrite(71420405) 
02-23 08:46:34.063 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(22527736) 
02-23 08:46:34.063 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicLongWrite(71420405) 
02-23 08:46:34.067 2864-3093/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
<more onCharacteristicWrites here, but omitted due to max post length> 
02-23 08:46:34.204 2864-3032/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
02-23 08:46:34.207 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicLongWrite(71420405) 
02-23 08:46:35.041 2864-3093/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicChanged characteristic=69af0004-f994-3a57-749b-0e0aad3fca18 
02-23 08:46:35.044 2864-5256/com.company.android.ble I/BLE_FW_WRITE: [FW_CHUNK_25] starting 
02-23 08:46:35.044 2864-4250/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: false 
02-23 08:46:35.048 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(183984640) 
02-23 08:46:35.049 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(183984640) 
02-23 08:46:35.053 2864-5256/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: true 
02-23 08:46:35.060 2864-5256/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(71847163) 
02-23 08:46:35.185 2864-3073/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:35.188 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(183984640) 
02-23 08:46:35.189 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(71847163) 
02-23 08:46:35.276 2864-2887/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:35.281 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicLongWrite(66222734) 
02-23 08:46:35.283 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(71847163) 
02-23 08:46:35.283 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicLongWrite(66222734) 
02-23 08:46:35.287 2864-3032/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
<more onCharacteristicWrites here, but omitted due to max post length> 
02-23 08:46:35.441 2864-3032/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
02-23 08:46:35.447 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicLongWrite(66222734) 
02-23 08:46:36.298 2864-2975/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicChanged characteristic=69af0004-f994-3a57-749b-0e0aad3fca18 
02-23 08:46:36.301 2864-4250/com.company.android.ble I/BLE_FW_WRITE: combine data for FW_CHUNK_25 
02-23 08:46:36.303 2864-4250/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: false 
02-23 08:46:36.303 2864-5256/com.company.android.ble D/SFlashRawWrite: msg length (total): 20, payload length: 3 
02-23 08:46:36.303 2864-5256/com.company.android.ble D/SFlashRawWrite: msgId: -128 
02-23 08:46:36.304 2864-5256/com.company.android.ble D/SFlashRawWrite: acked msg: 17 result: 1 
02-23 08:46:36.304 2864-5256/com.company.android.ble I/BLE_TIMING: chunk 25 took 1260 ms 
02-23 08:46:36.304 2864-5256/com.company.android.ble I/BLE_FW_WRITE: [FW_CHUNK_26] starting 
02-23 08:46:36.313 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(248228237) 
02-23 08:46:36.317 2864-5256/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: true 
02-23 08:46:36.319 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(248228237) 
02-23 08:46:36.321 2864-5256/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(164719036) 
02-23 08:46:36.452 2864-2888/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:36.455 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(248228237) 
02-23 08:46:36.456 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(164719036) 
02-23 08:46:36.602 2864-3032/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:36.616 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicLongWrite(227542569) 
02-23 08:46:36.623 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(164719036) 
02-23 08:46:36.624 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicLongWrite(227542569) 
02-23 08:46:36.632 2864-2975/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
<more onCharacteristicWrites here, but omitted due to max post length> 
02-23 08:46:36.897 2864-2975/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
02-23 08:46:36.902 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicLongWrite(227542569) 
02-23 08:46:37.563 2864-3093/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicChanged characteristic=69af0004-f994-3a57-749b-0e0aad3fca18 
02-23 08:46:37.566 2864-5256/com.company.android.ble I/BLE_FW_WRITE: [FW_CHUNK_27] starting 
02-23 08:46:37.567 2864-4250/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: false 
02-23 08:46:37.569 2864-5256/com.company.android.ble D/BluetoothGatt: setCharacteristicNotification() - uuid: 69af0004-f994-3a57-749b-0e0aad3fca18 enable: true 
02-23 08:46:37.572 2864-5256/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(150502697) 
02-23 08:46:37.573 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(150502697) 
02-23 08:46:37.574 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationDescriptorWrite(128172508) 
02-23 08:46:37.711 2864-2888/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:37.715 2864-4250/com.company.android.ble D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicLongWrite(58981097) 
02-23 08:46:37.730 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(150502697) 
02-23 08:46:37.730 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationDescriptorWrite(128172508) 
02-23 08:46:37.871 2864-3093/com.company.android.ble D/RxBle#BluetoothGatt: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0 
02-23 08:46:37.876 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationDescriptorWrite(128172508) 
02-23 08:46:37.879 2864-3150/com.company.android.ble D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicLongWrite(58981097) 
02-23 08:46:37.891 2864-3073/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
<more onCharacteristicWrites here, but omitted due to max post length> 
02-23 08:46:38.126 2864-3073/com.company.android.ble D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=69af0003-f994-3a57-749b-0e0aad3fca18 status=0 
02-23 08:46:38.128 2864-3150/com.company.android.ble D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicLongWrite(58981097) 

正如你可以在年底看我不在最後得到一個

onCharacteristicChanged characteristic=69af0004-f994-3a57-749b-0e0aad3fca18 

注:寫數據是在Scheduler.io()的線程中運行,並從庫中我使用的是最新的主提交(https://github.com/Polidea/RxAndroidBle/commit/37ecef5c270c792bcb9193f6dd76b8711642e0ff)(因爲我需要新的LongWriteBuilder)

有趣的是,如果我啓動另外「虛擬」通知可觀察的,什麼都不做:

mBleConnection 
      .flatMap(rxBleConnection -> rxBleConnection.setupNotification(LISTEN_UUID)) 
      .flatMap(notificationObservable -> notificationObservable) 
      .subscribe(Actions.empty(), Actions.empty()) 

上述可觀察的作品,我得到每一個通知。

我懷疑我在通知的設置上做錯了什麼,或者我錯過了某處的延遲。

回答

1

有兩件事情:

  • 的通知,但不保證接收 - 跡象都

  • 看來,Android操作系統在反覆設置通知關閉和失敗 - 判斷來自BluetoothGattCallback.onCharacteristicChanged()未被調用的日誌。

潛在的解決方案是設置第一個writeData(byte[] _payload)的開始和經過兩個_payloadObservable<byte[]>所以它看起來像writeData(byte[] _payload, Observable<byte[]> listenCharacteristicNotifications)之前通知。然後,您可以在LISTEN_CHARACTERISTIC_UUID上設置一次通知,並將其用於每次寫入。

+1

只使用一個通知observable而不是每次都設置新的通知,謝謝 – tiqz

相關問題