RxAndroidBle:设置通知,写特征并等待通知继续 [英] RxAndroidBle: Setup notification, write on characteristic and wait for notification to proceed
问题描述
我正在使用 Polidea的RxAndroidBle库与我的Android应用程序中的设备进行通信.
我对响应式编程非常陌生,因此我无法确切地知道如何执行以下操作:
- 具有一个特征(特征A)的设置通知.
- 完成通知设置后,写入另一个特征(特征B).这将触发来自特征A的通知.
- 完成写操作后,等待通知到达特性A.
- 在应用程序的不同部分重复相同的步骤(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:
- Setup Notification in one characteristic (Characteristic A).
- When notification setup is done, write to another characteristic (Characteristic B). This will trigger a Notification coming from Characteristic A.
- When the write operation is done, wait for the arrival of the Notification in Characteristic A.
- 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屋!