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
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()
-
Declaration
Swift
public var state: BluetoothState { get }
-
Declaration
Swift
public func observeState() -> Observable<BluetoothState>
-
Declaration
Swift
public func observeStateWithInitialValue() -> Observable<BluetoothState>
-
Scans for
Peripheral
s after subscription to returned observable. First parameterserviceUUIDs
is an array ofService
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 ofScannedPeripheral
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 receiveScannedPeripheral
which containsPeripheral
object,AdvertisementData
and 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
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 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.peripheralConnectionFailed
orBluetoothError.peripheralDisconnected
emission. Additionally you can pass optional dictionary to customise the behaviour of connection.Observable can ends with following errors:
BluetoothError.peripheralIsAlreadyObservingConnection
BluetoothError.peripheralConnectionFailed
BluetoothError.destroyed
BluetoothError.bluetoothUnsupported
BluetoothError.bluetoothUnauthorized
BluetoothError.bluetoothPoweredOff
BluetoothError.bluetoothInUnknownState
BluetoothError.bluetoothResetting
Declaration
Swift
public func establishConnection(_ peripheral: Peripheral, options: [String : Any]? = nil) -> Observable<Peripheral>
Parameters
peripheral
The
Peripheral
to whichCentralManager
is attempting to establish connection.options
Dictionary to customise the behaviour of connection.
Return Value
Observable
which emits next event after connection is established.
-
Returns list of the
Peripheral
s which are currently connected to theCentralManager
and contain all of the specifiedService
‘s UUIDs.Declaration
Swift
public func retrieveConnectedPeripherals(withServices serviceUUIDs: [CBUUID]) -> [Peripheral]
Parameters
serviceUUIDs
A list of
Service
UUIDsReturn Value
Retrieved
Peripheral
s. They are in connected state and contain all of theService
s with UUIDs specified in theserviceUUIDs
parameter. -
Returns list of
Peripheral
s by their identifiers which are known toCentralManager
.Declaration
Swift
public func retrievePeripherals(withIdentifiers identifiers: [UUID]) -> [Peripheral]
Parameters
identifiers
List of
Peripheral
‘s identifiers which should be retrieved.Return Value
Retrieved
Peripheral
s.
-
Emits
Peripheral
instance when it’s connected.It’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
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 anyPeripheral
.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.Observable can ends with following errors:
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 anyPeripheral
.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 thecancelPeripheralConnection
call.
-
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
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.Seealso
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.