This library is a wrapper around the CoreBluetooth framework which provides a modern async API based on Combine Framework.
This package contains two versions of the library:
iOS-BLE-Library
- the library that uses the native CoreBluetooth API.iOS-BLE-Library-Mock
- the library that uses the CoreBluetoothMock API.
TLDR: Run the next command to copy files
python3 code_gen/code_gen.py Sources/
The Mock version is automatically generated from the main library using a Python-based code generation system. This ensures both versions stay perfectly synchronized without maintaining duplicate codebases.
The generation process is handled by code_gen/code_gen.py
:
python3 code_gen/code_gen.py Sources/
-
Clone Sources: The script locates
Sources/iOS-BLE-Library
and clones it intoSources/iOS-BLE-Library-Mock
, recreating the full directory tree and overwriting existing files to ensure the mock target always mirrors the latest real library sources. -
Add Mock-Specific Files: Additional files from
code_gen/additional_files/
(likeAlias.swift
) are copied into the mock tree. These provide CoreBluetoothMock typealiases that keep the public API identical between versions. -
Code Replacement: The script walks through every
.swift
file in the cloned mock target, searching for regions delimited by special markers://CG_REPLACE import CoreBluetooth //CG_WITH /* import CoreBluetoothMock */ //CG_END
It comments out the "real" CoreBluetooth block and uncomments the mock implementation block, effectively swapping CoreBluetooth imports and related code with CoreBluetoothMock equivalents.
After generation, Sources/iOS-BLE-Library-Mock
is code-identical to the original except for the swapped regions plus alias helpers, giving you a build that links against CoreBluetoothMock without hand-maintaining two diverging codebases.
When adding new functionality:
- Only modify files in
Sources/iOS-BLE-Library/
- Use the code generation markers when Core Bluetooth API usage differs between real and mock implementations
- Run the generation script to update the Mock version
- Both targets will be automatically kept in sync
The library can be installed using Swift Package Manager.
You can choose between two versions of the library:
Or you can add it as a dependency to your library:
let package = Package(
/// . . .
dependencies: [
// Set the link to the library and choose the version
.package(url: "https://github.com/NordicSemiconductor/IOS-BLE-Library.git", from: "0.3.1"),
],
targets: [
.target(
name: "MyLib",
dependencies: [
// You can use "native" CoreBluetooth API
.product(name: "iOS-BLE-Library", package: "iOS-BLE-Library")
]
),
.testTarget(
name: "MyLibTests",
dependencies: [
"MyLib",
// Or you can use the CoreBluetoothMock API
.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.