0

我是 Rx 的新手,仍在试图弄清楚如何正确处理 observables。我想知道是否有更好的方法来编写多个特性而不是使用 RxAndroidBle 一次编写一个特性?目前我正在使用下面的代码一次做一个。

Observable<RxBleConnection> mConnectionObservable;

private void saveChanges(String serialNumber, Date date, MachineTypeEnum machineType, MachineConfig machineConfig) {
    mWriteSubscription = mConnectionObservable
            .flatMap(rxBleConnection -> Observable.merge(
                    getWrites(rxBleConnection, serialNumber, machineType, machineConfig, date)
            ))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(bytes -> {
                Toast.makeText(getContext(), "Saved Changes", Toast.LENGTH_SHORT).show();
                ((MainActivity)getActivity()).back();
            }, BleUtil::logError);
}

private Iterable<? extends Observable<? extends byte[]>> getWrites(RxBleConnection rxBleConnection,
                                                                   String serialNumber,
                                                                   MachineTypeEnum machineType,
                                                                   MachineConfig machineConfig,
                                                                   Date date) {
    List<Observable<byte[]>> observables = new ArrayList<>();


    observables.add(rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.DrainCleaner.Characteristics.UUID_WRITE_SERIAL_NUMBER,
            Utf8Util.nullPad(serialNumber, 16).getBytes()).doOnError(throwable -> Log.e("Write", "serial failed", throwable)));

    observables.add(rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.DrainCleaner.Characteristics.UUID_MACHINE_TYPE,
            new byte[]{(byte) machineType.val()}).doOnError(throwable -> Log.e("Write", "machine type failed", throwable)));

    observables.add(rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.DrainCleaner.Characteristics.UUID_CHARACTERISTIC,
            MachineConfigBitLogic.toBytes(machineConfig)).doOnError(throwable -> Log.e("Write", "machine config failed", throwable)));

    observables.add(rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.CurrentTime.Characteristics.UUID_CURRENT_TIME,
            TimeBitLogic.bytesFor(date)).doOnError(throwable -> Log.e("Write", "date failed", throwable)));

    return observables;
}

因此,我将旧代码更改为上面现在使用合并的代码,但现在似乎只有一个特征更新了。

4

2 回答 2

2

我会使用合并运算符:

mConnectionObservable
        .flatMap(rxBleConnection -> 
        Observable.merge(
        rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.DeviceInformation.Characteristics.UUID_SERIAL_NUMBER,
            Utf8Util.nullPad(serialNumber, 16).getBytes()
        ),
        rxBleConnection.writeCharacteristic(
            Constants.Bluetooth.Services.DrainCleaner.Characteristics.UUID_MACHINE_TYPE,
            new byte[]{(byte) machineType.val()}
        ))
        .subscribe(bytes -> {/* do something*/}, BleUtil::logError);

您也可以将可观察的列表传递给该运算符:

除了将多个 Observables(最多 9 个)传递给 merge,您还可以传入 Observables 的 List<>(或其他 Iterable)、Observables 数组,甚至是发出 Observables 的 Observable,然后 merge 会将它们的输出合并到单个 Observable 的输出

于 2017-09-25T18:43:29.767 回答
1

RxAndroidBle库在后台序列化任何 BLE 请求,因为 Android 上的 BLE 实现大部分是同步的(尽管 Android vanilla API 表明它不是)。

尽管您需要了解合并运算符的作用,但合并写入是一种好方法:

 * You can combine the items emitted by multiple Observables so that they appear as a single Observable, by
 * using the {@code merge} method.

因此,我将旧代码更改为上面现在使用合并的代码,但现在似乎只有一个特征更新了。

这种行为的原因可能是您使用流的方式:

        .subscribe(bytes -> {
            Toast.makeText(getContext(), "Saved Changes", Toast.LENGTH_SHORT).show();
            ((MainActivity)getActivity()).back();
        }, BleUtil::logError);

每当bytes发出Activity.back(). .merge()运算符会bytes为每个执行的写入命令发出。如果您取消订阅Subscriptionin ie,.onPause()那么它将在第一次写入完成后立即取消订阅。您可以让您的流程等到所有写入完成,如下所示:

private void saveChanges(String serialNumber, Date date, MachineTypeEnum machineType, MachineConfig machineConfig) {
    mWriteSubscription = mConnectionObservable
        .flatMap(rxBleConnection -> 
            Observable.merge(
                getWrites(rxBleConnection, serialNumber, machineType, machineConfig, date)
            )
            .toCompletable() // we are only interested in the merged writes completion
            .andThen(Observable.just(new byte[0])) // once the merged writes complete we send a single value that will be reacted upon (ignored) in .subscribe()
        )
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(ignored -> {
            Toast.makeText(getContext(), "Saved Changes", Toast.LENGTH_SHORT).show();
            ((MainActivity)getActivity()).back();
        }, BleUtil::logError);
}
于 2017-09-26T10:50:33.023 回答