2017-08-02 151 views
0

我正在做一個長期寫入BLE進行OTA更新,但我需要等待BLE設備的寫入響應發送更多數據,但我不知道如何捕獲設備寫響應,我使用的是三星Galaxy Tab S2與Android 7,和科特林我的代碼rxAndroidBle得到很長的寫回應

override fun otaDataWrite(data:ByteArray) { 
    manager.connection?.flatMap { rxBleConnection: RxBleConnection? -> rxBleConnection?.createNewLongWriteBuilder() 
      ?.setCharacteristicUuid(OTACharacteristics.OTA_DATA.uuid) 
      ?.setBytes(data) 
      ?.setMaxBatchSize(totalPackages) 
      ?.build() 
    }?.subscribe({ t: ByteArray? -> 
     Log.i("arrive", "data ${converter.bytesToHex(t)}") 
     manageOtaWrite() 
    }, { t: Throwable? -> t?.printStackTrace() }) 

每次我寫的特徵時間的訂閱與寫入的數據立即迴應我,我需要捕獲該特性的響應,用於發送更多數據

+0

不是一個真正的回答你的問題,但如果你需要快速寫入大量數據,你應該真的不使用長寫。取而代之的是使用無響應的寫入方式,因爲吞吐量非常高。 – Emil

+0

Leonardo - 1.'.setMaxBatchSize()'用於設置可以在一個包中發送的最大字節數,'totalPackages'似乎是要發送的包的數量2.'totalPackages'究竟是什麼? 3.通過'設備寫入響應',您可以考慮來自特定特性的通知,或僅從外設得到的確認是否已收到單個包? @Emil - 是否有可能以任何方式與您聯繫? –

+0

當然..是不是特別想談論的東西?我認爲最簡單的就是啓動堆棧溢出聊天。 – Emil

回答

0

您正在撰寫關於特性的響應 - 我假設您的特性呃是UUID=OTA_DATA。長寫由內部小寫(所謂的批)組成。

什麼你可能想實現的是一樣的東西:

fun otaDataWrite(data: ByteArray) { 
    manager.connection!!.setupNotification(OTA_DATA) // first we need to get the notification on to get the response 
      .flatMap { responseNotificationObservable -> // when the notification is ready we create the long write 
       connection.createNewLongWriteBuilder() 
         .setCharacteristicUuid(OTA_DATA) 
         .setBytes(data) 
//      .setMaxBatchSize() // -> if omitted will default to the MTU (20 bytes if MTU was not changed). Should be used only if a single write should be less than MTU in size 
         .setWriteOperationAckStrategy { writeCompletedObservable -> // we need to postpone writing of the next batch of data till we get the response notification 
          Observable.zip(// so we zip the response notification 
            responseNotificationObservable, 
            writeCompletedObservable, // with the acknowledgement of the written batch 
            { _, writeCompletedBoolean -> writeCompletedBoolean } // when both are available the next batch will be written 
          ) 
         } 
         .build() 
      } 
      .take(1) // with this line the notification that was set above will be discarded after the long write will finish 
      .subscribe(
        { byteArray -> 
         Log.i("arrive", "data ${converter.bytesToHex(byteArray)}") 
         manageOtaWrite() 
        }, 
        { it.printStackTrace() } 
      ) 
} 
+0

嗨,對不起,我正在項目的其他部分工作,今天測試你的代碼,但我得到這個錯誤'BleCannotSetCharacteristicNotificationException {bluetoothGattCharacteristic = 984227f3-34fc-4045-a5d0-2c581f81a153,reason = CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR' –

+0

所以顯然你的'OTA_DATA'特性不支持通知,或者它沒有違反藍牙規範(如果支持通知)的「客戶特性配置描述符」。無論哪種方式,您都需要指定外設需要更新的行爲,因爲它現在已經非常值得猜測了。 –

+0

是正確的OTA_DATA特性不支持通知,並且該特性屬於Silicon labs OTA服務,並且我無法控制該服務,是我的外設 –