RxBluetoothKit
RxBluetoothKit copied to clipboard
iOS reconnection in background/foreground
Hi guys,
Thanks for the library. I have been using it for our app here and the main issue that I am facing sometimes is re-connection. Sometimes I get the error that says you can't observe the connection for this peripheral that you have already observed connection and you need to dispose it
.
So mainly 2 questions here:
- doest the
establishConnection
has timeout by default or not? - if not, can I be 100% sure that it will come back with a result (success/error) ?
cause I am facing that error because I am trying to hit the establish connection in a loop if it doesn't return a result after a specific time.
What is the best approach to do the reconnection in background and foreground in iOS?
this is roughly what I have:
private func reconnect(to peripheral: Peripheral) {
let isConnected = peripheral.isConnected
SDLogger.debug("reconnectToDisconnected(peripherals: [Peripheral]) Start establish connection for \(peripheral.identifier)")
let disposable = peripheral.establishConnection()
.do(onNext: { [weak self] _ in
self?.observeDisconnect(for: peripheral)
self?.observerConnection(for: peripheral)
})
.subscribe(onNext: { (aPeripheral) in
SDLogger.debug("peripheral \(aPeripheral.identifier) isConnected: \(aPeripheral.isConnected)")
self.alreadyObservedPeripherals.remove(peripheral)
if peripheral.isConnected {
if let card = RealmManager.shared.getRealmObject(for: aPeripheral) {
RealmManager.shared.commitRealmIfInWrite()
try? RealmManager.shared.realm?.write {
guard let aCard = RealmManager.shared.realm?.object(ofType: RealmCardPripheral.self, forPrimaryKey: card.uuid) else { fatalError("in updating card is on in reconnectToDis func couldn't find the card in REALM !!!!") }
aCard.isOn = true
RealmManager.shared.realm?.add(aCard, update: true)
MFFirmwareUpdateManager.shared.startChecking(for: aCard)
MFBluetoothCharacterReader.shared.setBatteryNotificationOn(for: (aCard, peripheral), completion: { (_) in })
MFBluetoothCharacterReader.shared.observeFindMonitorParamters(for: (aCard, peripheral))
SDLogger.debug("reconnectToDisconnected(peripherals: [Peripheral]) reconnection Done for \(peripheral.identifier)")
self.reconnectFlow(for: (aCard,peripheral))
}
}
}
}, onError: { (error) in
// print("\(error)")
})
if isConnected {
disposeBag.insert(disposable)
} else {
peripheralConnections[peripheral] = disposable
}
}
func observeDisconnect(for peripheral: Peripheral) {
centralManager.observeDisconnect(for: peripheral).subscribe(onNext: { [unowned self] (peripheral, reason) in
self.disconnect(peripheral)
}, onError: { [unowned self] error in
self.disconnect(peripheral)
SDLogger.error("observer disconnect failed for \(peripheral.identifier)")
}).disposed(by: disposeBag)
}
public func disconnect(_ peripheral: Peripheral) {
self.peripheralConnections[peripheral]?.dispose()
self.peripheralConnections[peripheral] = nil
self.reconnect(to: peripheral)
self.disconnectionSubject.onNext(MFResult.success(peripheral))
}
any update on this?