关于android / BLE空间,我很新。我正在尝试写特征并使用RxAndroidBle库监听通知。我看过许多实现方案,它们看起来似乎相差甚远,以致无法使我的方案正常工作。这篇文章:No notification data in Write/Notification set-up几乎是我所需要的,但是当我使用发布的代码时,我无法捕获外围设备上的任何数据,由于我没有发回邮件,因此该外围设备也不会返回响应一个答复。当我切换到长写生成器时,可以写外围设备并看到数据流入,但是从外围设备返回响应后,通知订阅中没有捕获任何内容。
这是我的代码:
private void writeNotify(RxBleDevice peripheral, byte[] payload) {
Disposable connectionSubscription = peripheral.establishConnection(false)
.flatMap( // when the connection is available...
rxBleConnection -> rxBleConnection.setupNotification(UUID.fromString(notifyCharacteristic)), // ... setup the notification...
(rxBleConnection, notifyObservable) -> Observable.combineLatest( // ... when the notification is setup...
rxBleConnection.createNewLongWriteBuilder().setBytes(payload).setCharacteristicUuid(UUID.fromString(writecharateristic)).setMaxBatchSize(20).build(), // ... write the characteristic...
//rxBleConnection.writeCharacteristic(UUID.fromString(writecharateristic), payload).toObservable(), // ... write the characteristic...
notifyObservable, // ... and observe for the first notification on the AP_SCAN_DATA
(writtenBytes, responseBytes) -> responseBytes
)
)
.flatMap(observable -> observable) // ... flatMap the result as it is Observable<byte[]>...// ... and finish after first response is received to cleanup notifications
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
responseBytes -> {
String data = new String(responseBytes);
},
throwable -> {
}
);
}
另外,我尝试将其处理为2个操作,在对外围设备进行长时间写入之后,我设置了通知特征。我在我的外围设备上订阅了watchConnectionStateChanges方法的位置,并等待断开状态触发后再设置我的通知特征。在这种情况下,我实际上能够接收部分响应,因此我知道通知特征正在起作用。我只是没有正确实现此目的。有什么建议吗?