https://interrupt.memfault.com/blog/bluetooth-low-energy-a-primer
Introduction
You’ve probably heard the term IoT (Internet of Things) many times, and seen the hype in the media surrounding it. Simply stated, IoT refers to connecting devices to each other and to the Internet. IoT often depends on wireless technologies, which have been used to add connectivity to a slew of devices that were never connected before (even flip-flops!).
In today’s post, we will be covering one of the most popular low-power IoT technologies: Bluetooth Low Energy (BLE). We will give an overview of how BLE works, dig into each protocol layer, and walk you through an example setting up BLE on a Nordic nRF52 microcontroller.
Table of Contents
Basics of Bluetooth Low Energy (BLE)
Bluetooth Low Energy (BLE) is a low power wireless technology used for connecting devices with each other. BLE operates in the 2.4 GHz ISM (Industrial, Scientific, and Medical) band, and is targeted towards applications that need to consume less power and may need to run on batteries for longer periods of time — months, and even years.
Bluetooth started as a short-distance cable replacement technology. For example, to replace wires in devices such as a mouse, keyboard, or a PC communicating with a Personal Digital Assistant (PDA) which were popular in the late 1990s and early 2000s. The first official version of Bluetooth was released by Ericsson in 1994, named after King Harald “Bluetooth” Gormsson of Denmark who helped unify warring factions in the 10th century CE.
Bluetooth Low Energy (BLE) was introduced in the 4.0 version of the Bluetooth specification in 2010. The original Bluetooth defined in the previous versions is referred to as Bluetooth Classic. BLE was not an upgrade to Bluetooth Classic, but rather it’s a new technology that utilizes the Bluetooth brand but focuses on the Internet of Things (IoT) applications where small amounts of data are transferred at lower speeds. It’s important to note that there’s a big difference between Bluetooth Classic and Bluetooth Low Energy in terms of technical specification, implementation and the types of applications they’re each suitable for.
Bluetooth Classic vs. Bluetooth Low Energy
Itʼs important to note that thereʼs a big difference between Bluetooth Classic and Bluetooth Low Energy in terms of technical specification, implementation, and the types of applications to which theyʼre each suited. This is in addition to the fact that they are incompatible with each other.
Some of the notable differences are summarized in the following table:
Bluetooth Classic | BLE |
---|---|
Used for streaming applications such as audio streaming, file transfers, and headsets | Used for sensor data, control of devices, and low-bandwidth applications |
Not optimized for low power, but has a higher data rate (3Mbps maximum compared to 2Mbps for BLE) | Meant for low power, low duty data cycles |
Operates over 79 RF (radio frequency) channels | Operates over 40 RF channels. |
Discovery occurs on 32 channels | Discovery occurs on 3 channels, leading to quicker discovery and connections than Bluetooth Classic |
BLE has gone through some major revisions and changes in the short time since its official release in 2010, with the most recent major updates being Bluetooth 5 released in December 2016 and Bluetooth 5.1 released in January 2019.
Bluetooth 5 introduced many important upgrades to the Bluetooth specification, most of which were focused on BLE. Some of the most important enhancements include twice the speed, four times the range, and eight times the advertising data capacity.
On the other hand, Bluetooth 5.1 focused on direction finding by utilizing Angle of Arrival (AoA) and Angle of Departure (AoD) techniques.
Benefits and Drawbacks of BLE
Every technology has its own benefits and limitations, and BLE is no exception. It’s important to know these pros and cons to be able to determine whether BLE is suitable for your specific application and use case or not.
Benefits
- Lower power consumption even when compared to other low power technologies. This is achived by keeping the radio off as much as possible and sending small amounts of data at low transfer speeds.
- No cost to access the official specification documents. With most other wireless protocols and technologies, you would have to be a member of the official group or consortium for that technology in order to access the specification.
- Lower cost of modules and chipsets, even when compared to other similar technologies.
- Most importantly, its existence in most smartphones in the market.
Drawbacks and limitations
- Data Throughput: the data throughput of BLE is limited by the physical radio layer (PHY) data rate, which is the rate at which the radio transmits data. This rate depends on the Bluetooth version used. For Bluetooth 4.2 and earlier, the rate is fixed at 1 Mbps. For Bluetooth 5 and later, however, the rate varies depending on the mode and PHY used. The rate can be 1 Mbps like earlier versions, or 2 Mbps when utilizing the high-speed feature.
- Range: Bluetooth Low Energy (and Bluetooth in general) was designed for short-range applications and hence its range of operation is limited. There are a few factors that affect the range of BLE:
- BLE operates in the 2.4 GHz ISM spectrum which is greatly affected by obstacles that exist all around us such as metal objects, walls, and water (especially human bodies).
- Performance and design of the antenna of the BLE device.
- Physical enclosure and orientation of the device.
- Gateway Requirement for Internet Connectivity: In order to transfer data from a BLE-only device to the Internet, another BLE device that has an IP connection is needed to receive this data and then, in turn, relay it to another IP device (or to the Internet).
Bluetooth Low Energy Architecture
Here’s a diagram showing the different levels of the architecture of BLE:
The good thing is that, as a developer looking to develop BLE applications, you won’t have to worry much about the layers below the Security Manager and Attribute Protocol. But lets at least cover the definitions of these layers:
The Physical Layer (PHY) refers to the physical radio used for communication and for modulating/demodulating the data. It operates in the ISM band (2.4 GHz spectrum).
The Link Layer is the layer that interfaces with the Physical Layer (Radio) and provides the higher levels an abstraction and a way to interact with the radio (through an intermediary level called the HCI layer which we’ll discuss shortly). It is responsible for managing the state of the radio as well as the timing requirements for adhering to the Bluetooth Low Energy specification.
Direct Test Mode: the purpose of this mode is to test the operation of the radio at the physical level (such as transmission power, receiver sensitivity, etc.).
The Host Controller Interface (HCI) layer is a standard protocol defined by the Bluetooth specification that allows the Host layer to communicate with the Controller layer. These layers could exist on separate chips, or they could exist on the same chip.
The Logical Link Control and Adaptation Protocol (L2CAP) layer acts as a protocol multiplexing layer. It takes multiple protocols from the upper layers and places them in standard BLE packets that are passed down to the lower layers beneath it.
The Security Manager defines the protocols and algorithms for generating and exchanging keys between two devices. It involves five security features: pairing, bonding, authentication, encryption, and message integrity.
The Attribute Protocol (ATT) defines how a server exposes its data to a client and how this data is structured. There are two roles within the ATT:
- Server: the device that exposes the data it controls or contains, and possibly some other aspects of server behavior that other devices may be able to control.
- Client: the device that interfaces with the server with the purpose of reading the server’s exposed data and/or controlling the server’s behavior.
GAP, Advertising, Scanning, and Connections
GAP stands for Generic Access Profile. It provides a framework that defines how BLE devices interact with each other. This includes:
- Roles of BLE devices
- Advertisements (Broadcasting, Discovery, Advertisement parameters, Advertisement data)
- Connection establishment (initiating connections, accepting connections, Connection parameters)
- Security
The different roles of a BLE device are:
- Broadcaster: a device that sends out Advertisements and does not receive packets or allow Connections from others.
- Observer: a device that listens to others sending out Advertising Packets, but does not initiate a Connection with the Advertising device.
- Central: a device that discovers and listens to other devices that are Advertising. A Central also has the capability of connecting to an Advertising device.
- Peripheral: a device that Advertises and accepts Connections from Central devices.
Keep in mind that a single device may operate in multiple Roles at the same time. For example, your smartphone can operate in the Central role when communicating with your smartwatch, and also act in the Peripheral role while communicating with a PC.
Advertising & Scanning
In the Advertising state a device sends out packets containing useful data for others to receive and process. The packets are sent at a fixed interval defined as the Advertising Interval. There are 40 RF channels in BLE, each separated by 2 MHz (center-to-center), as shown in the following figure. Three of these channels are called the Primary Advertising Channels, while the remaining 37 channels are used for Secondary Advertisements and for Data Packet transfer during a Connection.
Advertisements always start with Advertisement Packets sent on the 3 Primary Advertising Channels (or a subset of these Channels). This allows Centrals to find the Advertising device (Peripheral or Broadcaster) and parse its Advertising packets. The Central can then Initiate a Connection if the Advertiser allows it (Peripheral devices).
Scanning
Centrals tune into the three Primary Advertising Channels one at a time. So, in order for a Central to discover a Peripheral, the Central has to be tuned into the same channel that the Peripheral is Advertising on at that given point. To increase the possibility of this happening, and in order to make it happen quickly, the different advertising and scanning parameters can be adjusted.
Connections
In order for two BLE devices to connect to each other, the following needs to happen:
- The Peripheral needs to start Advertising and send out Connectable Advertisement packets.
- The Central device needs to be Scanning for Advertisements while the Peripheral is Advertising.
- If the Central happens to be listening on an Advertising Channel that the Peripheral is Advertising on, then the Central device discovers the Peripheral and is able to read the Advertisement packet and all the necessary information in order to establish a Connection.
- The Central then sends a Connection Request packet.
- The peripheral always listens for a short interval on the same Advertising Channel after it sends out the Advertising packet. This allows it to receive the Connection Request packet from the Central device — which triggers the forming of the Connection between the two devices.
After that, the Connection is considered “created” but not yet “established”. A Connection is considered “established ” once the device receives a packet from its peer device. After a Connection becomes established, the Central becomes known as the Master, and the Peripheral becomes known as the Slave. The Master is responsible for managing the Connection, controlling the Connection Parameters and the timing of the different events within a Connection.
Connection Events
During a Connection Event, the Master and Slave alternate sending data packets to each other until neither side has data to send. Here are a few aspects of Connections that are very important to know:
- A Connection Event contains at least one packet sent by the Master. The Slave always sends a packet back if it received a packet from the Master.
- If the Master does not receive a packet back from the Slave, the Master will close the Connection Event — it resumes sending packets at the next Connection Event.
- The Connection Event can be closed by either side.
- The starting points of consecutive Connection Events are spaced by a period of time called the Connection Interval.
Connection Parameters
The most important parameters that define a Connection include:
- Connection Interval: the interval at which two connected BLE devices wake up the radio and exchange data (at each Connection Event).
- Slave Latency: this value allows the Peripheral to skip a number of consecutive Connection Events and not listen to the Central at these Connection Events without compromising the Connection. For example, a Slave Latency of 3 allows a Slave to skip 3 Connection Events and a value of 0 means that the Slave has to send data to the Master at every Connection Event.
- Supervision Timeout: the maximum time between two received data packets before the Connection is considered lost.
GATT, Services, and Characteristics
The main reason to connect two BLE devices to each other is to transfer data between them. Without a Connection, it is not possible to have a bidirectional data transfer between two BLE devices.
Which brings us to the concept of GATT…
The Generic Attribute Profile (GATT) defines the format of the data exposed by a BLE device. It also defines the procedures needed to access the data exposed by a device.
There are two Roles within GATT: Server and Client. The Server is the device that exposes the data it controls or contains, and possibly some other aspects of its behavior that other devices may be able to control. A Client, on the other hand, is the device that interfaces with the Server with the purpose of reading the Server’s exposed data and/or controlling the Server’s behavior.
Keep in mind that a BLE device can act as both a Server and a Client at the same time. Simply put, it acts as the Server for the sake of exposing its own data, and as a Client when accessing another device’s data.
Services and Characteristics
Services and Characteristics are probably the two most used terms in BLE! That’s why understanding them is crucial, especially for BLE devices that establish a connection with each other.
To better understand GATT, we need to cover a few important concepts (including Services and Characteristics):
- Attributes: a generic term for any type of data exposed by the Server and defines the structure of this data. For example, Services and Characteristics (both defined below) are types of Attributes.
- Services: a grouping of one or more Attributes (some of which are Characteristics). It’s meant to group together related Attributes that satisfy a specific functionality on the Server. For example, the SIG-adopted Battery Service contains one Characteristic called the Battery Level.
- Characteristics: a Characteristic is always part of a Service and it represents a piece of information/data that a Server wants to expose to a client. For example, the Battery Level Characteristic represents the remaining power level of a battery in a device which can be read by a Client.
- Profiles: Profiles are much broader in definition from Services. They are concerned with defining the behavior of both the Client and Server when it comes to Services, Characteristics and even Connections and security requirements. Services and their specifications, on the other hand, deal with the implementation of these Services and Characteristics on the Server side only.
A simplified representation of a Service:
In BLE, there are six types of operations on Characteristics:
- Commands: sent by the Client to the Server and do not require a Response (defined below). One example of a Command is a Write Command, which does not require a Response from the Server.
- Requests: sent by the Client to the Server and require a Response. Some examples of Requests include: Read Requests and Write Requests.
- Responses: sent by the Server in response to a Request.
- Notifications: sent by the Server to the Client to let the Client know that a specific Characteristic Value has changed. In order for this to be triggered and sent by the Server, the Client has to enable Notifications for the Characteristic of interest. Note that a Notification does not require a Response from the Client to acknowledge its receipt.
Indications: sent by the Server to the Client. They are very similar to Notifications but require an acknowledgment to be sent back from the Client to let the Server know that the Indication was successfully received.
Note: Notifications and Indications are exposed via the Client Characteristic Configuration Descriptor (CCCD) Attribute. Writing a “1” to this Attribute value enables Notifications, whereas writing a “2” enables Indications. Writing a “0” disables both Notifications and Indications.
- Confirmations: sent by the Client to the Server. These are the acknowledgment packets sent back to the Server to let it know that the Client received an Indication successfully.
Now that we’ve gone through the basics of BLE, we can take that knowledge and apply it to practice. In the next section, we will implement a simple BLE Peripheral running on the nRF52840 development kit.
Implementation of a BLE Peripheral (nRF52 example)
Rather than relying on an IDE, we’ll be using the command line for almost everything, and all steps should work pretty much on any Linux or macOS machine.
Hardware required:
Here’s a list of the hardware components required for following the steps in this tutorial:
- An nRF52840 development kit (PCA10056).
- A macOS computer (though most of these steps are transferrable to Linux and Windows).
- A smartphone that supports BLE (if your phone was manufactured in the past 3 years or so, it most likely supports BLE).
- A micro-USB cable for connecting the development kit to your computer.
Software required:
Here’s a list of the software packages required for following the steps in this tutorial:
- Nordic Semiconductor’s nRF5 SDK (we’ll be using SDK version 15.3.0).
- Nordic Semiconductor’s nRF5 Command Line Tools for macOS (we’ll be using version 9.8.1).
- SEGGER J-Link Software for macOS (we’ll be using version V6.44g).
- GNU Arm Embedded Toolchain (we’ll be using version 7-2017-q4-major which is recommended by Nordic per this article).
- CoolTerm serial terminal program (or any other serial terminal program).
- Xcode Command Line Tools — For installation steps, I found this tutorial quite helpful and easy to follow.
- A BLE Central mobile app such as nRF Connect (iOS or Android) or LightBlue (iOS or Android)
Setup
Let’s go through the different steps for downloading the necessary software packages.
For each of these downloads, you can place them anywhere on your machine. I recommend having them all in a single folder, to make it easier to locate later on.
If you’ve read and followed the steps in our previous post Debugging Firmware with GDB, you would already have all the necessary software packages installed and set up (and you could safely skip the installation section).
1. nRF5 SDK
Installation of the nRF5 SDK is straightforward. All you need to do is download the SDK tarball from Nordic’s website and then extract it.
Once you extract it, you should see the following directory structure:
2. nRF5 Command Line Tools
Next, we need to install the nRF5 Command Line Tools. These include nrfjprog, which is a tool for programming your nRF52 development kit via the Segger J-Link debugger and needed in our case for working from the command line.
Link to download the nRF5 Command Line Tools
First, make sure you select the appropriate operating system (macOS in our case).
Select the latest version, and then click “Download File”.
This is what the contents of the folder should look like:
3. SEGGER J-Link
The SEGGER J-Link software is needed for the GDB Server interface to the nRF52 chipset on the development kit.
So let’s go ahead and download the software.
Link to download SEGGER J-Link Software
This download (for macOS) is a .pkg installer file. Once you download it, simply double-click it and go through the installation process.
4. GNU Arm Embedded Toolchain
The next software package that we need to install is the GNU Arm Embedded Toolchain which includes the compiler (gcc) and debugger (gdb) for the Arm architecture (which the nRF52840 chipset is based on).
Direct link to download the GNU Arm Embedded Toolchain (version 7-2017-q4-major)
After you download the package, simply extract it to your folder of choice.
5. Serial Terminal Program
My choice for a terminal program that I use across projects is a program called CoolTerm. What I like about this program is its simplicity and that it supports all the major platforms (Windows, macOS, and Linux).
For now, all you need to do is download it, open the package and copy the application file to your Mac Applications folder so you could launch it in the later steps.
6. Adding the Necessary Folders to $PATH
In order to access the necessary commands that we’ll be calling from anywhere in your system, you need to add their paths to the system $PATH environment variable.
To do that, open up a Terminal session and type the following:
Once you’ve done that, go ahead and add the following lines to the bottom of the file:
Make sure to replace <Your Folder>
with the appropriate folder name where you placed those packages on your system.
We don’t need to add any paths for the SEGGER J-Link software because it ran as an installer and would have already added the necessary binaries to our system.
After you’re done editing and saving the paths
file, close the Terminal and then start a new session to make sure the updated paths
file gets loaded. Alternatively, you could just execute the following command to reload .bash_profile
:
To verify, you can type the following command:
That’s it for the setup part, now on to the fun part!
Developing the nRF52 BLE Peripheral Application
We will be using one of the BLE examples provided as part of the nRF5 SDK as a starting point. The example is called “ble_app_template” and is located at:
The example is provided as a boilerplate template that you could use whenever you’re developing an nRF52 BLE application from scratch. Here’s a screenshot of the contents of this example’s folder:
The example consists of a single source file named main.c
, which we’ll be modifying to do the following:
- Enable advertising as a BLE Peripheral that accepts a connection from a BLE Central device.
- Define a GATT service which we’ll call “simple service” which consists of one characteristic:
- A “Button 1” Characteristic (exposes the current state of the development kit’s Button 1: pressed (0x01) or released (0x00)). The Characteristic will allow both reads and notifications to allow a client to read the value and get notified only when the value changes (instead of polling for the value waiting for a change to occur).
- Handling of the Button 1 events (presses and releases).
Let’s go through the steps and code changes needed to implement our application. All the code used in this blog post is available on GitHub.
Enabling the nRF Log Module
In order to enable logging and see debug data, you need to do the following (some of these may already be enabled in your template application) in pca10056/s140/config/sdk_config.h
:
Enable the
NRF_LOG_ENABLED
macro:Set the log level to 3 (or 4 more debugging information):
Since we’ll be using a terminal program (such as CoolTerm), we need to enable logging via the UART backend. This is done by enabling the
NRF_LOG_BACKEND_UART_ENABLED
as follows:
That’s it! Now you have logging enabled and you will be able to see log messages show up in your terminal program of choice. We’ll get to configuring the terminal program in the “Testing” section.
Handling the Button Presses
The application already comes ready with code that sets up the LEDs and Buttons to be used. However, we’ll be modifying it to utilize an nRF module called the “App Button” module. This module simplifies the code for handling button events compared to the originally included BSP library.
You can find more about the App Button library here:
nRF5 SDK Button Handler Library
We will need to add three different segments of code to main.c
to enable handling of Button 1 events:
Configure the button we’ll be using (Button 1 on the dev kit). We also add a forward declaration of the function responsible for handling the button events:
The button event handler function. The function handles the Button 1 press and release events, and then calls a function to update our characteristic value that’s associated with the button.
The function to initialize and configure the LEDs and Buttons. We will change the following code:
to
Notice we removed the initialization of the buttons from the
bsp_init()
call and instead used theapp_button_init()
API. We also pass the buttons array to the API to configure only Button 1.
Those are all the changes needed to handle the Button 1 press and release events.
Initializing GATT Services and Characteristics
The boilerplate example provides example code to initialize your own Service (which in turn initializes its Characteristics) in the following function in main.c
:
Let’s modify this function to initialize our own service “simple_service” which we’ll provide the code for in the next section.
Another change we need to make to main.c
is to include our Service header file simple_service.h
. We do this towards the top of main.c
:
We also need to instantiate a variable for our Simple Service. This is done by adding the following code:
The final change we want to make in main.c
is to change the advertised device name. By default, it’s set to "Nordic_Template"
. Let’s change it to the following:
That’s it for main.c
! The source code for main.c
is available on GitHub.
Implementation of the GATT Services and Characteristics
For implementation of our Simple Service we will create two source files: simple_service.h
and simple_service.c
and place them in the same folder as main.c
.
GATT Service and Characteristic UUIDs
Before we go through the implementation of each of these, we need to assign the GATT Service and Characteristic their own custom UUIDs (a 128-bit value), which we can simply autogenerate using an online tool called Online GUID Generator.
“A UUID is a universally unique identifier that is guaranteed to be unique across all space and all time” (Bluetooth 5.1 spec, Vol 3, Part B, section 2.5.1 UUID).
The one restriction for use of a UUID for a custom BLE Service or Characteristic is that it does not overlap with the Bluetooth SIG Adopted UUIDs. Adopted UUIDs are represented with 16-bit value and its equivalent 128-bit value is computed as follows:
128-bit value = 16-bit-value * 2^96 + BluetoothBaseUUID
where the BluetoothBaseUUID is 00000000-0000-1000-8000-00805F9B34FB
Examples include:
0x180F -> Battery Service UUID (128-bit UUID: 0000180F-0000-1000-8000-00805F9B34FB)
0x2A00 -> Device Name Characteristic UUID (128-bit UUID: 00002A00-0000-1000-8000-00805F9B34FB)
The digits in bold represent the 16-bit value.
We can simply generate one UUID for the Service and increment it for the Characteristic, and as long as it doesn’t use the same “base” as the SIG Adopted UUIDs, we’ll be good!
So, we’ll use:
Simple Service UUID: E54B0001-67F5-479E-8711-B3B99198CE6C
Button 1 Characteristic UUID: E54B0002-67F5-479E-8711-B3B99198CE6C (simply increment the bold digits)
Now on to developing the code for our GATT Service and Characteristic.
simple_service.h
The simple_service.h
file serves as the header file for our Simple Service. We’ll cover the most important parts of the code (the full source code is available on GitHub for your reference).
First, we’ll define the macro that gets used in main.c
to instantiate a Simple Service object:
This is code that you’ll find used in many of the Nordic examples, and we simply customized it to our needs.
Next, we’ll define the “base” UUID to use along with the 16-bit definitions for each of the Simple Service and the Button 1 Characteristic. The following method is the common method of defining custom (also referred to as vendor-specific) Services and Characteristics in Nordic nRF applications.
Following that we define the data structure for the Simple Service:
Next, we define the events for Notifications getting enabled or disabled for the Button 1 Characteristic:
In the following code we define the function prototype for the BLE event handler:
We then define the actual structure of the Simple Service which holds the crucial information needed by the Service:
Finally, we declare the APIs exposed by the Simple Service. This includes functions for: initialization, the BLE event handler (which is needed by the Nordic SoftDevice), and the function for updating the Characteristic value (called when Button 1 is pressed or released:
That’s the end of the header file. Next, we’ll move on to the source file implementation.
simple_service.c
Let’s go over the most important parts of simple_service.c
.
A couple of global variables needed for storing the Button 1 Characteristic name and one for tracking whether Notifications are enabled or not.
A function for handling the connection event. In this function, we store the connection handle and make sure the
button_notifications_enabled
boolean is disabled.A function for handling the disconnection event. In this function, we invalidate the connection handle and make sure the
button_notifications_enabled
boolean is disabled.A function for handling the Characteristic Write event. In this function, we handle writing to the CCCD (Client Characteristic Configuration Descriptor) which is used for enabling or disabling Notifications. The event is then passed up to the application layer via the application event handler that’s set during the initialization of the Service.
- A function for adding the Button 1 Characteristic to the BLE stack. This function handles:
- Setting the CCCD permissions to allow subscribing to Notifications.
- Setting the Characteristic value permissions to allow read only (no write permissions).
- Setting the Characteristic name.
- Setting the UUID for the Characteristic.
- Defining the size of the Characteristic value (1 byte: 0x00 for released state and 0x01 for pressed state).
- Finally, calling the
sd_ble_gatts_characteristic_add()
API to add the Characteristic.
- A function to initialize the Service. It handles the following:
- Assigning the application level event handler (if passed in).
- Assigning the Service UUID and adding it to the BLE stack.
- Adding the Service to the BLE stack.
- Finally, adding the Button 1 Characteristic to the Service.
A function to handle the different BLE events reported by the SoftDevice. The three events handled are: the connected event, disconnected event, and the Characteristic write event.
- Finally, a function to handle updates to the Button 1 Characteristic value. This function handles:
- Setting the corresponding value to the Characteristic based on whether a Button 1 press or release event has occurred. This gets called by the application layer (in
main.c
). - Sending a Notification along with the updated value to the Client if it has enabled Notifications.
- Setting the corresponding value to the Characteristic based on whether a Button 1 press or release event has occurred. This gets called by the application layer (in
Custom (Vendor-Specific) Services and Characteristics in nRF Applications
Before we can build the application, there’s one last important change that we need to make. This change will be in the sdk_config.h
file (located at ble_app_template/pca10056/s140/config/sdk_config.h
).
We need to modify a macro responsible for telling the SoftDevice (Nordic’s BLE stack) about the number of vendor-specific UUIDs used in the application. This change is needed since it requires some (extra) dedicated memory for storing these UUIDs. We only need to increment this macro for each “base” UUID, basically for each time the sd_ble_uuid_vs_add()
API is called. So, in our case we only need to increment it by 1 (since the Service and Characteristic share the same “base”).
Look for the macro named NRF_SDH_BLE_VS_UUID_COUNT
in sdk_config.h
and change its value from 0 to 1:
Along with this change, we will need to make a change in the RAM offset and size — we’ll get to this in the Testing section since it requires us to actually run the application and get the appropriate values from the log output.
Running our Application
As we mentioned earlier, we’ll be using command line for almost everything in this post. For compiling the application, we’ll be using ARM GCC and GNU Make. For flashing the application to the board, we’ll be using the Nordic Command Line Tools (specifically, the nrfjprog
command).
1. Compiling the Application
Before we can compile the application, we need to make a couple of modifications:
Add the new Simple Service source code file to
Makefile
. We add$(PROJ_DIR)/simple_service.c
to theMakefile
file which is located atble_peripheral/ble_app_template/pca10056/s140/armgcc/Makefile
:Update the
Makefile.posix
file to point to the correct location and version of the armgcc compiler. Edit the file located at<nRF5 SDK Folder>/components/toolchain/gcc/Makefile.posix
:Make sure you replace
<Toolchain Folder>
with the appropriate path based on where you installed the armgcc toolchain.
Now that we’ve made these changes, we’re ready to compile our application and flash it to the development board.
In the same folder where Makefile
is located (ble_peripheral/ble_app_template/pca10056/s140/armgcc/
), run the following command.
Sometimes, it’s a good idea to run make clean
before running make
just to make sure you are starting from a clean state. You should see an output similar to this when it finishes:
2. Hardware Setup
The second step is to get the hardware set up to run the example.
Here are the steps to accomplish this:
- Connect the development kit to your computer (via the micro-USB to USB-A cable)
- Make sure the development kit jumpers are in the right settings:
- SW6 set to “Default” (not “nRF ONLY”)
- SW9 (nRF power source) set to “VDD”
- Power set to “ON”
Erase the development kit by running the following command. This will erase the chipset on the board, just to make sure we are starting from a clean slate.
Next, we will flash the chipset with Nordic’s SoftDevice which acts as both the BLE stack and RTOS (Real-Time Operating System) for the nRF52 chipset. This is crucial before flashing our application (since the binaries compile separately).
To do this, run the following command:
Don’t forget to change the
<nRF5 SDK Folder>
portion to match the location where you installed/unzipped the nRF5 SDK.The final step is to flash the application binary to the chipset. To do this, run the following command:
That’s it! Our application should now be running on the board. To verify that, we will be using a serial terminal program.
In the next section, we’ll go through setting up the terminal program and then verify that the application is running correctly.
3. Serial Terminal Program Setup
As we mentioned earlier, we’ll be using a program called CoolTerm.
There are three parts to get this working:
- Run the CoolTerm application
Make sure the serial port settings are correct (listed at this link):
Now, hit OK.
Finally, connect to the serial port by clicking the “Connect” button:
You may not see any output since the program probably started before you connected. To reset the development board, we can simply run the following command from the Terminal:
The first time you run the application you’ll most likely get an error that looks something like this:
This is due to the fact that we modified the value of the macro for the number of vendor-specific UUIDs in the SoftDevice. The good part is that the log output tells us exactly which values we need to use for RAM offset and RAM size to fix this problem.
In order to apply the fix, we need to edit the file named ble_app_template_gcc_nrf52.ld
located at ble_app_template/pca10056/s140/armgcc/
and modify the values of
and
Now we just need to repeat the steps of cleaning, building, and flashing the SoftDevice, and application to the target.
If all goes well, you should now see the following printed in the Terminal window:
Testing the Application
Even though we verified that our application is running correctly and no errors show up in the terminal window, we still need to verify its BLE functionality. The easiest way to do so is by running a BLE Central mobile app such as nRF Connect (iOS or Android) or LightBlue (iOS or Android).
The different aspect we want to test include:
- Making sure the BLE Peripheral advertises and can be discovered by the BLE Central.
- Making sure we can connect to the Peripheral.
- Verifying that we can discover the Simple Service and its Button 1 Characteristic.
- Verifying that we can read the state of Button 1 (pressed: 0x01 or released: 0x00).
- Verifying that we can subscribe to Notifications and get notified when Button 1’s state changes.
Let’s go through each of these tests. We’ll be using LightBlue for our tests here.
1. Advertisements
Once you launch LightBlue, you’ll be presented with a screen showing all the BLE devices advertising in the vicinity. Make sure you have Bluetooth enabled on your phone first.
2. Connection
Now that we verified that we can discover our device (“Memfault_Example”), we can attempt to connect to it. Simply click on the device and it should initiate a connection. You should momentarily see a message similar to the following before it establishes the connection:
After the connection is established, you will be presented with a screen similar to this:
3. GATT Service & Characteristic Discovery
The previous screen also verifies that the Simple Service was discovered correctly and it contains our Button 1 Characteristic.
If we click on the Simple Service, we should see the Button 1 Characteristic:
4. Characteristic Value Read
To verify that we can read the value of the Button 1 Characteristic, we can tap on “Read again”. To verify further, we can hold down Button 1 on the development board and then click “Read again”. This time it should display a value of 0x01.
5. Notifications
Finally, let’s verify that we can get notified when the Button 1 state changes. We can do so by clicking on the “Listen for notifications” button.
Once you click on it, you can test it by pressing Button 1 and releasing it a few times. You should see the updated values come in as you press and release the button:
Notifications disabled:
Notifications enabled:
Closing
That’s it for this post. We covered a whole lot here!
There’s a lot more to Bluetooth Low Energy including security, Bluetooth 5 features (long-range, high-speed, increased advertising capacity), GATT design, beacons, low-power optimization, and many more..
To recap, we went over:
- The basics of Bluetooth Low Energy (BLE)
- A comparison between BLE and Bluetooth Classic
- BLE Peripherals and Centrals
- Advertising
- Connections
- The Generic Access Profile (GAP)
- The Generic Attribute Profile (GATT), including Services and Characteristics
- Implementation of a simple BLE Peripheral application on the nRF52 development kit
- Interacting with the BLE Peripheral from a mobile phone
Be sure to let us know if you have any comments or suggestions on additional BLE-related topics you’d like us to cover in the discussion section below.
As a reminder, all the code used in this blog post is available on GitHub.
No comments:
Post a Comment