【问题标题】:Extend Bluetooth connection timeout only when connection was successful using RxBluetoothKit仅在使用 RxBluetoothKit 连接成功时延长蓝牙连接超时
【发布时间】:2019-06-15 13:48:20
【问题描述】:

我想做的是:

  1. 将值写入特征
  2. 如果在一定时间内连接失败,则作为超时错误处理
  3. 如果连接成功,延长或忽略超时并保持连接

我实现了 1 和 2,但我如何实现 3? 非常感谢您的帮助。

我的来源:

manager = CentralManager(queue: .main, options: options)
manager!.observeState()
    .startWith(self.manager!.state)
    .filter { $0 == .poweredOn }
    .timeout(3.0, scheduler: MainScheduler.instance)
    .take(1)
    .flatMap { _ in self.manager!.retrievePeripherals(withIdentifiers: [peripheralUUID])[0].establishConnection() }
    .timeout(5.0, scheduler: MainScheduler.instance) // (A) Set connection timeout here
    .flatMap{ $0.writeValue(data, for: BLECharacteristic.char, type: .withResponse)}
    .subscribe(onNext: { char in
        // (B) I want to extend timeout here
        // Handle success
    }, onError: { (error) in
        // Handle error
    }, onCompleted: nil, onDisposed: nil)

【问题讨论】:

标签: ios swift bluetooth rx-swift


【解决方案1】:

您希望连接超时的可观察对象,而不是对整个链施加超时

// ...
.take(1)
.flatMap { _ in 
  self.manager!.retrievePeripherals(withIdentifiers: [peripheralUUID])[0]
    .establishConnection()
    .timeout(5.0, scheduler: MainScheduler.instance)
    .take(1)
}
.flatMap{ $0.writeValue(data, for: BLECharacteristic.char, type: .withResponse) }
// ...

添加.take(1) 确保可观察对象在建立连接后完成(尽管在理想情况下,establishConnection() 应该是造成这种行为的人)。

【讨论】:

  • 非常感谢你,tomahh!我在 .establishConnection() 之后删除了 .take(1) ,以便在连接建立后写入值。效果和我预期的一样!!
猜你喜欢
  • 2011-10-14
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
相关资源
最近更新 更多