我正在执行对 BLE 特性的写入,其中对于这些特定设备,如果外围设备仍处于锁定状态,则某些特性是不可写入的。
如果在设备仍处于锁定状态时调用它,我只想提供一个视觉通知,而不是抛出异常。
这是代码的第一部分,它来自我为此应用程序编写的自定义库:
public Observable<byte[]> setInterval(int interval) {
if (!(interval > 0)) {
return Observable.error(new IllegalArgumentException("Interval must be greater than 0."));
}
byte[] bytes = Utils.formatIntToBytes(interval);
return mConnection.writeCharacteristic(Constants.INTERVAL, bytes)
.doOnError(throwable -> Log.d(TAG, "call: Error writing to the interval characteristic"))
.onErrorReturn(throwable -> {
return new byte[0];
});
}
这是使用前一个的方法:
private void setInterval(int interval) {
mDevice.setInterval(interval)
.onErrorResumeNext(throwable -> {
return Observable.empty();
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<byte[]>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(byte[] bytes) {
if (bytes != null) {
Toast.makeText(DeviceDetailActivity.this, Utils.formatResponse(bytes), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(DeviceDetailActivity.this, "Seems to of been an error. Ensure the device is unlocked.", Toast.LENGTH_SHORT).show();
}
}
});
}
这以前只是一个Action1
,但我改为aSubscriber
以清楚地显示onError()
已实施。
我添加了一堆额外的运算符 ( onErrorReturn()
, onErrorResumeNext()
) 以防止出现异常。
为什么异常仍然通过?
编辑:堆栈跟踪:
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:112)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: rx.exceptions.OnErrorNotImplementedException
at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386)
at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383)
at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:152)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:115)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: BleGattException{status=8, bleGattOperation=BleGattOperation{description='CHARACTERISTIC_WRITE'}}
at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.propagateStatusErrorIfGattErrorOccurred(RxBleGattCallback.java:245)
at com.polidea.rxandroidble.internal.connection.RxBleGattCallback.access$100(RxBleGattCallback.java:26)
at com.polidea.rxandroidble.internal.connection.RxBleGattCallback$1.onCharacteristicWrite(RxBleGattCallback.java:110)
at android.bluetooth.BluetoothGatt$1.onCharacteristicWrite(BluetoothGatt.java:407)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:279)
at android.os.Binder.execTransact(Binder.java:453)