0

我正在对 BLE 进行长时间写入以进行 OTA 更新,但我需要等待 BLE 设备的写入响应以发送更多数据,但我不知道如何捕获设备写入响应,我是使用带有 android 7 的三星 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() })

每次我编写特征时,订阅都会立即用写入的数据响应我,我需要捕获特征的响应,以发送更多数据

4

2 回答 2

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() }
            )
}
于 2017-08-03T15:15:17.610 回答
-1

嗯,经过大量的测试,我终于用android BLE API开发了一个用于OTA更新的独立类,并将它与我所有的RxBle方法一起使用,我不知道我是硬件问题还是其他问题,但我解决了这个问题,非常感谢。

于 2018-05-31T13:08:40.773 回答