PeripheralManager

public class PeripheralManager : ManagerType

PeripheralManager is a class implementing ReactiveX API which wraps all the Core Bluetooth Peripheral’s functions, that allow to advertise, to publish L2CAP channels and more. You can start using this class by adding services and starting advertising. Before calling any public PeripheralManager‘s functions you should make sure that Bluetooth is turned on and powered on. It can be done by observeStateWithInitialValue(), observing it’s value and then chaining it with add(_:) and startAdvertising(_:):

let disposable = centralManager.observeStateWithInitialValue()
    .filter { $0 == .poweredOn }
    .take(1)
    .flatMap { centralManager.add(myService) }
    .flatMap { centralManager.startAdvertising(myAdvertisementData) }

As a result, your peripheral will start advertising. To stop advertising simply dispose it:

disposable.dispose()
  • Implementation of CBPeripheralManager

    Declaration

    Swift

    public let manager: CBPeripheralManager

Initialization

  • Creates new PeripheralManager 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 peripheral manager. For more info about it please refer to Peripheral Manager initialization options

State

Advertising

Services

Read & Write

Updating value

Subscribing

L2CAP

State restoration

  • Creates new PeripheralManager 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.

    Seealso

    RestoredState

    Declaration

    Swift

    public convenience init(queue: DispatchQueue = .main,
                            options: [String: AnyObject]? = nil,
                            onWillRestorePeripheralManagerState: OnWillRestorePeripheralManagerState? = 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 peripheral manager. For more info about it please refer to Peripheral Manager initialization options

    onWillRestorePeripheralManagerState

    Closure called when state has been restored.