rxAndroidBle获得长时间的写入响应
我正在长时间写一个BLE进行OTA更新,但是我需要等待BLE设备的写入响应来发送更多的数据,但是我不知道如何捕获设备写入响应,我使用Android 7的三星galaxy标签s2和Kotlin代码
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() })
每次我编写特性的订阅都会立即用写入的数据来响应我,我需要捕获特性的响应,为了发送更多的数据
你正在写关于这个特性的响应 – 我假设你引用的特性是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() } ) }