IOS BLE Peripheral Application Using CoreBluetooth

Shahar Wie
4 min readJul 12, 2020

--

Introduction

In this short guide I will cover how to create a BLE peripheral using iOS core Bluetooth library.

The Basics

In the common Bluetooth low energy (BLE) there are 2 roles: a central and a peripheral.

Typically, the peripherals are the devices such as smart watches, heart rate monitors, smart scales, IoT devices, etc.

Peripheral is the entity that advertises the data while the Central is a server-like entity that scans and looks for peripherals.

Since this is the case, most articles and tutorial cover the central role. How to listen for peripheral and communicate with it.

There are very few tutorials explaining how to develop a peripheral.

In this article, I’ll write a simple example of a peripheral app.

Starting to code

The first step will be to create a simple app, let’s open the Xcode and select a Single View App:

Since we want our peripheral to work in the background, we have to give our app the capability of Background Modes:

And then to set it to Act as a Bluetooth LE accessory:

Now, we will create a class that wraps the CBPeripheralManager and will act as a singleton.

Let’s create a new class and name it MyPeripheralManager. It will inherit from NSObject and will implement the CBPeripheralManagerDelegate (don’t forget to import CoreBluetooth):

Now as a start we have to create an instance of CBPeripheralManager and to implement 4 methods from the CBPeripheralManagerDelegate:

peripheralManagerDidUpdateState:

will notice us for every state change from the peripheral, states are represented by the CBManagerState enum: .poweredOff, .poweredOn, resetting, unauthorized, unknown and unsupported. For now, we will deal with the .powerdOn:
Once the peripheral is powered on we would like to start advertising.
In order to advertise we need to have a service that contains characteristics. To understand, let’s assume that our service is a bulb, and it has characteristics such as brightness, color, temperature, or for example, our service is a speaker and it as a volume characteristic.
So first we need to CBMutableService and then we will need to add to it at least one CBMutableCharacteristic. The next step is to add the characteristic to the service and to start advertise it:

willRestoreState:

When the app goes to the background, even if the OS closes it, we will still be able to listen when a central connects to the phone for a read/write request.

We will have to set the peripheral delegate and to save the instance of it as a member. Also, this is the place where we can start advertising again in case the OS closed our advertising:

didReceiveRead:

Whenever the central reads a characteristic from our peripheral this function will be called.

When you implement this method, you can return the data and information (represented as Data object) back to the central that is communicating with us and to inform it about success or failure:

didReceiveWrite:

This function is called whenever the central writes data to the peripheral. Implement it to get the data from the central. You should notice the central can pass a few requests, in this example I will refer to only one request:

Almost the last thing, we need to add NSBluetoothAlwaysUsageDescription to the info.plist file:

The last step we have to do is to build a start function that will sign our class to the CBPeripheralManagerDelegate and will give out peripheral an identifier for the restoration when the OS closes us:

Now all that is left is to call the start function from our ViewController and to handle the launch Options in the AppDelegate class:

Don’t forget to add the start call to you viewDidLoad function in your ViewController:

Run the app, and run nRF Connect app on a different device, start scan in the nRF Connect and you should see our app advertising “max8Chars”.

Conclusion

In this little sample, we’ve created a BLE peripheral — a device that transmits BLE signals that can be connected by a central even when the app is running in the background.

--

--