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
-
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
-
Declaration
Swift
public var state: BluetoothState { get }
-
Declaration
Swift
public func observeState() -> Observable<BluetoothState>
-
Declaration
Swift
public func observeStateWithInitialValue() -> Observable<BluetoothState>
-
Starts peripheral advertising on subscription. It create inifinite observable which emits only one next value, of enum type
StartAdvertisingResult
, just after advertising start succeeds. For more info of what specificStartAdvertisingResult
enum cases means please refer to `StartAdvertisingResult
documentation.There can be only one ongoing advertising (CoreBluetooth limit). It will return
advertisingInProgress
error if this method is called when it is already advertising.Advertising is automatically stopped just after disposing of the subscription.
It can return
BluetoothError.advertisingStartFailed
error, when start advertisement failedObservable can ends with following errors:
Declaration
Swift
public func startAdvertising(_ advertisementData: [String : Any]?) -> Observable<StartAdvertisingResult>
Parameters
advertisementData
Services of peripherals to search for. Nil value will accept all peripherals.
Return Value
Infinite observable which emit
StartAdvertisingResult
when advertisement started.
-
Function that triggers
CBPeripheralManager.add(_:)
and waits for delegateCBPeripheralManagerDelegate.peripheralManager(_:didAdd:error:)
result. If it receives a non nil in the result, it will emitBluetoothError.addingServiceFailed
error. Add method is called after subscription toObservable
is made.Observable can ends with following errors:
Declaration
Swift
public func add(_ service: CBMutableService) -> Single<CBService>
Parameters
service
Characteristic
to read value fromReturn Value
Single
which emitsnext
with given characteristic when value is ready to read. -
Wrapper for
CBPeripheralManager.remove(_:)
methodDeclaration
Swift
public func remove(_ service: CBMutableService)
-
Wrapper for
CBPeripheralManager.removeAllServices()
methodDeclaration
Swift
public func removeAllServices()
-
Continuous observer for
CBPeripheralManagerDelegate.peripheralManager(_:didReceiveRead:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
public func observeDidReceiveRead() -> Observable<CBATTRequest>
Return Value
Observable that emits
next
event whenever didReceiveRead occurs. -
Continuous observer for
CBPeripheralManagerDelegate.peripheralManager(_:didReceiveWrite:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
public func observeDidReceiveWrite() -> Observable<[CBATTRequest]>
Return Value
Observable that emits
next
event whenever didReceiveWrite occurs. -
Wrapper for
CBPeripheralManager.respond(to:withResult:)
methodDeclaration
Swift
public func respond(to request: CBATTRequest, withResult result: CBATTError.Code)
-
Wrapper for
CBPeripheralManager.updateValue(_:for:onSubscribedCentrals:)
methodDeclaration
Swift
public func updateValue( _ value: Data, for characteristic: CBMutableCharacteristic, onSubscribedCentrals centrals: [CBCentral]?) -> Bool
-
Continuous observer for
CBPeripheralManagerDelegate.peripheralManagerIsReady(toUpdateSubscribers:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
public func observeIsReadyToUpdateSubscribers() -> Observable<Void>
Return Value
Observable that emits
next
event whenever isReadyToUpdateSubscribers occurs.
-
Continuous observer for
CBPeripheralManagerDelegate.peripheralManager(_:central:didSubscribeTo:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
public func observeOnSubscribe() -> Observable<(CBCentral, CBCharacteristic)>
Return Value
Observable that emits
next
event whenever didSubscribeTo occurs. -
Continuous observer for
CBPeripheralManagerDelegate.peripheralManager(_:central:didUnsubscribeFrom:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
public func observeOnUnsubscribe() -> Observable<(CBCentral, CBCharacteristic)>
Return Value
Observable that emits
next
event whenever didUnsubscribeFrom occurs.
-
Starts publishing L2CAP channel on a subscription. It creates an infinite observable which emits only one next value, of
CBL2CAPPSM
type, just after L2CAP channel has been published.Channel is automatically unpublished just after disposing of the subscription.
It can return
publishingL2CAPChannelFailed
error when publishing channel failedObservable can ends with following errors:
Declaration
Swift
@available(iOS 11, tvOS 11, watchOS 4, *) public func publishL2CAPChannel(withEncryption encryptionRequired: Bool) -> Observable<CBL2CAPPSM>
Parameters
encryptionRequired
Publishing channel with or without encryption.
Return Value
Infinite observable which emit
CBL2CAPPSM
when channel published. -
Continuous observer for
CBPeripheralManagerDelegate.peripheralManager(_:didOpen:error:)
resultsIt’s infinite stream, so
.complete
is never called.Observable can ends with following errors:
Declaration
Swift
@available(iOS 11, tvOS 11, watchOS 4, *) public func observeDidOpenL2CAPChannel() -> Observable<(CBL2CAPChannel?, Error?)>
Return Value
Observable that emits
next
event whenever didOpen occurs.
-
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
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.