A small library that adds concurrency to CoreBluetooth APIs.

Last update: May 16, 2022

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 Cookbok.

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

https://github.com/manolofdez/AsyncBluetooth
Comments
  • 1. Swift Playgrounds 4 on iPad cannot add AsyncBluetooth

    I'd like to use your AsyncBluetooth with Swift Playgrounds 4 on iPad, but I cannot do that. It doesn't seem to be able to import any Swift Package released by tags with alphabetic characters, like v1.1.0, as follows:

    IMG_0077

    I was able to import my cloned repository re-tagged with 1.1.0 as follows:

    IMG_0078

    If there is no particular reason, could you use only numbers for tagging?

    Reviewed by bricklife at 2022-02-03 00:52
  • 2. Batching the connection requests

    CoreBluetooth devices such as the iPhone or the iPad can only be connected to a certain amount of devices at the same time – I think the limitation is in the range of about 5-12.

    If you continue to try connecting to more devices, unfortunate things can happen, i.e. the bluetooth daemon reset, timeouts, etc.

    I wonder whether AsyncBluetooth could do anything to help circumventing this by allowing to specify a threshold number. Any more connection request would then either fail or halt until the number of connected devices has decreased again.

    A concrete example… in my app I can't filter to scan for a specific service, since it's not standardized. Hence, I need to connect to each and every available device, do some tests and then decide whether the device is eligible for displaying or not. In theory this could mean tens or hundreds of connection attempts in parallel, which is a recipe for desaster.

    Reviewed by mickeyl at 2022-02-07 13:30
  • 3. maybe fix possible data race

    I am not quite sure about this but I think the original code could miss a first state change the way it was. It is quite possible that staring CBCentral is slow enough for this to never happen, though. Please explain to me why my change is unnecessary, if that is the case.

    Reviewed by oliverepper at 2022-02-21 09:04
  • 4. Notifications when a peripheral gets disconnected

    I have successfully paired and connected to a given peripheral. After the device has been streaming data for a while, the user disconnected (or turned off) the device. How can my app get notified of events like these?.

    Presumably, we need to forward the

    func centralManager(
            _ central: CBCentralManager,  
               didDisconnectPeripheral peripheral: CBPeripheral, 
               error: Error?
              )
    

    method from the Central Manager's delegate to a new "app delegate" we need to implement?

    Reviewed by maurovm at 2022-01-26 08:53
  • 5. Peripheral: Fix notifications

    It seems that the following lines are not called even if you set any notify on: https://github.com/manolofdez/AsyncBluetooth/blob/bc3e0bee46aac0fb20127872cdba9419e0402d02/Sources/Peripheral/Peripheral.swift#L298-L300

    I'm not sure if my fix fits your design, but it works fine. I hope this PR is helpful.

    Reviewed by bricklife at 2022-02-03 00:38
  • 6. Support iOS 14.0+ and macOS 11.0+

    Now async/await APIs can be used even in iOS 13 and macOS Catalina with Xcode 13.2. AsyncBluetooth uses Logger and it can be used in iOS 14.0+/macOS 11.0+, so, I set these versions.

    Reviewed by shu223 at 2022-01-25 07:40
CombineCoreBluetooth is a library that bridges Apple's CoreBluetooth framework and Apple's Combine framework

CombineCoreBluetooth is a library that bridges Apple's CoreBluetooth framework and Apple's Combine framework, making it possible to subscribe to perform bluetooth operations while subscribing to a publisher of the results of those operations, instead of relying on implementing delegates and manually filtering for the results you need.

May 10, 2022
Simple, block-based, lightweight library over CoreBluetooth. Will clean up your Core Bluetooth related code.
Simple, block-based, lightweight library over CoreBluetooth. Will clean up your Core Bluetooth related code.

LGBluetooth Simple, block-based, lightweight library over CoreBluetooth. Steps to start using Drag and Drop it into your project Import "LGBluetooth.h

Apr 26, 2022
Small utility command line tool you can run locally from your mac

push-service Small utility command line tool you can run locally from your mac, for testing sending push notifications to actual devices. The push ser

Dec 1, 2021
RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant.
RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant.

RxBluetoothKit is a Bluetooth library that makes interaction with BLE devices much more pleasant. It's backed by RxSwift and CoreBluetooth and it prov

May 18, 2022
A very simple library to discover and retrieve data from nearby devices (even if the peer app works at background).
A very simple library to discover and retrieve data from nearby devices (even if the peer app works at background).

Discovery: A simple library to discover and retrieve data from nearby devices. Discovery is a very simple but useful library for discovering nearby de

Feb 28, 2022
BLE (Bluetooth LE) for U🎁 Bleu is the best in the Bluetooth library.
BLE (Bluetooth LE) for U🎁 Bleu is the best in the Bluetooth library.

Bleu Bleu is a Bluetooth library. Bleu is the easiest way to operate CoreBluetooth. Bleu is possible to operate by replacing Bluetooth 's Peripheral a

May 16, 2022
Fluetooth - Flutter library for sending bytes to Bluetooth devices on Android/iOS

A Flutter library for sending bytes to Bluetooth devices. Available on Android a

Jan 2, 2022
SwiftyBluetooth - Closures based APIs for CoreBluetooth.

SwiftyBluetooth Closures based APIs for CoreBluetooth. Features Replace the delegate based interface with a closure based interface for every CBCentra

May 13, 2022
Closures based APIs for CoreBluetooth

SwiftyBluetooth Closures based APIs for CoreBluetooth. Features Replace the delegate based interface with a closure based interface for every CBCentra

May 13, 2022
CombineCoreBluetooth is a library that bridges Apple's CoreBluetooth framework and Apple's Combine framework

CombineCoreBluetooth is a library that bridges Apple's CoreBluetooth framework and Apple's Combine framework, making it possible to subscribe to perform bluetooth operations while subscribing to a publisher of the results of those operations, instead of relying on implementing delegates and manually filtering for the results you need.

May 10, 2022
Simple, block-based, lightweight library over CoreBluetooth. Will clean up your Core Bluetooth related code.
Simple, block-based, lightweight library over CoreBluetooth. Will clean up your Core Bluetooth related code.

LGBluetooth Simple, block-based, lightweight library over CoreBluetooth. Steps to start using Drag and Drop it into your project Import "LGBluetooth.h

Apr 26, 2022
iBeacons + CoreBluetooth
iBeacons + CoreBluetooth

OWUProximityManager Detect and connect to nearby devices with iBeacons and CoreBluetooth. Sample Project To simulate functionality, select Client on o

May 12, 2022
A declarative state management and dependency injection library for SwiftUI x Concurrency
A declarative state management and dependency injection library for SwiftUI x Concurrency

A declarative state management and dependency injection library for SwiftUI x Concurrency

May 20, 2022
A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed flag
A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed flag

HidesNavigationBarWhenPushed A library, which adds the ability to hide navigation bar when view controller is pushed via hidesNavigationBarWhenPushed

Feb 21, 2022
JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop interactions on devices that do not natively support this kind of interaction.
JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop interactions on devices that do not natively support this kind of interaction.

JustPeek Warning: This library is not supported anymore by Just Eat. JustPeek is an iOS Library that adds support for Force Touch-like Peek and Pop in

Apr 4, 2021
A lightweight library to calculate tensors in Swift, which has similar APIs to TensorFlow's
A lightweight library to calculate tensors in Swift, which has similar APIs to TensorFlow's

TensorSwift TensorSwift is a lightweight library to calculate tensors, which has similar APIs to TensorFlow's. TensorSwift is useful to simulate calcu

Apr 18, 2022
iOS library for device fingerprinting. Does not require server APIs to work, fully client-side operation.
iOS library for device fingerprinting. Does not require server APIs to work, fully client-side operation.

Lightweight iOS library for local device fingerprinting Installation (CocoaPods) # Podfile pod 'FingerprintJS' Note: If you've never used CocoaPods fo

May 23, 2022
A complete set of primitives for concurrency and reactive programming on Swift
A complete set of primitives for concurrency and reactive programming on Swift

A complete set of primitives for concurrency and reactive programming on Swift 1.4.0 is the latest and greatest, but only for Swift 4.2 and 5.0 use 1.

Jan 29, 2022
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.

Venice provides structured concurrency and CSP for Swift. Features Coroutines Coroutine cancelation Coroutine groups Channels Receive-only chan

May 3, 2022