RxAndroidBle:设置通知,写特征并等待通知继续 [英] RxAndroidBle: Setup notification, write on characteristic and wait for notification to proceed

查看:243
本文介绍了RxAndroidBle:设置通知,写特征并等待通知继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Polidea的RxAndroidBle库与我的Android应用程序中的设备进行通信.

我对响应式编程非常陌生,因此我无法确切地知道如何执行以下操作:

  1. 具有一个特征(特征A)的设置通知.
  2. 完成通知设置后,写入另一个特征(特征B).这将触发来自特征A的通知.
  3. 完成写操作后,等待通知到达特性A.
  4. 在应用程序的不同部分重复相同的步骤(1至3)多次.

我已经看到了这个相关的答案,但这是使用库的第一个版本完成的,我无法弄清楚如何使用新版本.

谢谢.

解决方案

我结束了自己的研究.这是一种在特征中设置指示或通知,然后将一些字节写入另一个特征,然后返回Observable<String>的方法,该Observable<String>发出解析为十六进制String的,在通知/指示上读取的byte[]./p>

希望它可以帮助其他人在RxJava2中寻找此解决方案.

private Observable<String> writeAndReadOnNotification(UUID writeTo, UUID readOn,
                                                      String hexString,
                                                      boolean isIndication,
                                                      RxBleConnection rxBleConnection) {
    Observable<Observable<byte[]>> notifObservable =
            isIndication ?
                    rxBleConnection.setupIndication(readOn) :
                    rxBleConnection.setupNotification(readOn);
    return notifObservable.flatMap(
            (notificationObservable) -> Observable.combineLatest(
                    rxBleConnection.writeCharacteristic(writeTo, hexToBytes(hexString)).toObservable(),
                    notificationObservable.take(1),
                    (writtenBytes, responseBytes) -> bytesToHex(responseBytes)
            )
    ).take(1)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(this::throwException);
}

I am using Polidea's RxAndroidBle library to communicate with a Device in my Android application.

I am very new to Reactive Programming so I can't figure out exactly how to do the following:

  1. Setup Notification in one characteristic (Characteristic A).
  2. When notification setup is done, write to another characteristic (Characteristic B). This will trigger a Notification coming from Characteristic A.
  3. When the write operation is done, wait for the arrival of the Notification in Characteristic A.
  4. Repeat the same steps (1 to 3) many times in different parts of the application.

I have seen this related answer, but it is done using the first version of the library and I can't figure out how to do it using the new version.

Thanks.

解决方案

I ended figuring it out by myself. Here is a method that setup the indication or notification in a characteristic, then writes some bytes to another characteristic and returns an Observable<String> that emits the byte[] parsed to a hex String that were read on the notification/indication.

Hope it helps someone else looking for this solution in RxJava2.

private Observable<String> writeAndReadOnNotification(UUID writeTo, UUID readOn,
                                                      String hexString,
                                                      boolean isIndication,
                                                      RxBleConnection rxBleConnection) {
    Observable<Observable<byte[]>> notifObservable =
            isIndication ?
                    rxBleConnection.setupIndication(readOn) :
                    rxBleConnection.setupNotification(readOn);
    return notifObservable.flatMap(
            (notificationObservable) -> Observable.combineLatest(
                    rxBleConnection.writeCharacteristic(writeTo, hexToBytes(hexString)).toObservable(),
                    notificationObservable.take(1),
                    (writtenBytes, responseBytes) -> bytesToHex(responseBytes)
            )
    ).take(1)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(this::throwException);
}

这篇关于RxAndroidBle:设置通知,写特征并等待通知继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆