Introduction to Bluetooth Low Energy (BLE) communication

Bluetooth Low Energy, often abbreviated as Bluetooth LE or simply BLE, is a wireless communication technology that allows devices to connect and interact. BLE was designed with the primary goal of providing a more energy-efficient alternative to classic Bluetooth, while maintaining a reliable and robust wireless connection.

BLE is a respectable choice when battery life and efficiency are prioritized. With possible applications in wearable technologies, automation, IoT, and healthcare, it is essential for every developer to understand the fundamentals of BLE.

How it works

BLE supports communication in the 2.4 GHz ISMIndustrial, Scientific, and Medical band, akin to classic Bluetooth. One of the key ways BLE conserves energy is by entering a low-power sleep mode when not actively transmitting or receiving data. However, this energy conservation comes at the cost of transfer speeds, with BLE transfer speeds maxing out at 1MBs, which is 70% slower than classic Bluetooth.

BLE devices typically come in two forms:

  • Peripheral: The peripheral device advertises its presence by transmitting its ID to any observer in range, without seeking a response from it. For example, a sensor peripheral will constantly send out signals with its ID and name so that a central device can detect it.
  • Central: The central device scans for the signals transmitted by the peripherals in range and establishes a connection when needed. For example, a smartphone central device will read all the peripherals that are transmitting in its range, and the operator can decide which peripheral to establish a connection with.

GATT Profiles

Generic Attribute Profiles (GATT) define how two BLE devices organize and communicate data. These profiles are essentially standardized templates that ensure compatibility between different BLE devices, allowing them to understand and exchange data seamlessly. They use a generic data protocol called the Attribute Protocol (ATT), which stores data in a simple lookup table using 16-bit IDs for each entry in the table. There are two main components of a GATT Profile:

  • Characteristics: These are the individual data points other devices can access. Each characteristic has a unique UUIDUniversally Unique Identifier and can be used to read, write, or notify data. For example, an IoT device measuring humidity will have a humidity characteristic containing the current reading stored in the device.
  • Services: A service is a collection of related characteristics representing a specific functionality or feature of a device. For example, a heart rate monitor device may have a heart rate service with characteristics like heart rate measurement and blood oxygen level.

Note: GATT connections are exclusive. This means a peripheral device can only be connected to one central device at a time; it stops advertising itself once it connects to a central device.

GATT transactions

GATT transactions refer to the data exchange processes that occur between a central and peripheral device. There are four key GATT transactions:

  • Read: In a read transaction, the central requests data from a specific characteristic. The peripheral responds with the requested data if it supports reading from that characteristic.

  • Write: In a write transaction, the central sends data to a specific characteristic for the peripheral to process or store, and the peripheral acknowledges the write operation if it supports it.

  • Notify: Some characteristics can be configured to send notifications to the central device when their values change in real-time, given the central has subscribed to these notifications.

  • Descriptor operations: Descriptors are metadata associated with characteristics and can be used to modify their behavior. Central devices can read and write descriptors to control how characteristics behave.

How two BLE devices interact
How two BLE devices interact

Advantages of BLE

BLE offers low-power consumption, cost-effectiveness, and compatibility, making it suitable for various applications. It particularly benefits IoT, health and fitness, proximity-based services, asset tracking, home automation, and industrial applications. These are the main advantages BLE provides over other communication protocols:

  1. Efficiency: BLE is designed for low-power systems, making it ideal for battery-operated or energy-efficient devices. This is crucial for applications like wearables, IoT sensors, and other devices that need to run on a small power source for extended periods.

  2. Portability and low cost: BLE chips and components are small in size and relatively inexpensive, which makes them suitable for a wide range of devices. This cost-effectiveness allows BLE to be used in various applications.

  3. Compatibility: BLE is compatible with a wide range of devices, including smartphones, tablets, and computers. This makes it easy to connect and communicate with multiple devices, facilitating cross-platform development.

  4. Short range: BLE is designed for short-range communication, typically within a range of 10 meters. This is advantageous for applications where data transmission needs to be localized and secure.

  5. Fast connection: BLE enables fast and efficient connection between devices, reducing latency and improving the user experience.

  6. Low interference: BLE operates in the 2.4 GHz ISM band, which is often used for other wireless technologies. Despite this, it employs advanced techniques to minimize interference, ensuring reliable communication.

Implementation

BLE implementations vary depending on the type of libraries and environments being used. For this example, let’s use the Arduino BLE library, which has predefined transaction functions, making it easy to use and implement. Our goal is to configure a peripheral device, connect to a central device, and transmit a “Hello world” string.

#include <ArduinoBLE.h>
BLEService customService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Define a custom service UUID
BLECharacteristic customCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // Define a custom characteristic UUID
string dataToSend = "Hello world"; // Your data to be sent
void setup() {
// Initialize the BLE library
if (!BLE.begin()) {
while (1);
}
// Set the local name and advertising parameters
BLE.setLocalName("Peripheral 1");
BLE.setAdvertisedService(customService);
customService.addCharacteristic(customCharacteristic); // Add the characteristic to the service
BLE.addService(customService); // Add the service
BLE.advertise(); // Start advertising
}
void loop() {
BLEDevice central = BLE.central(); // Wait for a connection
if (central) {
while (central.connected()) {
customCharacteristic.writeValue(dataToSend);
delay(1000); // Send data every second
}
}
}

Explanation

  • Lines 1–5: We define a custom service and characteristic using the BLEService and BLECharacteristic functions. We load them with arbitrary UUIDs, and the permitted transactions: BLERead and BLEWrite.

  • Line 6: We store the string we want to send in the dataToSend variable.

  • Lines 8–22: In the setup() function, we configure our peripheral device by giving it a name, adding our custom characteristic to our custom service, and advertising it.

  • Lines 23–33: In the loop() function, we first check if our device is connected to a central device using BLE.central(), entering a while loop when it does. In this loop, we send dataToSend every second using the writeValue() and delay() functions.

Conclusion

As we continue to witness the rapid growth of energy-efficient and reliable wireless connectivity, BLE’s significance in the world of technology remains undeniable. Its efficiency, compatibility, and adaptability make it a crucial player in the ever-expanding landscape of connected devices, promising a future filled with innovative and efficient wireless solutions.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved