【问题标题】:RxAndroidBle: Setup notification, write on characteristic and wait for notification to proceedRxAndroidBle:设置通知,写入特征并等待通知继续
【发布时间】:2018-11-20 14:22:33
【问题描述】:

我正在使用 Polidea's RxAndroidBle library 与我的 Android 应用程序中的设备进行通信。

我对响应式编程非常陌生,所以我无法确切地弄清楚如何执行以下操作:

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

我见过this related answer,但它是使用库的第一个版本完成的,我不知道如何使用新版本。

谢谢。

【问题讨论】:

    标签: android bluetooth-lowenergy rx-java2 rx-android rxandroidble


    【解决方案1】:

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

    希望它可以帮助其他人在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多