Skip to content

1. Initalizing CentralManager

paweljaneczek edited this page Jun 29, 2018 · 2 revisions

To begin work you should create an instance of CentralManager. It is really easy - all you need to do is to specify its 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 the instance of manager object, and passing it between parts of your app.

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