我正在写一个很长的BLE来进行OTA更新,但我需要等待BLE设备的写响应来发送更多的数据,但我不知道如何捕捉设备写响应,我使用的是三星galaxy tab s2和安卓7,我的代码是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() })每次我写入特征时,订阅都会立即用写入的数据响应我,我需要捕获特征的响应,以便发送更多数据
发布于 2018-05-31 21:08:41
经过大量的测试,我终于用android BLE API为OTA更新开发了一个独立的类,我将它和我所有的RxBle方法一起使用,我不知道我是有硬件问题还是别的什么,但我解决了这个问题,谢谢。
发布于 2017-08-03 23:15:18
你写的是来自特征的响应-我假设你提到的特征是带有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() }
)
}https://stackoverflow.com/questions/45471294
复制相似问题