我想做的是:
我认识到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)
答案 0 :(得分:0)
您希望观察到的超时连接,而不是在整个链上施加超时。
// ...
.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()
应该是造成这种行为的原因)。