我要连接的 BLE 设备在其 GATT 特性之一上发出字节,以响应对该特性的写入。客户端应该启用有关该特征的通知,并解释该特征的更改字节。(我正在控制的行为是打开附近无线网络的扫描服务,然后监听服务输出。)
我正在使用 RxAndroidBle 并遵循示例。我有一个活动连接 Observable。我要观察的特征有一个名为AP_SCAN_DATA
. 它应该0xFE
响应接收到书面的0xFF
.
如何调用setupNotification
并在其上设置 Observer 以捕获发出byte[]
的 s,然后将值写入特征,以便我可以捕获响应?
到目前为止我最大的努力:
connectionObservable.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<RxBleConnection>() {
@Override
public void onCompleted() { // ignore...
}
@Override
public void onError(Throwable e) { // ignore...
}
@Override
public void onNext(final RxBleConnection connection) {
Observable.just(connection)
.flatMap(new Func1<RxBleConnection, Observable<Observable<byte[]>>>() {
@Override
public Observable<Observable<byte[]>> call(RxBleConnection connection) {
return connection.setupNotification(AP_SCAN_DATA);
}
})
.doOnNext(new Action1<Observable<byte[]>>() {
@Override
public void call(Observable<byte[]> observable) {
Log.i(TAG, "notification has been set up");
// This code logs on DEBUG that a write was made, but no response ever arrives
connection.writeCharacteristic(AP_SCAN_DATA, CharacteristicValue.RESET.asBytes())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
}
})
.flatMap(new Func1<Observable<byte[]>, Observable<byte[]>>() {
@Override
public Observable<byte[]> call(Observable<byte[]> observable) {
return observable;
}
})
.doOnNext(new Action1<byte[]>() {
@Override
public void call(byte[] bytes) {
Log.i(TAG, "want to read response bytes here, but I don't... " + HexString.bytesToHex(bytes));
}
})
.subscribe();
}
});