Esp32 Ble
Esp32 Ble
Esp32 Ble
2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
The ESP32 comes not only with Wi-Fi but also with Bluetooth and Bluetooth Low
Energy (BLE). This post is a quick introduction to BLE with the ESP32. First, we’ll
explore what’s BLE and what it can be used for, and then we’ll take a look at
some examples with the ESP32 using Arduino IDE. For a simple introduction we’ll
create an ESP32 BLE server, and an ESP32 BLE scanner to nd that server.
Recommended reading: learn how to use ESP32 Bluetooth Classic with Arduino
IDE to exchange data between an ESP32 and an Android smartphone.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 1/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
This makes it consume very low power. BLE consumes approximately 100x less
power than Bluetooth (depending on the use case).
Take a look at the table below that compares BLE and Bluetooth Classic in more
detail.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 2/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Due to its properties, BLE is suitable for applications that need to exchange small
amounts of data periodically running on a coin cell. For example, BLE is of great
use in healthcare, tness, tracking, beacons, security, and home automation
industries.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 3/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
The server advertises its existence, so it can be found by other devices, and
contains the data that the client can read. The client scans the nearby devices,
and when it nds the server it is looking for, it establishes a connection and
listens for incoming data. This is called point-to-point communication.
As mentioned previously, BLE also supports broadcast mode and mesh network:
Broadcast mode: the server transmits data to many clients that are
connected;
Mesh network: all the devices are connected, this is a many to many
connection.
Even though the broadcast and mesh network setups are possible to implement,
they were developed very recently, so there aren’t many examples implemented
for the ESP32 at this moment.
GATT
GATT stands for Generic Attributes and it de nes an hierarchical data structure
that is exposed to connected BLE devices. This means that GATT de nes the way
that two BLE devices send and receive standard messages. Understanding this
hierarchy is important, because it will make it easier to understand how to use
the BLE and write your applications.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 4/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
BLE Service
The top level of the hierarchy is a pro le, which is composed of one or more
services. Usually, a BLE device contains more than one service.
Every service contains at least one characteristic, or can also reference other
services. A service is simply a collection of information, like sensor readings, for
example.
There are prede ned services for several types of data de ned by the SIG
(Bluetooth Special Interest Group) like: Battery Level, Blood Pressure, Heart Rate,
Weight Scale, etc. You can check here other de ned services.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 5/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
BLE Characteristic
The characteristic is always owned by a service, and it is where the actual data is
contained in the hierarchy (value). The characteristic always has two attributes:
characteristic declaration (that provides metadata about the data) and the
characteristic value.
The properties describe how the characteristic value can be interacted with.
Basically, it contains the operations and procedures that can be used with the
characteristic:
Broadcast
Read
Write without response Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 6/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Write
Notify
Indicate
Authenticated Signed Writes
Extended Properties
UUID
Each service, characteristic and descriptor have an UUID (Universally Unique
Identi er). An UUID is a unique 128-bit (16 bytes) number. For example:
55072829-bc9e-4c53-938a-74a6d4c78776
There are shortened UUIDs for all types, services, and pro les speci ed in the
SIG (Bluetooth Special Interest Group).
But if your application needs its own UUID, you can generate it using this UUID
generator website.
In summary, the UUID is used for uniquely identifying information. For example,
it can identify a particular service provided by a Bluetooth device.
Note: You need to have the ESP32 add-on installed on the Arduino IDE. Follow
one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if
you haven’t already.
In your Arduino IDE, you can go to File > Examples > ESP32 BLE Arduino and
explore the examples that come with the BLE library. Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 7/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Note: to see the ESP32 examples, you must have the ESP32 board selected
on Tools > Board.
For a brief introduction to the ESP32 with BLE on the Arduino IDE, we’ll create an
ESP32 BLE server, and then an ESP32 BLE scanner to nd that server. We’ll use
and explain the examples that come with the BLE library.
To follow this example, you need two ESP32 development boards. We’ll be using
the ESP32 DOIT DEVKIT V1 Board.
should load:
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snip
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
For creating a BLE server, the code should follow the next steps:
1. Create a BLE Server. In this case, the ESP32 acts as a BLE server.
Menu
2. Create a BLE Service.
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 9/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
You can leave the default UUIDs, or you can go to uuidgenerator.net to create
random UUIDs for your services and characteristics.
Serial.begin(115200);
Then, you create a BLE device called “MyESP32”. You can change this name to
whatever you like.
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 10/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
After that, you create a service for the BLE server with the UUID de ned earlier.
Then, you set the characteristic for that service. As you can see, you also use the
UUID de ned earlier, and you need to pass as arguments the characteristic’s
properties. In this case, it’s: READ and WRITE.
After creating the characteristic, you can set its value with the setValue()
method.
In this case we’re setting the value to the text “Hello World says Neil”. You can
change this text to whatever your like. In future projects, this text can be a
sensor reading, or the state of a lamp, for example.
Finally, you can start the service, and the advertising, so other BLE devices can
scan and nd this BLE device.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 11/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
This is just a simple example on how to create a BLE server. In this code nothing
is done in the loop() , but you can add what happens when a new client
connects (check the BLE_notify example for some guidance).
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snipp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
This code initializes the ESP32 as a BLE device and scans for nearby devices.
Upload this code to your ESP32. You might want to temporarily disconnect the
other ESP32 from your computer, so you’re sure that you’re uploading the code
to the right ESP32 board.
Once the code is uploaded and you should have the two ESP32 boards powered
on:
Go to the Serial Monitor with the ESP32 running the “BLE_scan” example, press
the ESP32 (with the “BLE_scan” sketch) ENABLE button to restart andwait
Menua few
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 13/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
The scanner found two devices: one is the ESP32 (it has the name “MyESP32),
and the other is our MiBand2.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 14/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
You can scan your ESP32 BLE server with your smartphone and see its services
and characteristics. For that, we’ll be using a free app called nRF Connect for
Mobile from Nordic, it works on Android (Google Play Store) and iOS (App Store).
Go to Google Play Store or App Store and search for “nRF Connect for Mobile”.
Install the app and open it.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 15/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Don’t forget go to the Bluetooth settings and enable Bluetooth adapter in your
smartphone. You may also want to make it visible to other devices to test other
sketches later on.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 16/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Once everything is ready in your smartphone and the ESP32 is running the BLE
server sketch, in the app, tap the scan button to scan for nearby devices. You
should nd an ESP32 with the name “MyESP32”.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 17/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
As you can see in the gure below, the ESP32 has a service with the UUID that
you’ve de ned earlier. If you tap the service, it expands the menu and shows the
Characteristic with the UUID that you’ve also de ned.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 18/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
The characteristic has the READ and WRITE properties, and the value is the one
you’ve previously de ned in the BLE server sketch. So, everything is working ne.
Wrapping Up
In this tutorial we’ve shown you the basic principles of Bluetooth Low Energy and
shown you some examples with the ESP32. We’ve explored the BLE server sketch
and the BLE scan sketch. These are simple examples to get you started with BLE.
The idea is using BLE to send or receive sensor readings from other devices. We’ll
be posting more tutorials and projects about BLE with the ESP32, so stay tuned!
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 19/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32
and you want to learn more about it, we recommend enrolling in Learn ESP32 with
Arduino IDE course.
Build Web Server projects with the ESP32 and ESP8266 boards to control outputs
and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server
communication protocols DOWNLOAD »
Recommended Resources
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 20/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Home Automation using ESP8266 eBook and video course » Build IoT and
home automation projects.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 21/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Menu
ESP32/ESP8266 Web Server: Control Outputs with Timer
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 22/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
ESP32/ESP8266 Web Server: Control Outputs with Timer
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 23/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
SUBSCRIBE
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 24/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Miguel Wisintainer
June 19, 2018 at 11:20 am
Reply
Sheldon
June 19, 2018 at 6:41 pm
Reply
Germán
June 22, 2018 at 10:44 pm
Great Tutorial. As you say Esp32 supports Classic Bluetooth too, but there
are not many tutorials about it. There are some pro les in BT that do not
have a BLE equivalent, like SPP or A2DP. I’m developing a Classic BT
scanner library for Esp32 and Arduino IDE. I’ve got some good results but
a theoretical tutorial about it would be super.
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 25/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Reply
Rui Santos
June 23, 2018 at 8:35 am
Reply
Giovanni
July 6, 2018 at 7:25 pm
Great site!
I have installed ESP32 board and many examples on my Arduino IDE, nut
I can’t see on my IDE the example that you use… I see only
SimpleBLEdevice and SerialToSerialBT
Reply
Yahya Tawil
July 23, 2018 at 8:28 am
Reply
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 26/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Shehu Bello
September 19, 2018 at 8:30 am
Reply
Sara Santos
September 19, 2018 at 2:17 pm
Thank you!
Regards,
Sara
Reply
Túlio
November 8, 2018 at 11:39 pm
Reply
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 27/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Sara Santos
November 10, 2018 at 10:49 am
Hi Túlio.
Yes, it is possible. However we don’t have any tutorial about that subject
here on the blog.
We have a section about that in our course about ESP32:
https://randomnerdtutorials.com/learn-esp32-with-arduino-ide/
Regards,
Sara
Reply
Daniel Escasa
February 2, 2020 at 1:23 pm
Reply
Michele
March 3, 2019 at 8:28 am
Thanks, and congratulations for this useful tutorial. I also noticed that
there are not many news on this topic on the net. Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 28/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Reply
Paul
March 4, 2019 at 7:20 pm
Hi! Do you know if we can awake the ESP32 running BLE-Low energy? (not
the classic one). As far as I managed to nd till now out there, ESP32
switches o WiFi and BLE in sleep modes, so I can not gure out how we
could have an ESP32 asleep and wake it up when BLE tries to pair/send
something… Any tip will be of a great help! Thanks in advance,
Reply
JanWillem
September 6, 2019 at 11:12 am
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID)
Reply Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 29/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Enrrique Iturry
August 19, 2020 at 3:25 am
Reply
Ali
September 19, 2019 at 8:17 am
brief and short but good and applicable, thank you very much
Regards
Ali
Reply
Sara Santos
September 19, 2019 at 9:00 am
Thanks
Reply
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 30/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Joel
June 17, 2020 at 11:36 pm
Reply
Sara Santos
June 20, 2020 at 2:21 pm
Hi Joel.
Unfortunately, we don’t have any examples with BLE and audio.
Regards,
Sara
Reply
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 31/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
kunal
August 19, 2020 at 4:00 pm
Reply
kunal
August 19, 2020 at 4:11 pm
Reply
Ion
September 13, 2020 at 3:18 pm
Hello,
I have a project developed on TTGO T7 Ver 1.0
It is a simple keypad 3X4 reader, plus and RFID decoder (get RFID data
from TX of RDM6300 and extract the ID of the card) Match the keyboard
input with the one store on EEprom, match the RFID with the stored on
EEprom and if the match send an encrypted signal (like Go) through
Bluetooth to a slave identical TTGO chip. Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 32/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
I wonder if the project can work if i try to use a regular ESP32 NodeMCU.
TTGO chips are only in China and very hard to get through Ali. ESP32 is
easier to buy , even from Amazon. This is my problem.
Reply
Ralph Hilton
October 12, 2020 at 5:50 pm
Reply
Malcolm
September 22, 2020 at 7:23 pm
I have managed to create an ESP32 BLE server which sends data within
the loop() to an ESP32 BLE client. Great! But how do I send a reply back
from the client to the server?
Reply
Ion
September 22, 2020 at 7:30 pm
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 33/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Reply
Gianfranco Perini
November 3, 2020 at 2:15 pm
Hi…
I must thank you for more and more lessons I used for me and my
friends…
But I didn’t understand a little thing (maybe ’cause I am italian)…
Why it need two UUID?
Is this that I can use one UUID (service) for create a LAN (imagine) and
UUID (characteristic) for one host address???
Please help me…
Thank
Reply
alan
February 3, 2021 at 9:35 am
HI
why I cannot use esp32 as the client to connect the BLE headset, BLE
watch, BLE keyboard etc, I have use nRF connect APP to get the UUID,
MAC, and put that on the code, but the client does not work. Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 34/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
Reply
Leave a Comment
Name *
Email *
Website
Post Comment
Menu
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 35/36
11.02.2021 ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
About Support Terms Privacy Policy Refunds MakerAdvisor.com Join the Lab
https://randomnerdtutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/ 36/36