尝试使用 Observable 设置对 RxAndroidBle 连接状态的监控。下面的代码编译(我还不能测试它),但我不完全明白为什么。调用的第二个参数subscribe
应该是Action1<java.lang.Throwable> onError
. 我是否正确实施了这一点?为什么我不能写
throwable -> throw throwable
当我尝试时,第二个“throwable”被标记为“无法解析符号'throwable'”,并且在“->”和“throw”之间它表示它需要一个右括号、左括号或分号。
// set up monitoring of connection state with a subscription
boolean setConnectionStateNotification() {
asBleDevice.observeConnectionStateChanges()
.subscribe(
connectionState -> asBleConnectionState = connectionState,
throwable -> new RuntimeException( "Problem with subscription to Connection State Changes: "
+ throwable.getMessage() )
);
return true;
}
TBH 我很难理解 ; 的概念Action1<Throwable>
。有人可以解释一下吗?
更新:我想我可能已经弄清楚了。像这样:
boolean setConnectionStateNotification() {
asBleDevice.observeConnectionStateChanges() // returns Observable<RxBleConnection.RxBleConnectionState>
.subscribe(
connectionState -> asBleConnectionState = connectionState,
throwable -> { throw new RuntimeException(
"Problem with subscription to Connection State Changes: "
+ throwable.getMessage(), throwable );
},
( ) -> RxBleLog.d( "Connection State Observable has completed", null ) // onCompleted() with no arguments
); // subscribe
return true;
}
(我还为 onCompleted() 调用添加了第三个可选处理程序。)