CentralManager

public class CentralManager

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 observeState() and then chaining it with scanForPeripherals(_:options:):

centralManager.observeState
    .startWith(centralManager.state)
    .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.

Seealso

Peripheral
  • 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()
  • Scans for Peripherals after subscription to returned observable. First parameter serviceUUIDs is an array of Service UUIDs which needs to be implemented by a peripheral to be discovered. If user don’t want to filter any peripherals, nil can be used instead. Additionally dictionary of CBCentralManager specific options can be passed to allow further customisation. Scans by default are infinite streams of ScannedPeripheral structures which need to be stopped by the user. For example this can be done by limiting scanning to certain number of peripherals or time:

    centralManager.scanForPeripherals(withServices: nil)
        .timeout(3.0, timeoutScheduler)
        .take(2)
    

    There can be only one ongoing scanning. It will return BluetoothError.scanInProgress error if this method will be called when there is already ongoing scan. 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.

    Seealso

    Peripheral

    Declaration

    Swift

    public func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String: Any]? = nil)
                    -> Observable<ScannedPeripheral>

    Parameters

    serviceUUIDs

    Services of peripherals to search for. Nil value will accept all peripherals.

    options

    Optional scanning options.

    Return Value

    Infinite stream of scanned peripherals.

  • Establishes connection with a given Peripheral. When connection did succeded it sends event with Peripheral - from now on it is possible to call all other methods that require connection. The connection is automatically disconnected when resulting Observable is unsubscribed. On the other hand when the connection is interrupted or failed by the device or the system, the Observable will be unsubscribed as well following BluetoothError.peripheralConnectionFailed or BluetoothError.peripheralDisconnected emission. Additionally you can pass optional dictionary to customise the behaviour of connection.

    Declaration

    Swift

    public func establishConnection(_ peripheral: Peripheral, options: [String: Any]? = nil) -> Observable<Peripheral>

    Parameters

    peripheral

    The Peripheral to which CentralManager is attempting to establish connection.

    options

    Dictionary to customise the behaviour of connection.

    Return Value

    Observable which emits next event after connection is established.

  • Emits Peripheral instance when it’s connected.

    It’s infinite stream, so .complete is never called.

    Declaration

    Swift

    public func observeConnect(for peripheral: Peripheral? = nil) -> Observable<Peripheral>

    Parameters

    peripheral

    Optional Peripheral which is observed for connection. When not specified it will observe fo any Peripheral.

    Return Value

    Observable which emits next events when peripheral was connected.

  • Emits Peripheral instance when it’s disconnected.

    It’s infinite stream, so .complete is never called.

    Declaration

    Swift

    public func observeDisconnect(for peripheral: Peripheral? = nil) -> Observable<(Peripheral, DisconnectionReason?)>

    Parameters

    peripheral

    Optional Peripheral which is observed for disconnection. When not specified it will observe for any Peripheral.

    Return Value

    Observable which emits next events when Peripheral instance was disconnected. It provides optional error which may contain more information about the cause of the disconnection if it wasn’t the cancelConnection call.