0
我們有一個場景,在這裏我們關閉啓用BLE的設備,然後Android應用程序屏幕變灰。但是,當關閉啓用BLE的設備 而不是灰色顯示時,屏幕將保持凍結狀態,並且用戶界面不會響應Observer發出的數據不會觸發Rx中的訂閱者Java
用戶中的onNext(),onCompleted()和onError()在執行Observable之後。
下面是認購代碼:
private void subscribeToConnEvents() {
Subscriber<ConnService.ConnEvent> subscriber = new Subscriber<ConnService.ConnEvent>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ConnService.ConnEvent connEvent) {
}
};
SerializedSubscriber<ConnService.ConnEvent> serializedSubscriber = new SerializedSubscriber<>(subscriber);
}
下面是可觀察的代碼從該用戶將被稱爲:在connectionStateSubject得到的構造
private SerializedSubject<ConnEvent, ConnEvent> connectionStateSubject;
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
BluetoothDevice device = gatt.getDevice();
String address = gatt.getDevice().getAddress();
ConnectionEvent event = new ConnectionEvent();
event.deviceAddress = address;
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
event.status = ConnectionStatus.STATE_DISCONNECTED;
connectionStateSubject.onNext(event);
}
}
初始化
在下面的代碼