Swiftpack.co - Package - FitnessKit/BluetoothMessageProtocol


Swift4 Version License Platform Readme Score codebeat badge

This is not a BLE Manager for your phone. There are plenty of those out there. BluetoothMessageProtocol provides functions for encoding and decoding Bluetooth Characteristic data.


BluetoothMessageProtocol is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'BluetoothMessageProtocol'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/FitnessKit/BluetoothMessageProtocol", from: "0.20.0")

How to Use


The Service class helps to describe a BLE Service. There is no assumption to which Characteristics the service contains.

Example Using CoreBluetooth:

centralManager.scanForPeripherals(withServices: [CBUUID(string: ServiceHeartRate.uuidString),],
                                       options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])

Supported Services

  • BLE SIG Services
  • Home Kit Accessory Protocol (HAP)
  • BLE Mesh


Each Bluetooth Characteristic has an encode and decode method. When you receive the data from a sensor you call the static decode method to turn the data into a Characteristic Object as seen below in the example:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

    if let sensorData = characteristic.value {
        if characteristic.uuid.uuidString == CharacteristicHeartRateMeasurement.uuidString {
            doDecodeHRMess(sensorData: sensorData)

        if characteristic.uuid.uuidString == CharacteristicBodySensorLocation.uuidString {
            doDecodeBody(sensorData: sensorData)

func doDecodeHRMess(sensorData: Data) {

    do {
        let hrData = try CharacteristicHeartRateMeasurement.decode(data: sensorData)

        print("HR: \(hrData.heartRate)")

    } catch  {

func doDecodeBody(sensorData: Data) {

    do {
        let hrData = try CharacteristicBodySensorLocation.decode(data: sensorData)

        print("Location: \(hrData.sensorLocation.stringValue)")

    } catch  {

Manufacturer Specific Data

Manufacturer Specific data contains a Company Assigned Number and specific data defined by the Manufacturer.

Example using Apple iBeacon:

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    if let advertData = advertisementData[CBAdvertisementDataManufacturerDataKey] as? Data {

        if let beacon = try? ManufacturerDataAppleiBeacon.decode(data: advertData) {


Manufacturer Specific Data

  • Apple iBeacon
  • AltBeacon
  • HomeKit
  • HomeKit Encrypted Notification Advertisement
  • Polar Heart Rate


This package is developed and maintained by Kevin A. Hoogheem


BluetoothMessageProtocol is available under the MIT license


Stars: 17
Help us keep the lights on


Used By

Total: 0