NordicSemiconductor / IOS-CoreBluetooth-Mock

Mocking library for CoreBluetooth framework.
BSD 3-Clause "New" or "Revised" License
225 stars 51 forks source link
ble bluetooth-low-energy corebluetooth mock mocking-framework nrf5x

Core Bluetooth Mock

Version number Platform Carthage compatible SPM compatible

The Core Bluetooth Mock library was designed to emulate Core Bluetooth objects, providing easy way to test Bluetooth-enabled apps. As the native Bluetooth API is not supported on a simulator, using this library you can run, test and take screenshots of such apps without the need of a physical phone or tablet. You may also start working on the iOS app when your peripheral is still under development.

Core Bluetooth?

The Core Bluetooth framework provides the classes needed for your apps to communicate with Bluetooth-equipped low energy (LE) wireless technology. It requires an iPhone or iPad to work making Bluetooth-enabled apps difficult to test. As the documentation states:

Don’t subclass any of the classes of the Core Bluetooth framework. Overriding these classes isn’t supported and results in undefined behavior.

Core Bluetooth Mock!

The Core Bluetooth Mock library defines a number of CBM... classes and constants, that wrap or imitate the corresponding CB... counterparts from Core Bluetooth framework. For example, CBMCentralManager has the same API and behavior as CBCentralManager, etc. On physical iDevices all calls to CBMCentralManager and CBMPeripheral are forwarded to their native equivalents, but on a simulator a user defined mock implementation is used.

Requirements

The Core Bluetooth Mock library is available only in Swift, and compatible with

(with some features available only on newer platforms).

[!Note] For projects running Objective-C we recommend https://github.com/Rightpoint/RZBluetooth library.

[^1]: Xcode 15 dropped support for iOS 9.0 and tvOS 9.0 in simulator. Now the minimum supported version is 12.0 for both platforms.

Installation

The library support CocoaPods, Carthage and Swift Package Manager.

CocoaPods - Create/Update your **Podfile** with the following contents ```ruby target 'YourAppTargetName' do pod 'CoreBluetoothMock' end ``` - Install dependencies ```bash pod install ``` - Open the newly created `.xcworkspace`
Carthage - Create a new **Cartfile** in your project's root with the following contents ``` github "https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock" ~> x.y // Replace x.y with your required version ``` - Build with carthage ```bash carthage update --platform iOS // also supported are tvOS, watchOS and macOS ``` - Copy the **CoreBluetoothMock.framework** from *Carthage/Build* to your project and follow [instructions from Carthage](https://github.com/Carthage/Carthage).
Swift Package Manager - In Xcode: *File -> Swift Packages -> Add package dependency*, type *https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock.git* and set required version, branch or commit. - If you have *Swift.package* file, include the following dependency: ```swift dependencies: [ // [...] .package(name: "CoreBluetoothMock", url: "https://github.com/NordicSemiconductor/IOS-CoreBluetooth-Mock.git", .upToNextMajor(from: "x.y")) // Replace x.y with your required version ] ``` and add it to your target: ```swift targets: [ // [...] .target( name: "", dependencies: ["CoreBluetoothMock"]), ] ```

Documentation

The documentation of the library is available here.

Migration from CoreBluetooth

Migration example is available here. See Pull Request #1 for step-by-step guide.

[!Note] The migration example application currently does not use mocks in tests. For that, check out the Example folder in this repository with nRF Blinky app, which is using mock peripherals in Unit Tests and UI Tests. See below.

Sample application: nRF Blinky

nRF Blinky is an example app targeted towards newcomer BLE developers, and also demonstrating the use of Core Bluetooth Mock library. This application controls an LED on an nRF5DK and receive notifications whenever the button on the kit is pressed and released.

The mock implementation is used in Unit tests and UI tests. See AppDelegate.swift where the mock environment is set up and and UITests.swift and UITests.swift classes.

The mock peripherals are defined in MockPeripherals.swift.

Nordic LED and Button Service

A simplified proprietary service by Nordic Semiconductor, containing two characteristics one to control LED3 and Button1:

Requirements

Installation and usage