我们有一个场景,我们关闭BLE Enabled设备然后Android App屏幕会变灰。但是当关闭BLE Enabled设备时, 而不是灰显,屏幕仍然冻结,UI不响应
执行Observable后,调用订阅者中的onNext(),onCompleted()和onError()等方法都没有。
以下是订阅者代码:
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);
}
以下是将从中调用订户的Observable代码:
在下面的代码中,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);
}
}
答案 0 :(得分:0)
您需要让您的订阅者订阅主题。 像 -
这样的东西serializedSubscriber.add(connectionStateSubject.subscribe())