SwiftyBluetooth
Closures based APIs for CoreBluetooth.
Features
- Replace the delegate based interface with a closure based interface for every
CBCentralManager
andCBPeripheral
operation. - Notification based event for CBCentralManager state changes and state restoration.
- Notification based event for CBPeripheral name updates, characteristic value updates and services updates.
- Precise errors and guaranteed timeout for every Bluetooth operation.
- Will automatically connect to a CBPeripheral and attempt to discover the required BLE services and characteristics required for a read or write operation if necessary.
Usage
The Library has 2 important class:
- The
Central
class, a Singleton wrapper aroundCBCentralManager
used to scan for peripherals with a closure callback and restore previous sessions. - The
Peripheral
class, a wrapper aroundCBPeripheral
used to callCBPeripheral
functions with closure callbacks.
Below are a couple examples of operations that might be of interest to you.
Scanning for Peripherals
Scan for peripherals by calling scanWithTimeout(...)
while passing a timeout
in seconds and a callback
closure to receive Peripheral
result callbacks as well as update on the status of your scan:
Note that the callback closure can be called multiple times, but always start and finish with a callback containing a .scanStarted
and .scanStopped
result respectively. Your callback will be called with a .scanResult
for every unique peripheral found during the scan.
Connecting to a peripheral
Reading from a peripheral's service's characteristic
If you already know the characteristic and service UUIDs you want to read from, once a peripheral has been found you can read from it right away like this:
This will connect to the peripheral if necessary and ensure that the characteristic and service needed are discovered before reading from the characteristic matching characteristicUUID
. If the charac/service cannot be retrieved you will receive an error specifying which charac/service could not be found.
If you have a reference to a CBCharacteristic
, you can read using the characteristic directly:
Writing to a Peripheral's service's characteristic
If you already know the characteristic and service UUID you want to write to, once a peripheral has been found, you can write to that characteristic right away like this:
Receiving Characteristic update notifications
Receiving characteristic value updates is done through notifications on the default NotificationCenter
. All supported Peripheral
notifications are part of the PeripheralEvent
enum. Use this enum's raw values as the notification string when registering for notifications:
Discovering services
Discover services using the discoverServices(...)
function:
Like the CBPeripheral discoverServices(...) function, passing nil instead of an array of service UUIDs will discover all of this Peripheral's services.
Discovering characteristics
Discover characteristics using the discoverCharacteristics(...)
function. If the service on which you are attempting to discover characteristics from has not been discovered, an attempt will first be made to discover that service for you:
Like the CBPeripheral discoverCharacteristics(...) function, passing nil instead of an array of service UUIDs will discover all of this service's characteristics.
State preservation
SwiftyBluetooth is backed by a CBCentralManager singleton wrapper and does not give you direct access to the underlying CBCentralManager.
But, you can still setup the underlying CBCentralManager for state restoration by calling setSharedCentralInstanceWith(restoreIdentifier: )
and use the restoreIdentifier of your choice.
Take note that this method can only be called once and must be called before anything else in the library otherwise the Central sharedInstance will be lazily initiated the first time you access it.
As such, it is recommended to call it in your App Delegate's didFinishLaunchingWithOptions(:)
Register for state preservation notifications on the default NotificationCenter. Those notifications will contain an array of restored Peripheral
.
Installation
CocoaPods
Add this to your Podfile:
Swift Package Manager
Simply add the library to your xcode project as a "Package Dependency"
Carthage
Add this to your Cartfile
Requirements
SwiftyBluetooth requires iOS 10.0+
License
SwiftyBluetooth is released under the MIT License.