Skip to content

Instantly share code, notes, and snippets.

@monday8am
Last active January 7, 2019 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monday8am/2aebdff7216b09498771393eb50ba664 to your computer and use it in GitHub Desktop.
Save monday8am/2aebdff7216b09498771393eb50ba664 to your computer and use it in GitHub Desktop.
Example of how to use the RxCBCentralManager utility
// 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