首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rxAndroidBle获取长写入响应

rxAndroidBle获取长写入响应
EN

Stack Overflow用户
提问于 2017-08-03 05:28:06
回答 2查看 929关注 0票数 0

我正在写一个很长的BLE来进行OTA更新,但我需要等待BLE设备的写响应来发送更多的数据,但我不知道如何捕捉设备写响应,我使用的是三星galaxy tab s2和安卓7,我的代码是Kotlin

代码语言:javascript
复制
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() })

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-31 21:08:41

经过大量的测试,我终于用android BLE API为OTA更新开发了一个独立的类,我将它和我所有的RxBle方法一起使用,我不知道我是有硬件问题还是别的什么,但我解决了这个问题,谢谢。

票数 -1
EN

Stack Overflow用户

发布于 2017-08-03 23:15:18

你写的是来自特征的响应-我假设你提到的特征是带有UUID=OTA_DATA的特征。长写入由内部的小写入(所谓的批处理)组成。

您可能想要实现的内容如下:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45471294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档