Skip to content

Repository files navigation

BluetoothMessageProtocol

Swift5.2VersionLicensePlatformDocs

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.

Installation

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:"2.0.1")]

How to Use

Services

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

Characteristic

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?){iflet 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){lethrData:Result<CharacteristicHeartRateMeasurement,BluetoothDecodeError>=CharacteristicHeartRateMeasurement.decode(with: sensorData)switch hrData {case.success(let char):print("HR: \(char.heartRate)")case.failure(let error):print(error)}
/// Or you can stil use the doCatch
do{lethrData=tryCharacteristicHeartRateMeasurement.decode(with: sensorData).get()print("HR: \(hrData.heartRate)")}catch{print(error)}}func doDecodeBody(sensorData:Data){letsensor:Result<CharacteristicBodySensorLocation,BluetoothDecodeError>=CharacteristicBodySensorLocation.decode(with: sensorData)switch sensor {case.success(let char):print("Location: \(char.sensorLocation.stringValue)")case.failure(let error):print(error)}
/// Or you can stil use the doCatch
do{letsensor=tryCharacteristicBodySensorLocation.decode(with: sensorData).get()print("Location: \(sensor.sensorLocation.stringValue)")}catch{print(error)}}

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){iflet advertData =advertisementData[CBAdvertisementDataManufacturerDataKey]as?Data{switchManufacturerDataAppleiBeacon.decode(with: advertData){case.success(let beacon):print(beacon.proximityUUID.uuidString)case.failure(let error):print(error)}
/// Or you can stil use the doCatch
iflet beacon =try?ManufacturerDataAppleiBeacon.decode(with: advertData).get(){print(beacon.proximityUUID.uuidString)}}}

Manufacturer Specific Data

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

Author

This package is developed and maintained by Kevin A. Hoogheem

License

BluetoothMessageProtocol is available under the MIT license

About

Swift Implementation of Bluetooth Message Protocols. Encode and Decode BLE Characteristics

Topics

Resources

Stars

46 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages