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
-
Implementation of CBCentralManager
Declaration
Swift
public let manager: CBCentralManager -
Undocumented
Declaration
Swift
public var centralManager: CBCentralManager { get }
-
Creates new
CentralManagerinstance. 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
queueQueue on which bluetooth callbacks are received. By default main thread is used.
optionsAn 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()
-
Declaration
Swift
public var state: BluetoothState { get } -
Declaration
Swift
public func observeState() -> Observable<BluetoothState> -
Declaration
Swift
public func observeStateWithInitialValue() -> Observable<BluetoothState>
-
Scans for
Peripherals after subscription to returned observable. First parameterserviceUUIDsis an array ofServiceUUIDs which needs to be implemented by a peripheral to be discovered. If user don’t want to filter any peripherals,nilcan be used instead. Additionally dictionary of CBCentralManager specific options can be passed to allow further customisation. Scans by default are infinite streams ofScannedPeripheralstructures 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.scanInProgresserror if this method will be called when there is already ongoing scan. As a result you will receiveScannedPeripheralwhich containsPeripheralobject,AdvertisementDataand peripheral’s RSSI registered during discovery. You can thenestablishConnection(_:options:)and do other operations.Seealso
Observable can ends with following errors:
Declaration
Swift
public func scanForPeripherals(withServices serviceUUIDs: [CBUUID]?, options: [String: Any]? = nil) -> Observable<ScannedPeripheral>Parameters
serviceUUIDsServices of peripherals to search for. Nil value will accept all peripherals.
optionsOptional scanning options.
Return Value
Infinite stream of scanned peripherals.
-
Establishes connection with a given
Peripheral. When connection did succeded it sends event withPeripheral- 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 followingBluetoothError.peripheralConnectionFailedorBluetoothError.peripheralDisconnectedemission. Additionally you can pass optional dictionary to customise the behaviour of connection.Observable can ends with following errors:
BluetoothError.peripheralIsAlreadyObservingConnectionBluetoothError.peripheralConnectionFailedBluetoothError.destroyedBluetoothError.bluetoothUnsupportedBluetoothError.bluetoothUnauthorizedBluetoothError.bluetoothPoweredOffBluetoothError.bluetoothInUnknownStateBluetoothError.bluetoothResetting
Declaration
Swift
public func establishConnection(_ peripheral: Peripheral, options: [String : Any]? = nil) -> Observable<Peripheral>Parameters
peripheralThe
Peripheralto whichCentralManageris attempting to establish connection.optionsDictionary to customise the behaviour of connection.
Return Value
Observablewhich emits next event after connection is established.
-
Returns list of the
Peripherals which are currently connected to theCentralManagerand contain all of the specifiedService‘s UUIDs.Declaration
Swift
public func retrieveConnectedPeripherals(withServices serviceUUIDs: [CBUUID]) -> [Peripheral]Parameters
serviceUUIDsA list of
ServiceUUIDsReturn Value
Retrieved
Peripherals. They are in connected state and contain all of theServices with UUIDs specified in theserviceUUIDsparameter. -
Returns list of
Peripherals by their identifiers which are known toCentralManager.Declaration
Swift
public func retrievePeripherals(withIdentifiers identifiers: [UUID]) -> [Peripheral]Parameters
identifiersList of
Peripheral‘s identifiers which should be retrieved.Return Value
Retrieved
Peripherals.
-
Emits
Peripheralinstance when it’s connected.It’s infinite stream, so
.completeis never called.Observable can ends with following errors:
Declaration
Swift
public func observeConnect(for peripheral: Peripheral? = nil) -> Observable<Peripheral>Parameters
peripheralOptional
Peripheralwhich is observed for connection. When not specified it will observe fo anyPeripheral.Return Value
Observable which emits next events when
peripheralwas connected. -
Emits
Peripheralinstance when it’s disconnected.It’s infinite stream, so
.completeis never called.Observable can ends with following errors:
Declaration
Swift
public func observeDisconnect(for peripheral: Peripheral? = nil) -> Observable<(Peripheral, DisconnectionReason?)>Parameters
peripheralOptional
Peripheralwhich is observed for disconnection. When not specified it will observe for anyPeripheral.Return Value
Observable which emits next events when
Peripheralinstance was disconnected. It provides optional error which may contain more information about the cause of the disconnection if it wasn’t thecancelPeripheralConnectioncall.
-
Undocumented
Declaration
Swift
public func observeANCSAuthorized(for peripheral: Peripheral) -> Observable<Bool>
-
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
CentralManagerinstance, 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
Declaration
Swift
public convenience init(queue: DispatchQueue = .main, options: [String: AnyObject]? = nil, onWillRestoreCentralManagerState: OnWillRestoreCentralManagerState? = nil)Parameters
queueQueue on which bluetooth callbacks are received. By default main thread is used and all operations and events are executed and received on main thread.
optionsAn optional dictionary containing initialization options for a central manager. For more info about it please refer to Central Manager initialization options
onWillRestoreCentralManagerStateClosure called when state has been restored.
View on GitHub
CentralManager Class Reference