Building BLE (Bluetooth Low Energy) device companion applications can lead to lots of copy/paste code. We decided to provide our open source framework to help others reduce development time and avoid copy / paste snippet between projects.

Image parPublicDomainPictures de Pixabay

Overview

In this tutorial you will build a heart rate monitoring application using Bluetooth heart rate sensor. We use the Polar H10 Bluetooth Heart Rate Sensor. But any other BLE heart rate sensor should work as well as long as they are compliant with Bluetooth GATT services specifications (custom services and characteristics are also available)

Gormsson Framework

The framework is named against Harald “Bluetooth” Gormsson. King of Denmark and Norway born in 910 and died in 986.

The basics

If you want to do the same with a more low level development manner, we recommend to read Jawwad Ahmad’s tutorial available on www.raywenderlich.com.

His tutorial will give you some details on Bluetooth-specific terms such as: centrals, peripherals, services, characteristics and advertising packets.

You will also be able to evaluate the benefit of using Gormsson framework to achieve the exact same application with a lots of fewer lines of code.

Note

The iOS Simulator doesn’t support Bluetooth . You’ll need to build and run on an actual device.

Setup xCode project

Start xCode and create a Single View App then type “Next” button and name the project “HeartRateMonitor”.

Cocoapods

You can install the framework with either Cocoapods or Carthage. Let’s choose Cocoapods for this tutorial. If you don’t have it installed on your computer, open your favorite Terminal application and run the following command.

sudo gem install cocoapods

Once Cocoapods has been installed, go to your project path using your terminal and type the following pod command to setup your project workspace.

cd HeartRateMonitor/

pod init

pod install

If every things works fine, you should see the following in your terminal.

Analyzing dependencies

Downloading dependencies

Generating Pods project

Integrating client project [!] Please close any current Xcode sessions and use `HeartRateMonitor.xcworkspace` for this project from now on.

Sending stats

Pod installation complete! There are 0 dependencies from the Podfile and 0 total pods installed.

Close your xCode project and as stated above, open the heartRateMonitor.xcworkspace in xCode.

Add Gormsson Framework

Open the Podfile from the Pods project and add the Sejima framework dependency.

pod 'Gormsson'

Go back to your terminal and type the install command to make sure Gormsson framework is added to your project.

pod install Analyzing dependencies

Downloading dependencies

Installing Gormsson (0.3.0)

Generating Pods project

Integrating client project

Sending stats

Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

User interface

Simple interface with UILabel

Swift code

The code is pretty straight forward.

First we start the service on a concurrent queue named “com.ble.manager”. (note: By default main queue is used, but we do not recommend it) In viewDidLoad we start the Bluetooth devices scan by filtering only devices that provide the Heart Rate service as described in the Bluetooth GATT specifications. As soon a device is found we connect it. Gormsson framework will automatically stop the scan for other peripherals. We read the value for the Body Sensor Location characteristic and update the UI on the main thread. We also subscribe to the Heart Rate Measurement value updates using the notify method and update the UI in the main thread every time a value is given.

Summary

Compared to low level CoreBluetooth implementation, Gormsson provide a sugar syntax to read or write characteristics.

Where to go from here?

You can find a complete working project named “Heart Rate Demo” with the Gormsson framework.

The framework do not yet implement all the Bluetooth GATT services but you are welcome to help us to fill the missing services you need.