Connecting iOS device to Bluetooth Low Energy (BLE) peripheral simulator

Mostafa Mazrouh
Swift2Go
Published in
4 min readJun 7, 2021

Using Light Blue app to simulate a BLE device

Source: altium

In the late 90s, Intel, Nokia and Ericson was working on different implementations for short-range wireless technology. They gathered to put a stranded tech for everyone.

Meanwhile, an engineer at Intel named Jim Kardach was reading about the vikings and he admired king Harald Bluetooth for unifying all the danish tribes, or all Scandinavia “which he didn’t really”

And so they named the new tech after the viking king.

A screenshot from this video: https://www.youtube.com/watch?v=fo-MZQ7-pBY

In 2010, Bluetooth 4 or Bluetooth Smart was adopted which included Bluetooth Low Energy or BLE for short. BLE is intended to provide considerably reduced power consumption which make it suitable for medical devices, fitness sensors, beacons and smart homes.

If you are an iOS developer and you want to communicate an iOS device to a BLE device, you will need the following:

  1. An actual iOS device like iPhone, not a simulator.
  2. And guess what? a BLE peripheral.

I think you probably have the first, but may be not a BLE peripheral.

So in this article we will use Light Blue app as a BLE simulator. You can find it for Mac, iOS and Android. We are using the Mac version here.

The good people of Raywenderlich has a good tutorial explaining how to communicate between an iOS device and a Hear Rate Sensor through BLE.

Not to reinvent the wheel, we will just use their work but we replace the actual Hear Rate Sensor with a virtual one using Blue Light app on Mac.

Now let’s start with Blue Light app, go ahead and downloaded from the store to your Mac/iOS/Android what ever you prefer. Since this is an iOS tutorial, I assume you have a Mac.

Open Light Blue, choose Virtual Devices tab. Now you see a list of devices you can play with.

Choose Heart Rate and Save.

Your simulator is one step away from being ready.

Choose the new added Hart Rate, select Body Sensor Location, edit the Hex value to be 01. This Hex value means that this sensor is worn on the Chest. Now your BLE simulator is ready.

King Harald would be proud of you.

From here you have 2 options:

  1. You can switch to the Raywenderlich tutorial to get a step by step explanation and use your virtual sensor instead of the actual sensor then come back.
  2. Or if you already know what central, peripheral, services, characteristics mean you can stick with me here and read through the marked comments in the following gist. It shows the 7 steps of how the connection works.

You can find the full project on GitHub. I am using the same project that is used in the original tutorial and I added marked comments to describe how it works from inside the code.

Open the project in XCode and see the 7 steps to establish and use the connection in code.

To establish the connection between the iOS device and the BLE peripheral we do the following:

  1. Initialize Central Manager, it represents the iOS device.
  2. Start scanning for Peripherals. We are specifically looking for peripherals with Heart Rate service.
  3. Get a reference to the peripheral, connect centralManager to heartRatePeripheral.
  4. Discover the Heart Rate Service in the Peripheral.
  5. Discover all characteristics in the Hate Rate service.
  6. Read the value of each characteristic.
  7. Display characteristics value on the screen.

In the following Gist, you can find HeartRateMonitor.swift with the 7 steps in the comments. Each step above its corresponding delegate method:

Now run the project on your iPhone and you should see the heart rate changes every while.

References:

--

--

Mostafa Mazrouh
Swift2Go

Mobile architect — write about iOS, Android and Automotive