LittleBlueTooth
public class LittleBlueTooth : Identifiable
LittleBlueTooth can control only one peripheral at time. It has an id properties to identifiy different instances.
Please note that Apple do not enacourage the use of more CBCentralManger instances, due to resurce hits.
Link
-
LittleBlueTooth instance identifier
Declaration
Swift
public let id: UUID -
This is usefull when you have auto-reconnection and want to do some task right after a connection. All other tasks will be delayed until this one ends.
Declaration
Swift
public var connectionTasks: AnyPublisher<Void, LittleBluetoothError>? -
This handler must be used to handle connection process after a disconnession. You can inspect the error and decide if an automatic connection is necessary. If you return
truethe connection process will start, once the peripheral has been found a connection will be established. If you returnfalseiOS will not try to establish a connection Connection process will remain active also in background if the app has the right permission, to cancel just calldisconnect. When a connection will be established an.autoConnected(PeripheralIdentifier)event will be streamed to theconnectionEventPublisherDeclaration
Swift
public var autoconnectionHandler: AutoconnectionHandler? -
Connected peripheral.
nilif not connected or a connection is not requestedDeclaration
Swift
public var peripheral: Peripheral? { get set } -
Publisher that streams peripheral state available only when a connection is requested for fine grained control
Declaration
Swift
public var peripheralStatePublisher: AnyPublisher<PeripheralState, Never> { get } -
Publisher that streams
ConnectionEventDeclaration
Swift
public lazy var connectionEventPublisher: AnyPublisher<ConnectionEvent, Never> { get set } -
Publish name and service changes
Declaration
Swift
public var changesStatePublisher: AnyPublisher<PeripheralChanges, Never> { get } -
Publish all values from
LittleBlueToothCharacteristicthat you are already listening to. It’s up to you to filter them and convert raw data to theReadableobject.Declaration
Swift
public var listenPublisher: AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError> { get } -
Undocumented
Declaration
Swift
public var restoreStatePublisher: AnyPublisher<CentralRestorer, Never> { get } -
Enable logging disabled by default Enable logging, log is made using os_log and it exposes some information even in release configuration
Declaration
Swift
public var isLogEnabled: Bool { get set }
-
Undocumented
Declaration
Swift
public init(with configuration: LittleBluetoothConfiguration)
-
Undocumented
Declaration
Swift
public func readRSSI() -> AnyPublisher<Int, LittleBluetoothError>
-
Returns a multicast publisher once you attached all the subscriber you must call
connect()Important
The type of the value must be conform toReadableDeclaration
Swift
public func connectableListenPublisher<T>(for characteristic: LittleBlueToothCharacteristic, valueType: T.Type) -> Publishers.MakeConnectable<AnyPublisher<T, LittleBluetoothError>> where T : ReadableParameters
characteristicCharacteristc you want to be notified.
valueTypeThe type of the value you want the raw
Databe convertedReturn Value
A multicast publisher that will send out values of the type you choose.
-
Returns a shared publisher for listening to a specific characteristic.
Important
The type of the value must be conform toReadableDeclaration
Swift
public func startListen<T>(from charact: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where T : ReadableParameters
characteristicCharacteristc you want to be notified.
Return Value
A shared publisher that will send out values of the type defined by the generic type.
-
Returns a publisher with the
LittleBlueToothCharacteristicwhere the notify command has been activated. After starting the listen command you should subscribe to thelistenPublisherto be notified.Important
This publisher only activate the notification on a specific characteristic, it will not send notified values. After starting the listen command you should subscribe to thelistenPublisherto be notified.Declaration
Swift
public func enableListen(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError>Parameters
characteristicCharacteristc you want to be notified.
Return Value
A publisher with the
LittleBlueToothCharacteristicwhere the notify command has been activated. -
Disable listen from a specific characteristic
Declaration
Swift
public func disableListen(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError>Parameters
characteristiccharacteristic you want to stop listen
Return Value
A publisher with that informs you about the successful or failed task
-
Read a value from a specific charteristic
Important
The type of the value must be conform toReadableDeclaration
Swift
public func read<T>(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where T : ReadableParameters
characteristiccharacteristic where you want to read
Return Value
A publisher with the value you want to read.
-
Write a value to a specific charteristic
Important
The type of the value must be conform toWritableDeclaration
Swift
public func write<T>(to characteristic: LittleBlueToothCharacteristic, value: T, response: Bool = true) -> AnyPublisher<Void, LittleBluetoothError> where T : WritableParameters
characteristiccharacteristic where you want to write
valueThe value you want to write
responseAn optional
Boolvalue that will look for error after write operationReturn Value
A publisher with that informs you about eventual error
-
Write a value to a specific charteristic and wait for a response
Declaration
Swift
public func writeAndListen<W, R>(from characteristic: LittleBlueToothCharacteristic, value: W) -> AnyPublisher<R, LittleBluetoothError> where W : Writable, R : ReadableParameters
characteristiccharacteristic where you want to write and listen
valueThe value you want to write must conform to
WritableReturn Value
A publisher with that post and error or the response of the write requests.
-
Starts scanning for
PeripheralDiscoveryDeclaration
Swift
public func startDiscovery(withServices services: [CBUUID]?, options: [String : Any]? = nil) -> AnyPublisher<PeripheralDiscovery, LittleBluetoothError>Parameters
servicesServices for peripheral you are looking for
optionsScanning options same as CoreBluetooth central manager option.
Return Value
A publisher with stream of disovered peripherals.
-
Stops peripheral discovery
Declaration
Swift
public func stopDiscovery() -> AnyPublisher<Void, LittleBluetoothError>Return Value
A publisher when discovery has been stopped
-
Starts connection for
PeripheralIdentifierDeclaration
Swift
public func connect(to peripheralIdentifier: PeripheralIdentifier, options: [String : Any]? = nil) -> AnyPublisher<Peripheral, LittleBluetoothError>Parameters
optionsConnecting options same as CoreBluetooth central manager option.
Return Value
A publisher with the just connected
Peripheral. -
Starts connection for
PeripheralDiscoveryDeclaration
Swift
public func connect(to discovery: PeripheralDiscovery, options: [String : Any]? = nil) -> AnyPublisher<Peripheral, LittleBluetoothError>Parameters
optionsConnecting options same as CoreBluetooth central manager option.
Return Value
A publisher with the just connected
Peripheral.
-
Disconnect the connected
PeripheralDeclaration
Swift
@discardableResult public func disconnect() -> AnyPublisher<Peripheral, LittleBluetoothError>Return Value
A publisher with the just disconnected
Peripheralor aLittleBluetoothError
-
Sometimes you may need to extract
CBCentralManagerandCBPeripheralDuring this operation everything is stopped, delegates are set to nil current operation cancelledDeclaration
Swift
public func extract() -> (central: CBCentralManager, peripheral: CBPeripheral?)Return Value
A tuple with the central and the peripheral if connected
-
Undocumented
Declaration
Swift
public func restart(with central: CBCentralManager, peripheral: CBPeripheral? = nil)
View on GitHub
LittleBlueTooth Class Reference