Categories: ESP32 Firmware Guide

SimpleBLE on ESP32

ESP32 integrates Bluetooth link controller and Bluetooth baseband, which carry out the baseband protocols and other low-level link routines, such as modulation/demodulation, packets processing, bitstream processing, frequency hopping, etc. This tutorial “SimpleBLE on ESP32” shows how to use SimpleBLE to advertise the name of the device and change it on the press of a button. The button is attached between GPIO0 and GND, and the device name changes each time the button is pressed.
BLE stands for Bluetooth Low Energy. It’s a Bluetooth protocol that boosts considerably lower power consumption compared to “Classic” Bluetooth. BLE “servers” (like the ESP32 reading sensor data) can “notify” clients (like your smartphone) periodically to send them bits of data. Therefore, BLE is more suitable for low-power IoT applications where large amounts of data aren’t required.

All BLE devices use the Generic Attribute Profile (GATT). The Generic Attributes (GATT) services are collections of characteristics and relationships to other services that encapsulate the behavior of part of a device. The GATT has the following terms

  • Client: A device that initiates GATT commands and requests, and accepts responses, for example, a computer
  • Server: A device that receives GATT commands and requests, and returns responses, for example, a temperature sensor
  • Characteristic: A data value transferred between client and server, for example, the current battery voltage.
  • Descriptor: A descriptor provides additional information about a characteristic. For instance, a temperature value characteristic may have an indication of its units (e.g. Celsius), and the maximum and minimum values which the sensor can measure. Descriptors are optional – each characteristic can have any number of descriptors.
  • Service: A collection of related characteristics, which operate together to perform a particular function. For instance, the Health Thermometer service includes characteristics for a temperature measurement value and a time interval between measurements.

Code snippet

Here using the library SimpleBLE

#include<SimpleBLE.h>

After enabling the Bluetooth on the device, connect to the ESP32 SimpleBLE. When pressing the push button, changing the device’s name.

ble.begin("ESP32 SimpleBLE");

Initializes the BLE in order to use all its methods within the sketch.

ble.begin("ESP32 SimpleBLE");
Serial.println("Press the button to change the device's name");

The ESP32 (acting as the server) “notifies” the client via the TX characteristic UUID and data is sent to the ESP32 and received via the RX characteristic UUID. However, since there is sending and receiving, TX on the ESP32 is actually RX on the Android app, so inside Thunkable you will notice that the UUID’s are swapped from those in the Arduino sketch.

Share