CentralManager

public class CentralManager : ManagerType

CentralManager is a class implementing ReactiveX API which wraps all Core Bluetooth Manager’s functions allowing to discover, connect to remote peripheral devices and more. You can start using this class by discovering available services of nearby peripherals. Before calling any public CentralManager‘s functions you should make sure that Bluetooth is turned on and powered on. It can be done by calling and observing returned value of observeStateWithInitialValue() and then chaining it with scanForPeripherals(_:options:):

let disposable = centralManager.observeStateWithInitialValue()
    .filter { $0 == .poweredOn }
    .take(1)
    .flatMap { centralManager.scanForPeripherals(nil) }

As a result you will receive ScannedPeripheral which contains Peripheral object, AdvertisementData and peripheral’s RSSI registered during discovery. You can then establishConnection(_:options:) and do other operations. You can also simply stop scanning with just disposing it:

disposable.dispose()

Seealso

Peripheral

Initialization

  • Creates new CentralManager instance. By default all operations and events are executed and received on main thread.

    Warning

    If you pass background queue to the method make sure to observe results on main thread for UI related code.

    Declaration

    Swift

    public convenience init(queue: DispatchQueue = .main,
                            options: [String: AnyObject]? = nil)

    Parameters

    queue

    Queue on which bluetooth callbacks are received. By default main thread is used.

    options

    An optional dictionary containing initialization options for a central manager. For more info about it please refer to Central Manager initialization options

  • Attaches RxBluetoothKit delegate to CBCentralManager. This method is useful in cases when delegate of CBCentralManager was reassigned outside of RxBluetoothKit library (e.g. CBCentralManager was used in some other library or used in non-reactive way)

    Declaration

    Swift

    public func attach()

State

Scanning

Peripheral’s Connection Management

Retrieving Lists of Peripherals

Connection and disconnection observing

ANCS

State restoration

  • Deprecated, use CentralManager.init(queue:options:onWillRestoreCentralManagerState:) instead

    Declaration

    Swift

    @available(*, deprecated, renamed: "CentralManager.init(queue:options:onWillRestoreCentralManagerState:﹚")
    public convenience init(queue: DispatchQueue = .main,
                            options: [String: AnyObject]? = nil,
                            onWillRestoreState: OnWillRestoreState? = nil)
  • Creates new CentralManager instance, which supports bluetooth state restoration.

    Warning

    If you pass background queue to the method make sure to observe results on main thread for UI related code.

    Declaration

    Swift

    public convenience init(queue: DispatchQueue = .main,
                            options: [String: AnyObject]? = nil,
                            onWillRestoreCentralManagerState: OnWillRestoreCentralManagerState? = nil)

    Parameters

    queue

    Queue on which bluetooth callbacks are received. By default main thread is used and all operations and events are executed and received on main thread.

    options

    An optional dictionary containing initialization options for a central manager. For more info about it please refer to Central Manager initialization options

    onWillRestoreCentralManagerState

    Closure called when state has been restored.