我正在使用带有RxJava2的RxAndroidBle库来读取BLE特征。我认为这个问题只是一个RxJava问题,但包括我正在使用RxAndroidBle
的详细信息,以防万一。
我获得了连接,然后使用它来调用readCharacteristic()
,它本身返回Single<ByteArray>
。在这一点上,我不仅仅想获得一个ByteArray
。我需要多次读取这个特性,因为BLE设备设置为让我得到一个小文件,并且特征一次只能发回20个字节,因此我需要重复读取。
是否可以修改此代码,以便switchMap()
下面的Observable
返回ByteArrays
,而val connection: Observable<RxBleConnection> = selectedDevice.record.bleDevice.establishConnection(false, Timeout(30, TimeUnit.SECONDS))
return connection
.subscribeOn(Schedulers.io())
.switchMap {
// I want to get an Observable that can read multiple times here.
it.readCharacteristic(serverCertCharacteristicUUID).toObservable()
}
.doOnNext {
Timber.e("Got Certificate bytes")
}
.map {
String(it as ByteArray)
}
.doOnNext {
Timber.e("Got certificate: $it")
}
.singleOrError()
会发出多个 <android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:clipChildren="true"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="16dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.gms.maps.StreetViewPanoramaView
android:id="@+id/street_view"
android:layout_width="match_parent"
android:layout_height="150dp" />
[...]
,而不只是单个?{/ p>
我是RxJava的新手。
{{1}}
答案 0 :(得分:1)
要多次重复读取,直到发出特定值,需要更改此部分:
// I want to get an Observable that can read multiple times here.
it.readCharacteristic(serverCertCharacteristicUUID).toObservable()
类似于suggested by the RxJava author in the first answer that google gives for phrase rxjava single repeat
:
// this will repeat until a `checkRepeatIf` returns false
Observable.defer {
val successValue = AtomicReference<ByteArray>()
connection.readCharacteristic(serverCertCharacteristicUUID)
.doOnSuccess { successValue.lazySet(it) }
.repeatWhen { completes -> completes.takeWhile { checkRepeatIf(successValue.get()) } }
}
答案 1 :(得分:0)
您可以使用通知来缓冲数据。
device.establishConnection(false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid))
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(
bytes -> {
// Given characteristic has been changes, here is the value.
},
throwable -> {
// Handle an error here.
}
);
答案 2 :(得分:0)
我能够通过发送信号来停止def index(request):
user = request.user
print(user)
和蓝牙特性的读取来实现这一点。值得注意的是,您需要在connectionObservable
之后拨打toObservable()
或者这不起作用,尽管我不知道原因。
repeat()