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
true
the connection process will start, once the peripheral has been found a connection will be established. If you returnfalse
iOS 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 theconnectionEventPublisher
Declaration
Swift
public var autoconnectionHandler: AutoconnectionHandler?
-
Connected peripheral.
nil
if 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
ConnectionEvent
Declaration
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
LittleBlueToothCharacteristic
that you are already listening to. It’s up to you to filter them and convert raw data to theReadable
object.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 toReadable
Declaration
Swift
public func connectableListenPublisher<T>(for characteristic: LittleBlueToothCharacteristic, valueType: T.Type) -> Publishers.MakeConnectable<AnyPublisher<T, LittleBluetoothError>> where T : Readable
Parameters
characteristic
Characteristc you want to be notified.
valueType
The type of the value you want the raw
Data
be 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 toReadable
Declaration
Swift
public func startListen<T>(from charact: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where T : Readable
Parameters
characteristic
Characteristc 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
LittleBlueToothCharacteristic
where the notify command has been activated. After starting the listen command you should subscribe to thelistenPublisher
to 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 thelistenPublisher
to be notified.Declaration
Swift
public func enableListen(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError>
Parameters
characteristic
Characteristc you want to be notified.
Return Value
A publisher with the
LittleBlueToothCharacteristic
where the notify command has been activated. -
Disable listen from a specific characteristic
Declaration
Swift
public func disableListen(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<LittleBlueToothCharacteristic, LittleBluetoothError>
Parameters
characteristic
characteristic 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 toReadable
Declaration
Swift
public func read<T>(from characteristic: LittleBlueToothCharacteristic) -> AnyPublisher<T, LittleBluetoothError> where T : Readable
Parameters
characteristic
characteristic 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 toWritable
Declaration
Swift
public func write<T>(to characteristic: LittleBlueToothCharacteristic, value: T, response: Bool = true) -> AnyPublisher<Void, LittleBluetoothError> where T : Writable
Parameters
characteristic
characteristic where you want to write
value
The value you want to write
response
An optional
Bool
value 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 : Readable
Parameters
characteristic
characteristic where you want to write and listen
value
The value you want to write must conform to
Writable
Return Value
A publisher with that post and error or the response of the write requests.
-
Starts scanning for
PeripheralDiscovery
Declaration
Swift
public func startDiscovery(withServices services: [CBUUID]?, options: [String : Any]? = nil) -> AnyPublisher<PeripheralDiscovery, LittleBluetoothError>
Parameters
services
Services for peripheral you are looking for
options
Scanning 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
PeripheralIdentifier
Declaration
Swift
public func connect(to peripheralIdentifier: PeripheralIdentifier, options: [String : Any]? = nil) -> AnyPublisher<Peripheral, LittleBluetoothError>
Parameters
options
Connecting options same as CoreBluetooth central manager option.
Return Value
A publisher with the just connected
Peripheral
. -
Starts connection for
PeripheralDiscovery
Declaration
Swift
public func connect(to discovery: PeripheralDiscovery, options: [String : Any]? = nil) -> AnyPublisher<Peripheral, LittleBluetoothError>
Parameters
options
Connecting options same as CoreBluetooth central manager option.
Return Value
A publisher with the just connected
Peripheral
.
-
Disconnect the connected
Peripheral
Declaration
Swift
@discardableResult public func disconnect() -> AnyPublisher<Peripheral, LittleBluetoothError>
Return Value
A publisher with the just disconnected
Peripheral
or aLittleBluetoothError
-
Sometimes you may need to extract
CBCentralManager
andCBPeripheral
During 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)