Guides and Sample Code

Developer

Playground Book Format Reference

On This Page

PlaygroundBluetoothCentralManager

A streamlined interface for connecting the central manager for the current playground page to nearby Bluetooth peripherals.

Initializers

init(services:queue:)

Creates a central manager that supports communicating with Bluetooth peripherals.

Declaration

  • init(services: [CBUUID]?, queue: DispatchQueue = .main)

Parameters

services

An array of service identifiers that the central manager scans for. If nil, all peripherals in range are eligible for connection attempts.

queue

The dispatch queue used to dispatch events to the central manager’s delegate.

Instance Properties

connectedPeripherals

An array of the peripherals currently connected to the central manager.

Declaration

  1. var connectedPeripherals: [CBPeripheral]

delegate

A delegate that can receive messages from the central manager by adopting the PlaygroundBluetoothCentralManagerDelegate protocol.

Declaration

  1. weak var delegate: PlaygroundBluetoothCentralManagerDelegate?

scanning

A Boolean value that determines whether the central manager is scanning for peripherals.

Declaration

  1. var scanning: Bool = false

Discussion

Set this property to true to start scanning for peripherals. Upon discovering a peripheral, the central manager calls the delegate’s centralManager(_:didDiscover:withAdvertisementData:rssi:) method.

state

The current state of the central manager.

Declaration

  1. var state: CBManagerState

Instance Methods

connect(to:timeout:callback:)

Attempts to connect the central manager to the specified peripheral.

Declaration

  • func connect(to peripheral: CBPeripheral, timeout: TimeInterval? = nil, callback: ((CBPeripheral, Error?) -> Void)? = nil)

Parameters

peripheral

The peripheral to attempt to connect to.

timeout

The amount of time, in seconds, before the connection attempt is aborted. If the timeout value is nil, the attempt won’t time out. To cancel a connection attempt, call the disconnect(from:) method.

callback

A function that’s called when the connection attempt succeeds or fails.

connect(toPeripheralWithUUID:timeout:callback:)

Attempts to connect the central manager to a peripheral with the specified unique identifier.

Declaration

  • func connect(toPeripheralWithUUID uuid: UUID, timeout: TimeInterval = 7, callback: ((CBPeripheral?, Error?) -> Void)? = nil)

Parameters

uuid

The unique identifier of the peripheral to attempt to connect to.

timeout

The amount of time, in seconds, before the connection attempt is aborted. If the timeout value is nil, the attempt won’t time out. To cancel a connection attempt, call the disconnect(from:) method.

callback

A function that’s called when the connection attempt succeeds or fails.

connectToLastConnectedPeripheral(timeout:callback:)

Attempts to reconnect the central manager to the most recently connected peripheral.

Declaration

  • func connectToLastConnectedPeripheral(timeout: TimeInterval = 7.0, callback: ((CBPeripheral?, Error?) -> Void)? = nil) -> Bool

Parameters

timeout

The amount of time, in seconds, before the connection attempt is aborted. If the timeout value is nil, the attempt won’t time out. To cancel a connection attempt, call the disconnect(from:) method.

callback

A function that’s called when the connection attempt succeeds or fails.

Return Value

A Boolean value indicating whether the currently active playground book has previously connected to a peripheral. If true, the central manager attempts to reconnect to it.

disconnect(from:)

Disconnects the central manager from the specified, connected peripheral.

Declaration

  • func disconnect(from peripheral: CBPeripheral)

Parameters

peripheral

The peripheral to disconnect from the central manager.