gpt4 book ai didi

swift - 在 RxBluetoothKit 中链接多个命令

转载 作者:行者123 更新时间:2023-11-30 11:20:10 25 4
gpt4 key购买 nike

之前我有过关于很棒的 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 )

}
}

非常感谢任何指点!谢谢

最佳答案

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")
}
}

关于swift - 在 RxBluetoothKit 中链接多个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51344397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com