Skip to content

1. Initalizing CentralManager

paweljaneczek edited this page Jun 22, 2018 · 2 revisions

To begin work you should create an instance of CentralManager. Doing it is really easy - all you need to specify is queue(main queue is used by default):

let manager = CentralManager(queue: .main)

It is also possible to create CentralManager instance with options:

let options = [CBCentralManagerOptionRestoreIdentifierKey: "RestoreIdentifierKey"] as [String: AnyObject]
let manager = CentralManager(queue: .main, options: options)

Handling restore state:

let options = [CBCentralManagerOptionRestoreIdentifierKey: "RestoreIdentifierKey"] as [String: AnyObject]
let manager = CentralManager(queue: .main, options: options, onWillRestoreCentralManagerState: { restoredState in
    let restoredPeripherals = restoredState.peripherals
    let restoredScanOptions = restoredState.scanOptions
    let restoredServices = restoredState.services
})

You are responsible for maintaining instance of manager object, and passing it between parts of your app.

Note: All operations are executed in queue which you have provided, so make sure to observe UI related effects in main thread when it's needed.