Giter Club home page Giter Club logo

bluetoothmessageprotocol's Introduction

BluetoothMessageProtocol

Swift5 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.

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

Swift4

dependencies: [
    .package(url: "https://github.com/FitnessKit/BluetoothMessageProtocol", .branch("swift42")),
]

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?) {

    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) {

    let hrData = 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 {
        let hrData = try CharacteristicHeartRateMeasurement.decode(with: sensorData).get()

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

    } catch  {
        print(error)
    }
}

func doDecodeBody(sensorData: Data) {

    let sensor = 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 {
        let sensor = try CharacteristicBodySensorLocation.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) {

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

        switch ManufacturerDataAppleiBeacon.decode(with: advertData) {
        case .success(let beacon):
            print(beacon.proximityUUID.uuidString)

        case .failure(let error):
            print(error)

        }

        /// Or you can stil use the doCatch
        if let 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

bluetoothmessageprotocol's People

Contributors

khoogheem avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.