【问题标题】:rxAndroidBle get long write responserxAndroidBle 得到长写响应
【发布时间】:2018-01-10 07:06:47
【问题描述】:

我正在对 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() })

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

【问题讨论】:

  • 不是你问题的真正答案,但如果你需要快速写入大量数据,你真的不应该使用长写。而是使用无响应写入,因为这样的吞吐量要大得多。
  • Leonardo — 1. .setMaxBatchSize() 用于设置一个包中可以发送的最大字节数,totalPackages 似乎是要发送的包数 2. @ 到底是什么987654324@? 3. 通过device write response,您会想到来自特定特征的通知,还是只是来自外围设备的确认它已收到单个包? @Emil — 可以以任何方式与您联系吗?
  • 当然.. 有什么特别想谈的吗?我认为最简单的方法就是开始一个堆栈溢出聊天。
  • @DariuszSeweryn 是的,我注意到我在 setMaxBatchSize() 方法中的错误现在我将 data.size 传递给该方法,2 totalPackages 是我需要发送的包的总数(我正在发送一个文件通过 BLE 进行 OTA 更新),3 是的,我需要外围设备确认它已收到包,在此基础上我将发送另一个包
  • @Emil 我知道,但它是 OTA 更新的 DFU 特性,我这样做 IOS 和每次写入的特性响应

标签: android bluetooth-lowenergy kotlin ota rxandroidble


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您正在写关于特征的响应——我假设您所指的特征是带有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() }
                )
    }
    

    【讨论】:

    • 您好,抱歉耽搁了,我在项目的其他部分工作,今天测试您的代码,但我收到此错误BleCannotSetCharacteristicNotificationException{bluetoothGattCharacteristic=984227f3-34fc-4045-a5d0-2c581f81a153, reason=CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR
    • 显然你的OTA_DATA 特性要么不支持通知,要么没有Client Characteristic Config Descriptor,这会违反蓝牙规范(如果它支持通知)。无论哪种方式,您都需要指定外围设备需要更新的行为,因为它现在几乎是在猜测。
    • 是正确的,OTA_DATA 特性不支持通知,并且该特性属于 Silicon labs OTA 服务,我无法控制该服务,是我外围设备中的默认服务
    • OTA 进程的行为是,我有一个名为 OTA 服务的服务,只有 1 个特性 OTA_CONTROl,我将 0x00 写入该特性,设备在 DFU 模式下重新启动,并向我显示 OTA 服务 2 OTA_CONTROL(write) 和 OTA_DATA(Write with response) 特性,我将 0x00 写入 OTA_CONTROL,然后我用 longWriter 将二进制文件的一部分写入 OTA_DATA,问题是我需要等待 writer 的响应(如确认)发送文件的下一部分
    • 不确定您是否在第 5.4 点中描述的版本中使用 OTA OTA_UPLOAD:silabs.com/documents/login/application-notes/… 但如果是这种情况,那么我不确定为什么您的初始代码无法正常工作从外围端没有ACK
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多