Swiftpack.co - manolofdez/AsyncBluetooth as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by manolofdez.
manolofdez/AsyncBluetooth v1.1.0
A small library that adds concurrency to CoreBluetooth APIs.
⭐️ 79
🕓 44 weeks ago
iOS macOS watchOS
.package(url: "https://github.com/manolofdez/AsyncBluetooth.git", from: "v1.1.0")

AsyncBluetooth

A small library that adds concurrency to CoreBluetooth APIs.

Features

  • Async/Await APIs
  • Queueing of commands
  • Data conversion to common types
  • Thread safety
  • Convenience APIs for reading/writing without needing to explicitly discover characteristics.
  • Convenience API for waiting until Bluetooth is ready.

Usage

Scanning for a peripheral

Start scanning by calling the central manager's scanForPeripherals function. It returns an AsyncStream you can use to iterate over the discovered peripherals. Once you're satisfied with your scan, you can break from the loop and stop scanning.

let centralManager = CentralManager()

try await centralManager.waitUntilReady()

var scanDataStream = try await centralManager.scanForPeripherals(withServices: nil)
for await scanData in scanDataStream {
    // Check scan data...
}

await centralManager.stopScan()

Connecting to a peripheral

Once you have your peripheral, you can use the central manager to connect to it. Note you must hold a reference to the Peripheral while it's connected.

try await centralManager.connect(peripheral, options: nil)

Read value from characteristic

You can use convenience functions for reading and writing to characteristics. They will find the characteristic by using a UUID, and parse the data into the appropriate type.

let value: String? = try await peripheral.readValue(
    forCharacteristicWithUUID: UUID(uuidString: "")!,
    ofServiceWithUUID: UUID(uuidString: "")!
)

Examples

You can find practical, tasty recipes for how to use AsyncBluetooth in the AsyncBluetooth Cookbook.

Installation

Swift Package Manager

This library can be installed using the Swift Package Manager by adding it to your Package Dependencies.

Requirements

  • iOS 14.0+
  • MacOS 11.0+
  • Swift 5
  • Xcoce 13.2.1+

License

Licensed under MIT license.

GitHub

link
Stars: 79
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.4.0
1 week ago
  • Fixes characteristic notifications not sending data as expected
  • Fixes canceled connections preventing reconnections

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics