Last active
January 7, 2019 13:27
-
-
Save monday8am/2aebdff7216b09498771393eb50ba664 to your computer and use it in GitHub Desktop.
Example of how to use the RxCBCentralManager utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Creates the Rx version of the API passing the same parameters as the system version | |
let centralManager: RxCBCentralManager = RxCBCentralManager(queue: DispatchQueue.main, options: nil) | |
// Creates a Rx pipe using the published methods | |
centralManager.scanForPeripherals(withServices: [requiredServiceUIDD], options: nil) | |
.filter { (peripheral: CBPeripheral) -> Bool in | |
if let name = peripheral.name { | |
return name.contains("peripheralName") | |
} | |
return false | |
} | |
.flatMap { (peripheral: CBPeripheral) -> Observable<CBPeripheral> in | |
centralManager.stopScan() | |
return centralManager.connect(peripheral, options: nil) | |
} | |
.subscribe(onNext: { peripheral in | |
print("Conected to device: \(peripheral.name)") | |
}, onError: { error in | |
print("Error: \(peripheral.name)") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment