BLESwift is a Bluetooth Low Energy library for Swift 6.2 that uses async/await.
It puts CoreBluetooth inside one actor. You do not write delegates or callbacks.
You connect, read, write, and scan with async throws calls and AsyncSequence streams.
BLESwift supports the central role and the peripheral role. It also includes fakes, so you
can test your BLE code without hardware.
Add BLESwift to the dependencies in your Package.swift:
dependencies: [
.package(url: "https://github.com/kylebrowning/BLESwift.git", from: "3.2.0")
]Then add "BLESwift" to the dependencies of your target.
import BLESwift
import BLESwiftProfiles
let central = Central()
// Wait until the radio is on.
for await state in await central.stateEvents() {
if state == .poweredOn { break }
}
// Find one peripheral that advertises the Heart Rate service, then stop the scan.
var target: PeripheralIdentifier?
for try await event in await central.scan(services: [HeartRateMeasurement.service]) {
if case .discovered(let discovery) = event {
target = discovery.peripheral
break
}
}
guard let identifier = target else { return }
// Connect. If the connection drops unexpectedly, BLESwift connects again.
let peripheral = try await central.connect(identifier, reconnect: .always())
// Receive heart-rate notifications. BLESwiftProfiles decodes each value.
let readings: AsyncThrowingStream<HeartRateMeasurement, Error> =
peripheral.notifications(for: HeartRateMeasurement.characteristic)
for try await reading in readings {
print("\(reading.beatsPerMinute) bpm")
}For the full example, see
Examples/HeartRateMonitor.
The DocC catalog has articles about these topics:
- Getting started
- Scanning
- Connections and reconnection
- Reads, writes, and notifications
- L2CAP channels
- The peripheral role
- Background restoration
You do not need real hardware to test code that uses Central. The BLESwiftTestSupport
product contains these fakes:
FakeCentralandFakePeripheral, which replaceCBCentralManagerandCBPeripheralFakeL2CAPChannel, for L2CAP codeFakePeripheralManager, for peripheral-role code
You control each fake from your test. To connect a real Central to a fake instead of to
CoreBluetooth, use the public
init(backend:queue:configuration:startupBackgroundTask:connectedPeripherals:):
import BLESwift
import BLESwiftCore
import BLESwiftTestSupport
import Dispatch
let queue = DispatchQueue(label: "MyAppTests.rig")
let fakeCentral = FakeCentral(queue: queue)
let central = Central(backend: fakeCentral, queue: queue)
fakeCentral.simulateStateChange(.poweredOn)
// Script the connects, reads, writes, and notifications on `fakeCentral` and a `FakePeripheral`.For the full procedure, read the "Testing Your BLE Code" article in the BLESwiftTestSupport
documentation. For a complete example, see
Examples/ConsumerTests.
That package uses the fakes from outside BLESwift, without @testable import.
CoreBluetooth does not work in the iOS Simulator. BLESwiftSimulatorLink lets your BLE code
run there without changes. It replaces the backends that Central() and PeripheralHost()
use. The new backends send each call over a localhost TCP connection to bleswift-provider,
which runs on your Mac.
import BLESwiftSimulatorLink
#if targetEnvironment(simulator)
SimulatorLink.install() // Call this before your first Central() or PeripheralHost().
#endifswift run bleswift-provider --passthrough # Also give access to the Mac's real radio.
swift run bleswift-provider --fixture hrm.json # Serve virtual devices from a JSON file.The provider has a virtual radio. The virtual radio does not need hardware, entitlements, or
approval. A PeripheralHost in a simulator becomes a device on the virtual radio, so
two simulators can communicate. Scripts/sim-to-sim-e2e.sh tests this. You can run it
locally. CI also runs it on each push to main and on each pull request from this repository.
The two simulators can also use L2CAP channels. One app publishes a PSM, and the other app
opens it.
The --passthrough option also gives access to the real radio of your Mac. For this option,
the provider build must be signed and must have the Bluetooth entitlement.
The provider also has tools. capture records real devices as a fixture document. status
shows the sessions of a running provider. install-agent runs the provider at login.
--verbose logs each link message.
For fixtures, virtual devices in code, and the differences from CoreBluetooth, read the Running in the iOS Simulator article.
To build the provider from source, run swift run bleswift-provider. A signed binary and a
Homebrew formula (brew install kylebrowning/tap/bleswift-provider) are not published yet.
- It supports the central role and the peripheral role in the simulator.
- It connects several simulators to one virtual radio at the same time.
- It serves fixtures from a JSON file or from Swift code.
- It gives central-role clients access to the real radio of your Mac with
--passthrough.
BLESwiftVirtualRadio is the same virtual radio without the provider. It runs inside your
app or test process, on all platforms. A Central can scan for, connect to, and read the
devices that you register in code. This works on a device and in the simulator. It does not
use TCP or a second process.
import BLESwiftVirtualRadio
let radio = VirtualRadio()
// Register your VirtualDevice values on `radio`.
VirtualRadio.install(radio) // Call this before your first Central() or PeripheralHost().In a unit test, you do not need the registry. Make the Central directly:
Central(backend: VirtualCentralBackend(radio: radio, queue: queue), queue: queue).
For more information, read the "Mocking Bluetooth in-process" article in the
BLESwiftVirtualRadio
documentation.
| Platform | Minimum version |
|---|---|
| iOS | 16.0 |
| macOS | 13.0 |
| watchOS | 9.0 |
| tvOS | 16.0 |
| visionOS | 1.0 |
| iOS Simulator | 16.0, with BLESwiftSimulatorLink and bleswift-provider |
The central role is available on all of these platforms. The peripheral role
(PeripheralHost) is available only on iOS and macOS, because CBPeripheralManager does not
exist on the other platforms. In the iOS Simulator, both roles use BLESwiftSimulatorLink and
bleswift-provider. For more information, see BLE in the iOS Simulator.
BLESwift uses the iOS 18 standard library. Two internal modules supply the missing parts on older systems:
BLESwiftCompat17supplies aMutexfor iOS 17, macOS 14, watchOS 10, tvOS 17, and visionOS 1.BLESwiftCompat16supplies a serial-queue executor andassumeIsolatedfor iOS 16, macOS 13, watchOS 9, and tvOS 16.
These modules are not public API.
On each commit, CI runs all tests in these configurations:
- the newest iOS simulator, with each of the two executors
- an iOS 17 simulator
- macOS, with Thread Sanitizer
CI only builds for iOS 16, because current macOS cannot run an iOS 16 simulator.
Each public API takes a DispatchQueue, and that queue must be serial. On iOS 17 and later,
a concurrent queue causes a trap. The BLESwiftExplorer sample app requires iOS 18 or
macOS 15.
Each module has a DocC catalog. The Swift Package Index hosts the catalogs:
- BLESwift:
Central, connections, scanning, GATT, L2CAP, the peripheral role, and restoration (in repo) - BLESwiftCore: the types that do not depend on a backend, the backend protocols, and the assigned numbers (in repo)
- BLESwiftProfiles: typed decoders for standard Bluetooth SIG GATT characteristics (in repo)
- BLESwiftTestSupport: the fakes and the test rig (in repo)
- BLESwiftSimulatorLink:
SimulatorLink.install()and the backends that run in the simulator (in repo) - BLESwiftVirtualRadio:
the in-process virtual radio,
VirtualRadio.install(_:), and virtual devices in code (in repo) - BLESwiftProvider: the provider that runs on the Mac and serves the virtual radio and the real radio (in repo)
- BLESwiftLink: the wire protocol and the fixture format that the simulator and the provider use (in repo)
To build the catalogs locally, run swift package generate-documentation.
Examples:
Examples/BLESwiftExplorer: a SwiftUI sample app for iOS and macOS that uses all of the public API. It shows filtered scans, connections, GATT browsing, reads and writes, notifications, RSSI, connection logs, saved devices, system connection events, ANCS, and background restoration.Examples/HeartRateMonitor: a complete app flow for the central role.Examples/ConsumerTests: a separate package that tests BLE code with the fakes, from outside BLESwift.
- Async/await API.
Centralis an actor. Connect, read, and write areasync throws. Scans and notifications areAsyncSequencestreams. - Actor isolation on the CoreBluetooth queue.
Centralruns on the same serialDispatchQueuethat itsCBCentralManageruses for delegate callbacks. Each event arrives on the executor of the actor. There is no thread hop, so events cannot arrive out of order. - Scans.
scan(services:allowDuplicates:rssiThreshold:lossTimeout:timeout:)gives.discovered,.updated, and.lostevents. You can filter by signal strength. A loss timeout reports peripherals that stop advertising. A scan timeout ends the stream. - Known peripherals.
knownPeripherals(withIdentifiers:)andsystemConnectedPeripherals(withServices:)find peripherals without a new scan. - Many connections. You can connect to many peripherals at the same time. Each connection
has its own lifecycle,
ReconnectPolicy, and GATT and notification state. - Reconnection policy.
ReconnectPolicycan be.never,.always(maxAttempts:backoff:), or a custom backoff. You do not write retry logic. - Timeouts. Connect, read, write, descriptor operations, RSSI reads, and L2CAP opens
accept an optional
Duration. When the time ends, the call throws an error. - Bluetooth SIG profiles. The
BLESwiftProfilesproduct decodes standard characteristics into typed values: Heart Rate Measurement, Battery Level, Device Information, Current Time, Body Sensor Location, CSC Measurement, Cycling Power Measurement, and Temperature Measurement. It also addsPeripheral.readDeviceInformation()andreadBatteryLevel(). - GATT discovery.
discoverServices(),discoverCharacteristics(for:),discoverDescriptors(for:), andproperties(of:)show the attributes of an unknown peripheral. - Descriptors. You can discover descriptors, and read and write them with
readDescriptor(_:timeout:)andwriteDescriptor(_:value:timeout:). - Flow control for writes without response. A
.withoutResponsewrite waits until CoreBluetooth can send it, so no packets are lost.maximumWriteValueLength(for:)gives the maximum payload for each write type. - RSSI and service changes.
readRSSI(timeout:)reads the signal strength.serviceChanges()gives a stream of the services that the peripheral changes. - Multicast streams. Bluetooth state, connection events, notifications, and restoration events each support many subscribers at the same time.
- L2CAP channels.
openL2CAPChannel(psm:timeout:)opens a channel. You receive data as anAsyncThrowingStreamand send data withwrite(_:). - Peripheral role.
PeripheralHostpublishes a GATT database and advertises it. It gives streams of read requests, write requests, and subscriber changes. It answers a failed request with anATTError. This role is available only on iOS and macOS. - Background restoration for both roles. iOS state restoration gives one stream for the central role and one stream for the peripheral role. Each stream keeps its events until the first subscriber reads them. Read the DocC article for the rules at app launch.
- Typed values.
TransmittableandReceivablegive typed reads and writes. Fixed-width integers,String,Data, andDataPaddingconform.combine(_:)joins values into one payload. - Combined operations.
writeAndAwaitNotification,writeAndAssemble, andflushdo a request and response, or a multi-packet read, in one call. - Bluetooth SIG assigned numbers.
GATTAssignedNumbersand the.nameproperty of the identifier types give a readable name for each standard UUID. - Use an existing manager.
Central(adopting:connectedPeripherals:callbackQueue:)uses aCBCentralManagerthat you already have. You can move to BLESwift one part at a time. - Tests without hardware.
BLESwiftTestSupportcontainsFakeCentral,FakePeripheral,FakeL2CAPChannel, andFakePeripheralManager. A realCentraluses them through the public backend protocols. - All CoreBluetooth platforms. BLESwift supports iOS 16, macOS 13, watchOS 9, tvOS 16, visionOS 1, and later. For more information, see Platform support.
- One runtime dependency. BLESwift depends only on
swift-log. To see the internal logs of BLESwift,
install a custom
LogHandler.
| Capability | CoreBluetooth | BLESwift |
|---|---|---|
| Scans | didDiscover callbacks. You remove duplicates, filter by RSSI, and add a timeout. |
scan(...) gives .discovered, .updated, and .lost, with rssiThreshold, allowDuplicates, lossTimeout, and timeout. |
| Connect and disconnect | connect returns nothing. The result comes later on a delegate. |
try await central.connect(id) returns a Peripheral or throws an error. |
| Reconnection | Delegate callbacks and your own retry logic. | One ReconnectPolicy for each connection: .never, .always(maxAttempts:backoff:), or .custom. |
| Reads and writes | Untyped Data for a CBCharacteristic that you must discover first. |
try await peripheral.read(from:) and write(_:to:type:), typed with Receivable and Transmittable, by identifier. |
| Timeouts | None. An operation that stops never completes. | An optional Duration on connect, read, write, descriptor operations, RSSI, and L2CAP open. |
| Writes without response | You check canSendWriteWithoutResponse, or wait for a delegate callback. |
Each write waits until CoreBluetooth is ready. maximumWriteValueLength(for:) gives the size. |
| Notifications | One delegate callback for all characteristics of the peripheral. | One typed multicast AsyncThrowingStream for each characteristic, with a BufferingPolicy. |
| Descriptors | You discover them, then find each CBDescriptor object yourself. |
DescriptorIdentifier, discoverDescriptors(for:), readDescriptor, and writeDescriptor. |
| RSSI | readRSSI(), then didReadRSSI on the delegate. |
try await peripheral.readRSSI(timeout:) |
| Service changes | didModifyServices. You discover the services again. |
serviceChanges(), a stream of the changed services. |
| L2CAP | You open the channel, then connect NSStream delegates to your own buffers. |
openL2CAPChannel(psm:) gives incomingData as a stream, and write(_:). |
| Peripheral role | CBPeripheralManager and a second delegate protocol. |
The PeripheralHost actor: add(_:), startAdvertising(_:), and streams of requests and subscribers. iOS and macOS only. |
| Background restoration | A willRestoreState dictionary that you must decode at launch. |
A typed RestoredState on a stream that keeps its events for the first subscriber, for both roles. |
| Many peripherals | One delegate gets the events of all peripherals. | One Peripheral value for each connection, with its own GATT and notification state. |
| Tests without hardware | You cannot make or control a CBCentralManager. |
FakeCentral, FakePeripheral, and FakePeripheralManager control a real Central. |
See CONTRIBUTING.md. For the release history, see CHANGELOG.md.
BLESwift uses the Apache License 2.0. See LICENSE for the full text.