订阅多个特征时Rxandroid崩溃
我目前正在为一款专有的蓝牙低能设备开发一款Android应用程序 我决定使用RxAndroidBle,与内置蓝牙协议栈相比,RxAndroidBle的使用相对简单,我对此感到满意 我遇到的问题是:我需要订阅两个特性,并不断读取和聚合它们的值 查看此示例页面,我能够根据示例读取多个特征,但在组合多个订阅时失败 这就是我得到的:订阅多个特征时Rxandroid崩溃,android,bluetooth,rxandroidble,Android,Bluetooth,Rxandroidble,我目前正在为一款专有的蓝牙低能设备开发一款Android应用程序 我决定使用RxAndroidBle,与内置蓝牙协议栈相比,RxAndroidBle的使用相对简单,我对此感到满意 我遇到的问题是:我需要订阅两个特性,并不断读取和聚合它们的值 查看此示例页面,我能够根据示例读取多个特征,但在组合多个订阅时失败 这就是我得到的: subscriptionA = device.establishConnection(this, false) .flatMap(rxBle
subscriptionA = device.establishConnection(this, false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(aUUID))
.doOnNext(notificationObservable -> {
// Notification has been set up
})
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(bytes -> {
// Given characteristic has been changes, here is the value.
System.out.printf("Received 03: %d\n\tdata: %s\n", bytes.length, Arrays.toString(bytes));
});
subscriptionB = device.establishConnection(this, false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(bUUID))
.doOnNext(notificationObservable -> {
// Notification has been set up
})
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(bytes -> {
// Given characteristic has been changes, here is the value.
System.out.printf("Received 05: %d\n\tdata: %s\n", bytes.length, Arrays.toString(bytes));
});
你犯了两个错误:
由:BleAlreadyConnectedException{macAddress=35:32:30:30:44:53}
引起)。BLE连接是点对点类型,不能同时打开同一设备的两个连接(与HTTP不同)rx.exceptions.OnErrorNotImplementedException
) subscription = device.establishConnection(this, false)
.flatMap(rxBleConnection -> Observable.combineLatest( // use the same connection and combine latest emissions
rxBleConnection.setupNotification(aUUID).<byte[]>flatMap(observable -> observable), // sometimes IDE get's lost in what type is returned from an Observable - that's why I added <byte[]>
rxBleConnection.setupNotification(bUUID).<byte[]>flatMap(observable -> observable),
Pair::new // merge into a Pair
))
.subscribe(
byteArrayPair -> {
// here you get the latest values from notifications
byte[] aBytes = byteArrayPair.first;
byte[] bBytes = byteArrayPair.second;
// do your thing
},
throwable -> {
// handle errors
}
);
subscription=device.buildConnection(此,false)
.flatMap(rxbeconnection->Observable.combinelatetest(//使用相同的连接并合并最新的排放
setupNotification(aUUID).flatMap(observable->observable),//有时IDE get会丢失在从observable返回的类型中,这就是我添加
rxbeconnection.setupNotification(bUUID).flatMap(可观察->可观察),
Pair::new//合并成一对
))
.订阅(
byteArrayPair->{
//在这里,您可以从通知中获得最新的值
字节[]aBytes=byteArrayPair.first;
字节[]bBytes=byteArrayPair.second;
//做你的事
},
可丢弃->{
//处理错误
}
);
向您致意您犯了两个错误:
由:BleAlreadyConnectedException{macAddress=35:32:30:30:44:53}
引起)。BLE连接是点对点类型,不能同时打开同一设备的两个连接(与HTTP不同)rx.exceptions.OnErrorNotImplementedException
) subscription = device.establishConnection(this, false)
.flatMap(rxBleConnection -> Observable.combineLatest( // use the same connection and combine latest emissions
rxBleConnection.setupNotification(aUUID).<byte[]>flatMap(observable -> observable), // sometimes IDE get's lost in what type is returned from an Observable - that's why I added <byte[]>
rxBleConnection.setupNotification(bUUID).<byte[]>flatMap(observable -> observable),
Pair::new // merge into a Pair
))
.subscribe(
byteArrayPair -> {
// here you get the latest values from notifications
byte[] aBytes = byteArrayPair.first;
byte[] bBytes = byteArrayPair.second;
// do your thing
},
throwable -> {
// handle errors
}
);
subscription=device.buildConnection(此,false)
.flatMap(rxbeconnection->Observable.combinelatetest(//使用相同的连接并合并最新的排放
setupNotification(aUUID).flatMap(observable->observable),//有时IDE get会丢失在从observable返回的类型中,这就是我添加
rxbeconnection.setupNotification(bUUID).flatMap(可观察->可观察),
Pair::new//合并成一对
))
.订阅(
byteArrayPair->{
//在这里,您可以从通知中获得最新的值
字节[]aBytes=byteArrayPair.first;
字节[]bBytes=byteArrayPair.second;
//做你的事
},
可丢弃->{
//处理错误
}
);
致以最诚挚的问候我最初尝试过这样做,但没有成功。这样好多了。非常感谢!我明天会试试,让你知道它是怎么做的。我一开始试过这样做,但没有成功。这样好多了。非常感谢!我明天会试试,让你知道进展如何