Skip to content

Instantly share code, notes, and snippets.

@charlesmuchene
Created April 17, 2020 03:48
Show Gist options
  • Save charlesmuchene/f89ac3a2cea3937583065dc66d25a85c to your computer and use it in GitHub Desktop.
Save charlesmuchene/f89ac3a2cea3937583065dc66d25a85c to your computer and use it in GitHub Desktop.
iOS as Central
// Initialize central manager, set self as delegate
centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: TraceService.CMRestoreIdentifierKey])
// Provide implementation for delegate callback methods
extension TraceService: CBCentralManagerDelegate {
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String: Any]) { ... }
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// When .poweredOn, scan for Peripherals
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
// Filter Peripherals with TraceService UUID in advertisementData
// Initiate connection to Peripheral
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
peripheral.delegate = self
peripheral.discoverServices(TraceProfile.SERVICES)
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { ... }
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment