rxAndroidBle 获得长写响应
rxAndroidBle get long write response
我正在长时间写入 BLE 以进行 OTA 更新,但我需要等待 BLE 设备的写入响应以发送更多数据,但我不知道如何捕获设备写入回复,我正在使用 android 7 的 Samsung galaxy tab 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() }
)
}
好吧,经过大量测试,我终于开发了一个独立的 class 用于 OTA 更新 android BLE API,我将它与我所有的 RxBle 一起使用方法,不知道是我硬件问题还是别的什么问题,解决了,非常感谢
我正在长时间写入 BLE 以进行 OTA 更新,但我需要等待 BLE 设备的写入响应以发送更多数据,但我不知道如何捕获设备写入回复,我正在使用 android 7 的 Samsung galaxy tab 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() }
)
}
好吧,经过大量测试,我终于开发了一个独立的 class 用于 OTA 更新 android BLE API,我将它与我所有的 RxBle 一起使用方法,不知道是我硬件问题还是别的什么问题,解决了,非常感谢