Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnect Connection When device lost connection #405

Open
andrewehab opened this issue Jan 8, 2022 · 0 comments
Open

Disconnect Connection When device lost connection #405

andrewehab opened this issue Jan 8, 2022 · 0 comments

Comments

@andrewehab
Copy link

When terminated the app the connection is still restored but when the device lose connection it can't reconnect again

class BluetoothManager {
//MARK: - Singelton
static let shared = BluetoothManager()
//MARK: - Propertise
var centralManager: CentralManager!

//MARK: - Configure Central Manager
func configureCentralManager() {
    //        let options = [CBCentralManagerOptionRestoreIdentifierKey: "NOUR", CBCentralManagerOptionShowPowerAlertKey: true] as [String: AnyObject]
    
    centralManager = CentralManager(
        options: [CBCentralManagerOptionRestoreIdentifierKey: NSString("NOUR")],
        onWillRestoreCentralManagerState: { (restoreState) in
            print("RESTORED")
            print(restoreState.centralManager )
        })
}

//MARK: - Start Scanning For Peripherals
func startScanningForPeripherals() {
    _ = centralManager.observeState()
        .startWith( centralManager.state )
        .filter { $0 == .poweredOn }
        .flatMap { _ in self.centralManager.scanForPeripherals(withServices: [self.beaconServiceCBUUID]) }
        .subscribe(onNext: { [weak self] scannedPeripheral in
            guard let self = self else { return }
            self.configureScannedPeripherals(scannedPeripheral: scannedPeripheral)
        }, onError: { [weak self] error in
            guard let self = self else { return }
           print("\(error)")
            
        })
}

//MARK: - Connect To Peripheral Devices
func connectToPeripheralDevices(peripheral: Peripheral) {
    peripheral.establishConnection()
        .flatMap { $0.discoverServices(nil) }.asObservable()
        .flatMap { Observable.from($0) }
        .flatMap { $0.discoverCharacteristics(nil)}.asObservable()
        .flatMap { Observable.from($0) }
        .filter({ $0.properties.contains(.read) })
        .flatMap { $0.readValue() }
        .subscribe(onNext: {
            let data = $0.value
        }, onError: { error in
            print(error)
        }).disposed(by: disposeBag)
 }

}

// In App Delegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BluetoothManager.shared.configureCentralManager()
}

@andrewehab andrewehab changed the title Disconnect When device Disconnect Connection When device lost connection Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant