我正在将 RxAndroidBle 用于一个项目,我需要从 ble 设备上读取一些特征,然后将它们显示在屏幕上。下面的代码工作除了this::displayCharacteristics
private void readCharacteristics() {
mConnectionObservable.flatMap(rxBleConnection -> Observable.combineLatest(
rxBleConnection.readCharacteristic(UUID_SERIAL_NUMBER),
rxBleConnection.readCharacteristic(UUID_MACHINE_TYPE),
rxBleConnection.readCharacteristic(UUID_CHARACTERISTIC),
ConnectedViewModel::new
).doOnError(this::logError)
).doOnError(this::logError)
.subscribe(this::displayCharacteristics, this::logError);
}
private void displayCharacteristics(ConnectedViewModel model) {
mSerialNumber.setText(model.getSerialNumber());
mMachineType.setText(model.getMachineType());
mCableLength.setText(model.getCableLength());
mCableSize.setText(model.getCableSize());
mUnits.setText(model.getUnits());
}
这是因为CalledFromWrongThreadException
执行this::displayCharacteristics
. 在片段所在的线程上显示数据的正确方法是什么?我必须使用监听器吗?谢谢!