在RxBluetoothKit中链接多个命令

问题描述 投票:0回答:1

以前我有关于令人敬畏的rxAndroidBle - Writing multiple commands to characteristic的这个问题

解决方案效果很好!

现在是将这个应用程序移植到iOS版本的时候了,我正在努力找到一种合适的方法来实现相同的结果。

基本上我需要按顺序向外设发送一系列命令,命令需要按顺序发送,下一个命令应该在前一个完成时发送,理想情况下发送所有命令后的最终事件,就像在上面链接中的android应用程序代码段

下面的代码完成了这项工作,但是当你看到它不漂亮时,随着命令数量的增加,也很容易变得无法管理!

Android应用程序使用Single.concat,RxSwift的等价物是什么?

self.writeCharacteristic?.writeValue(command1, type: .withResponse)
        .subscribe {
            print("Command 1 complete ", $0 )
            self.writeCharacteristic?.writeValue(command2, type: .withResponse)
                .subscribe {
                    print("command2 complete ", $0 )

            }
    }

任何指针非常感谢!!!谢谢

swift rx-swift rxandroidble rxbluetooth
1个回答
0
投票

Single没有concat方法,但你可以只为我们Observable.concat并为每个asObservable()方法调用writeValue,如下所示:

Observable.concat(
        characteristic.writeValue(command1, type: .withResponse).asObservable(),
        characteristic.writeValue(command2, type: .withResponse).asObservable(),
        characteristic.writeValue(command3, type: .withResponse).asObservable(),
        ...
        characteristic.writeValue(command4, type: .withResponse).asObservable()
        ).subscribe { event in
            switch event {
            case .next(let characteristic):
                print("Did write for characteristic \(characteristic)")
            case .error(let error):
                print("Did fail with error \(error)")
            case .completed:
                print("Did completed")
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.