SynchronizedPeripheral
public class SynchronizedPeripheral
A synchronous interface to the Bluetooth peripheral, intended to be used inside the backgroundTask block of run(backgroundTask:completionOnMainThread:)
to perform multi-part operations without the need for a complicated callback or promise setup.
-
Read a value from the specified characteristic synchronously.
Declaration
Swift
public func read<R>(from characteristicIdentifier: CharacteristicIdentifier) throws -> R where R : Receivable
-
Write a value from the specified characteristic synchronously.
Declaration
Swift
public func write<S: Sendable>(to characteristicIdentifier: CharacteristicIdentifier, value: S, type: CBCharacteristicWriteType = .withResponse) throws
-
Write to one characterestic then reading a value from another.
Declaration
Swift
public func writeAndRead<R: Receivable, S: Sendable> ( writeTo: CharacteristicIdentifier, value: S, type: CBCharacteristicWriteType = .withResponse, readFrom: CharacteristicIdentifier) throws -> R
-
Listen for changes on a specified characterstic synchronously.
Declaration
Swift
public func listen<R: Receivable>( // swiftlint:disable:this cyclomatic_complexity to characteristicIdentifier: CharacteristicIdentifier, timeout: Timeout = .none, completion: @escaping (R) -> ListenAction) throws
-
Stop listening to a characteristic synchronously.
Declaration
Swift
public func endListen(to characteristicIdentifier: CharacteristicIdentifier, error: Error? = nil, completion: ((WriteResult) -> Void)? = nil) throws
-
Flush a listen to a characteristic by receiving and discarding values for the specified duration.
Warning Timeout defaults to 3 seconds. Specifying no timeout or a timeout with zero second will result in a fatal error.
Declaration
Swift
public func flushListen(to characteristicIdentifier: CharacteristicIdentifier, nonZeroTimeout: Timeout = .seconds(3), completion: @escaping () -> Void) throws
Parameters
characteristicIdentifier
The characteristic to flush.
nonZeroTimeout
How long to wait for incoming data.
completion
Block to call when the flush is complete.
-
Handle a compound operation consisting of writing on one characterstic followed by listening on another for some streamed data.
Conceptually very similar to just calling write, then listen, except that the listen is set up before the write is issued, so that there should be no risks of data loss due to missed notifications, which there would be with calling them seperatly.
Declaration
Swift
public func writeAndListen<S: Sendable, R: Receivable>( // swiftlint:disable:this cyclomatic_complexity writeTo charToWriteTo: CharacteristicIdentifier, value: S, type: CBCharacteristicWriteType = .withResponse, listenTo charToListenTo: CharacteristicIdentifier, timeoutInSeconds: Int = 0, completion: @escaping (R) -> ListenAction) throws
-
Similar to
writeAndListen
, but use this if you don’t know or don’t have control over how many packets will be sent to you. You still need to know the total size of the data you’re receiving.Declaration
Swift
public func writeAndAssemble<S: Sendable, R: Receivable>( // swiftlint:disable:this cyclomatic_complexity writeTo charToWriteTo: CharacteristicIdentifier, value: S, listenTo charToListenTo: CharacteristicIdentifier, expectedLength: Int, timeoutInSeconds: Int = 0, completion: @escaping (R) -> ListenAction) throws
-
Ask for the peripheral’s maximum payload length in bytes for a single write request.
Declaration
Swift
public func maximumWriteValueLength(for writeType: CBCharacteristicWriteType) -> Int
-
Declaration
Swift
public func bluetoothAvailable(_ available: Bool)
-
Declaration
Swift
public func disconnected(from peripheral: PeripheralIdentifier)