A small library that adds concurrency to CoreBluetooth APIs.

Overview

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.

Comments
  • Swift Playgrounds 4 on iPad cannot add AsyncBluetooth

    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?

    opened by bricklife 4
  • Batching the connection requests

    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.

    opened by mickeyl 3
  • willRestoreState delegate function support

    willRestoreState delegate function support

    Would it be possible to add support for centralManager(_:willRestoreState:) ?

    Currently there is no way to support state restoring because this is not available on the interface. This could work as a publisher.

    opened by gdetari 1
  • maybe fix possible data race

    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.

    opened by oliverepper 1
  • Notifications when a peripheral gets disconnected

    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?

    opened by maurovm 1
  • Peripheral: Fix notifications

    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.

    opened by bricklife 0
  • Support iOS 14.0+ and macOS 11.0+

    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.

    opened by shu223 0
  • [peripheral] Received UpdateValue result for characteristic without a continuation

    [peripheral] Received UpdateValue result for characteristic without a continuation

    Getting lots of "[peripheral] Received UpdateValue result for characteristic without a continuation" prints in console. This seems to slow down reads and writes, but eventually everything is working ok. The amount of prints seems to increase when disconnecting and connecting again to a device.

    Has this something to do with queueing of commands? Is there are way to flush the queue?

    opened by solita-jtn 4
  • Please mark Characteristic init as `public`

    Please mark Characteristic init as `public`

    Hi there! I am really loving using this package for my BLE projects; however I have run into several cases where I would like to manually generate a Characteristic struct in my app code, but the init is internal since it is not marked public in the code.

    I have so far hacked around this by cloning the repo and importing the code and manually modifying the code, but I would really like and appreciate if this was marked public in the package itself.

    I am curious if there is a reason it is not marked public?

    Thanks again for this amazing package!

    opened by jazzychad 1
  • How do I read/write to a peripheral?

    How do I read/write to a peripheral?

    As far as I can tell, there are no examples for reading from and writing to a peripheral, and no documentation whatsoever for the writeValue method. I'm having trouble receiving data from my peripheral but I'm not sure if I'm even using the readValue method correctly.

    opened by soltesza 2
Releases(1.4.1)
Owner
Manuel Fernandez
Manuel Fernandez
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.

Starry 74 Dec 29, 2022
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

null 170 Sep 19, 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

Blub Blub 3 Aug 2, 2022
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

Polidea 1.3k Jan 6, 2023
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

Ömer Faruk Gül 412 Dec 19, 2022
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

1amageek 484 Dec 29, 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

Iandi Santulus 1 Jan 2, 2022
The official Swift Library for Vital API, HealthKit and Devices

vital-ios The official Swift Library for Vital API, HealthKit and Devices Install We currently support SPM. Documentation Please refer to the official

Vital 17 Dec 22, 2022
The Bluetooth LE library for iOS and Mac. 100% Swift.

iOS-BLE-Library An in-development Bluetooth Low Energy Library by Nordic Semiconductor to interact with the , which is not complicated, but requires w

Nordic Semiconductor 6 Dec 19, 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

Jordane Belanger 181 Jan 2, 2023
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

Jordane Belanger 181 Jan 2, 2023
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.

Starry 74 Dec 29, 2022
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

null 170 Sep 19, 2022
This is a small View modifier that adds detents for native .sheet representations that appeared in iOS 16

SheetDetentsModifier This is a small View modifier that adds detents for .sheet representations that appeared in iOS 16 It works starting with iOS 15

Alex Artemev 19 Oct 9, 2022
iBeacons + CoreBluetooth

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

David Ohayon 363 Dec 24, 2022
MbientLab 2 Feb 5, 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

Ryo Aoyama 199 Jan 1, 2023
⚛️ A Reactive Data-Binding and Dependency Injection Library for SwiftUI x Concurrency.

SwiftUI Atom Properties A Reactive Data-Binding and Dependency Injection Library for SwiftUI x Concurrency ?? API Reference Introduction Examples Gett

Ryo Aoyama 199 Dec 17, 2022
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

Danil Gontovnik 55 Oct 19, 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 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

Just Eat 68 Apr 4, 2021