This library is a wrapper around the CoreBluetooth framework which provides a modern async API based on Combine.
The package ships two products, and consumers pick the one that fits their needs:
iOS-BLE-Library— links realCoreBluetooth. For production apps.iOS-BLE-Library-Mock— linksCoreBluetoothMock. The public API is identical (a top-levelAlias.swiftre-exportsCB*names for the underlyingCBM*types), so code written foriOS-BLE-Libraryrecompiles unchanged againstiOS-BLE-Library-Mockfor unit testing.
Both products are built from a single source tree at Sources/iOS-BLE-Library/. The Mock target's compilation unit is produced at build time by the MockGenerator SwiftPM build plugin — there are no Python scripts, no committed duplicates, no manual sync step.
At the handful of sites where the two products diverge (mostly imports, plus a couple of init-time branches), the source uses native Swift conditional compilation:
#if MOCK_TRANSPORT
import CoreBluetoothMock
#else
import CoreBluetooth
#endifThe Mock target sets swiftSettings: [.define("MOCK_TRANSPORT")]; the native target leaves the flag undefined. The compiler picks the right branch per build.
- Edit files only in
Sources/iOS-BLE-Library/. Do not edit anything underSources/iOS-BLE-Library-Mock/(other thanAlias.swiftandDocumentation.docc/, which are static). - For code that needs to behave differently in the Mock build, wrap it in
#if MOCK_TRANSPORT … #else … #endif. - Run
swift build— the plugin re-generates the Mock target's sources automatically.
That's the whole workflow. No code_gen step, no marker DSL.
Add the package to your Package.swift dependencies and pick the product you need:
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/nordicsemi/IOS-BLE-Library.git", from: "1.0.0"),
],
targets: [
.target(
name: "MyApp",
dependencies: [
// Production: links real CoreBluetooth
.product(name: "iOS-BLE-Library", package: "IOS-BLE-Library")
]
),
.testTarget(
name: "MyAppTests",
dependencies: [
"MyApp",
// Testing: links CoreBluetoothMock
.product(name: "iOS-BLE-Library-Mock", package: "IOS-BLE-Library")
]
),
]
)Please check the Documentation Page to start using the library.
Also you can check iOS-nRF-Toolbox to find more examples.
Please consider backing this project by using the following GitHub Sponsor button.
We want to thank all of our contributors for all of their additions and improvements to this project. With special mention to one in particular: Nick!