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 find that server.
Updated 11 June 2024
Introducing Bluetooth Low Energy
For a quick introduction to BLE, you can watch the video below, or you can scroll down for a written explanation.
Recommended reading: learn how to use ESP32 Bluetooth Classic with Arduino IDE to exchange data between an ESP32 and an Android smartphone.
What is Bluetooth Low Energy?
Bluetooth Low Energy, BLE for short, is a power-conserving variant of Bluetooth. BLE’s primary application is short distance transmission of small amounts of data (low bandwidth). Unlike Bluetooth that is always on, BLE remains in sleep mode constantly except for when a connection is initiated.
This makes it consume very low power. BLE consumes approximately 100x less power than Bluetooth (depending on the use case).
Additionally, BLE supports not only point-to-point communication, but also broadcast mode, and mesh network.
Take a look at the table below that compares BLE and Bluetooth Classic in more detail.
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, fitness, tracking, beacons, security, and home automation industries.
BLE Server and Client
With Bluetooth Low Energy, there are two types of devices: the server and the client. The ESP32 can act either as a client or as a server.
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 finds 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 defines an hierarchical data structure that is exposed to connected BLE devices. This means that GATT defines 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.
BLE Service
The top level of the hierarchy is a profile, 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 predefined services for several types of data defined by the SIG (Bluetooth Special Interest Group) like: Battery Level, Blood Pressure, Heart Rate, Weight Scale, etc. You can check here other defined services.
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.
Additionally, the characteristic value can be followed by descriptors, which further expand on the metadata contained in the characteristic declaration.
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
- Write
- Notify
- Indicate
- Authenticated Signed Writes
- Extended Properties
UUID
Each service, characteristic and descriptor have an UUID (Universally Unique Identifier). 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 profiles specified 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 to uniquely identify information. For example, it can identify a particular service provided by a Bluetooth device.
BLE with ESP32
The ESP32 can act as a BLE server or as a BLE client. There are several BLE examples for the ESP32 in the ESP32 BLE library for Arduino IDE. This library comes installed by default when you install the ESP32 on the Arduino IDE.
Note: You need to have the ESP32 add-on installed on the Arduino IDE. Follow the next tutorial 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 > BLE and explore the examples that come with the BLE library.
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 find 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.
ESP32 BLE Server
To create an ESP32 BLE Server, open your Arduino IDE and go to File > Examples > BLE and select the Server example. The following code should load:
/*
Complete Getting Started Guide: https://RandomNerdTutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
For creating a BLE server, the code should follow the next steps:
- Create a BLE Server. In this case, the ESP32 acts as a BLE server.
- Create a BLE Service.
- Create a BLE Characteristic on the Service.
- Create a BLE Descriptor on the Characteristic.
- Start the Service.
- Start advertising, so it can be found by other devices.
How the code works
Let’s take a quick look at how the BLE server example code works.
It starts by importing the necessary libraries for the BLE capabilities.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
Then, you need to define a UUID for the Service and Characteristic.
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
You can leave the default UUIDs, or you can go to uuidgenerator.net to create random UUIDs for your services and characteristics.
In the setup(), it starts the serial communication at a baud rate of 115200.
Serial.begin(115200);
Then, you create a BLE device called “MyESP32”. You can change this name to whatever you like.
// Create the BLE Device
BLEDevice::init("MyESP32");
In the following line, you set the BLE device as a server.
BLEServer *pServer = BLEDevice::createServer();
After that, you create a service for the BLE server with the UUID defined earlier.
BLEService *pService = pServer->createService(SERVICE_UUID);
Then, you set the characteristic for that service. As you can see, you also use the UUID defined earlier, and you need to pass as arguments the characteristic’s properties. In this case, it’s: READ and WRITE.
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
After creating the characteristic, you can set its value with the setValue() method.
pCharacteristic->setValue("Hello World says Neil");
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 find this BLE device.
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
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 Notify example for some guidance).
ESP32 BLE Scanner
Creating an ESP32 BLE scanner is simple. Grab another ESP32 (while the other is running the BLE server sketch). In your Arduino IDE, go to File > Examples > BLE and select the Scan example. The following code should load.
/*
Complete Getting Started Guide: https://RandomNerdTutorials.com/esp32-bluetooth-low-energy-ble-arduino-ide/
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; // in seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults *foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices->getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
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:
- One ESP32 with the “BLE_server” sketch;
- Other with ESP32 “BLE_scan” sketch.
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 and wait a few seconds while it scans.
The scanner found two devices: one is the ESP32 (it has the name “MyESP32“), and the other is a smart watch.
Testing the ESP32 BLE Server with Your Smartphone
Most modern smartphones should have BLE capabilities. I was using a OnePlus, but most smartphones should also work.
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.
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.
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 find an ESP32 with the name “MyESP32”.
Click the “Connect” button.
As you can see in the figure below, the ESP32 has a service with the UUID that you’ve defined earlier. If you tap the service, it expands the menu and shows the Characteristic with the UUID that you’ve also defined.
The characteristic has the READ and WRITE properties, and the value is the one you’ve previously defined in the BLE server sketch. So, everything is working fine.
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.
Other Bluetooth-related tutorials that you may like:
- ESP32 with Bluetooth and Bluetooth Low Energy: The Ultimate Guide
- ESP32 BLE Server and Client (Bluetooth Low Energy)
- ESP32 Wi-Fi Provisioning via BLE (Bluetooth Low Energy) – Arduino IDE
- ESP32 Web Bluetooth (BLE): Getting Started Guide
- ESP32 Bluetooth Classic with Arduino IDE – Getting Started
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.
You might also like reading:
- Learn ESP32 with Arduino IDE
- ESP32 Bluetooth Classic with Arduino IDE – Getting Started
- ESP32 Data Logging Temperature to MicroSD Card
- ESP32 with DC Motor and L298N Motor Driver – Control Speed and Direction
Thanks for reading.
Should be good invest more in the BLE 4.2 THEORY!
Wow. Awesome tutorial. Thanks, Rui!
Great Tutorial. As you say Esp32 supports Classic Bluetooth too, but there are not many tutorials about it. There are some profiles 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.
You’re right! Unfortunately I also couldn’t find any information on that subject. Let me know if you end up developing a classic BLE app.
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
Thanks for this tutorial. It’s a very handy overview of BLE.
Indeed it is very handy and working fine. However, with this solution BLE is turning into BHE (high energy) because the ESP32 does not go into sleep after each transmission. Are there ways to do that and wakeup again?
Thanks,
Tjerk
Thanks Rue for the tutorial. It is very informative.
Thank you!
Regards,
Sara 🙂
is it possible to make an application with two esp32 (one like a server an another like a client)? I didn´t find anyting about it.
Thanks for sharing! Túlio
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 🙂
Túlio, there are two Examples, available from Files ∘ Examples ∘ ESP32BLEArduino — BLE_client and BLE_server. I also saw something on YouTube, could’ve been Rui himself :), maybe a later post here
Thanks, and congratulations for this useful tutorial. I also noticed that there are not many news on this topic on the net.
Hi! Do you know if we can awake the ESP32 running BLE-Low energy? (not the classic one). As far as I managed to find till now out there, ESP32 switches off WiFi and BLE in sleep modes, so I can not figure 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,
Thanks for this tutorial. It helped me a lot making a connection to my FitBit wearable. Now I also tryed to make a connection to a toy with Bluetooth and the software crahses at :
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID)
in the serial monitor i find:
Stack smashing protect failure!
abort() was called at PC 0x4014f34b on core 1
Do you have an idea how to solve this, or where i need to look?
I had the same problem, it’s a flaw in our code, in my case I had a “String” subroutine and within this I had “int” type variables, I solved it by removing the “int” variable type
I’m having the same problem. Can you a bit more specific on your solution?
Hi.
ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re having trouble getting the value from an ADC2 GPIO, you may consider using an ADC1 GPIO instead. That should solve your problem.
So, instead of GPIO 27, you can try using GPIO 32, for example.
Regards,
Sara
thanks a lot for sharing.
brief and short but good and applicable, thank you very much
Regards
Ali
Thanks 😀
Hello, Rui Santos.
I have a bluethooth project and I would like to know if you could give a more in-depth lesson on the bluetooth of the esp32.
I’ll give you a little context of what I want to accomplish so you can see if you can help me.
What I’m doing is a small phone (with a sim900 expansion card for arduino one) that can send the audio data (through bluetooth) to a bluetooth headset I have (it’s a samsung icon x 2018). The headphones also have a built-in microphone, so I would also like you to send audio (via bluetooth) to the esp32 and then to the sim900
I’d like to know what you think, if you can talk more about it or any code you already have done I’d appreciate it very much.
I would also like to support you with a donation :).
I’ll say goodbye and I’ll keep an eye on your answer.
Hi Joel.
Unfortunately, we don’t have any examples with BLE and audio.
Regards,
Sara
How can i update the ble name during runtime???
It’s possible to in ESP32 beacon, BLE manufacturer data can be segregation?
my question is BLE manufacturer data ( Beacon identification + Beacon ID+ Length+ Device UUID + major + minor + RSSI )can be possible to print on separated?
It’s mean’s in ESP32 beacon, BLE manufacturer data can be segregation?
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.
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.
Banggood has TTGO
banggood.com/search/ttgo.html?from=nav
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?
pCharacteristic->setValue() i cannt set value in loop..getting error as ” Guru Meditation Error: Core 1 panic’ed (StoreProhibited). Exception was unhandled” . Please help me with code
same ere i would like to know if there is any solution
My question is about licensing
I read that you have to get license for Bluetooth in order to use it, like FCC and others.
Does ESP32 is licence for using Bluetooth, or i need additional licence ?
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
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.
hello can you please make a tutorial to connect multiple esp32 on ble mesh network.
please share any helpful link if possible.
Got any reference? I also need this
Great thanks
Is it possible to send serial data (like SerialBluetooth.h) through BLE?
Hi.
For that, you need to use Bluetooth Classic.
See this tutorial: https://randomnerdtutorials.com/esp32-bluetooth-classic-arduino-ide/
Regards,
Sara
Thank you Sarah. Do you have a tutorial on OTA over Bluetooth? Also setting WiFi credentials over initial BLE (wifimanager)?
Hello,
The code is very good and I just wanted to know that if this code will work with NodeMCU ESP32?
Regards
Hi.
This code should work with any ESP32 board.
Regards,
Sara
thanks for this amazing tutorial. am currently working on using the ESP32 board as a regular IBeacons but somehow could not figure out how to transmit the UUID, Major, Minor and RSSI the way normal iBeacons do. can anyone help me with that
Hi Sara,
This is really nice project.
Can you create some project on ESP32CAM for video streaming + control over WebBLE if possible ?
Thanks,
KP
the simple BLE server seems to have some issue now, it worked when I tried it a year or so ago, but not now.
What happens:
you can scan and connect and see the info from the device. if you disconnect then go back to scanning again, the ESp32 device has vanished. if you reboot it it comes back. scan and read again, close and go back to scanning and it has vanished.
I tried different ESp32 boards and still the same result.
I found an example of a esp32 BLE serial server that I used about a year ago to send strings to/from a ESP32-BLE which used to work. Now, the same error! connecting once, talk to the device, then disconnect. Device has vanished from the network and only comes back with a reboot.! tried scanning from Android phone iPad, all the same result
Maybe the ESP32 Arduino BLE lib. has issues??
Hi.
Did you try it with the sketch provided in the Examples menu?
Regards,
Sara
thanks – I think the issue is it needs to restart advertising again. ESP32 example BLE_server_multiconnect does this, so I can adapt that.
I’ll test the example and see if that also happens.
Regards,
Sara
Hi again.
I just tested the BLE Server sketch and it is working as expected.
I connected to it using my smartphone, then disconnected. After that, I was able to reconnect again, and so on…
Regards,
Sara
How can i update the name of the bluetooth in runtime??
Very interesting project and well described.
What could be the average power consumption sending one message every hour?
Thanks
Renzo
Maybe this will be usefull for someone, who use esp32 with ble:
Use NimBLEDevice.h instead BLEDevice.h for ESP32 !!!.
The BLEDevice.h eating too much memory, and if you will use wifi & BLE – the free memory will be dramatically low.
Found NimBLEDevice.h library,
In my sketches Its use up to 44% less memory, compared to BLEDevice.h !!!
I’d tried load this firmware on ESP32-CAM and shows this message:
“Arduino: 1.8.13 (Windows 10), Placa:”AI Thinker ESP32-CAM, 240MHz (WiFi/BT), QIO, 80MHz”
O sketch usa 806526 bytes (25%) de espaço de armazenamento para programas. O máximo são 3145728 bytes.
Variáveis globais usam 38936 bytes (11%) de memória dinâmica, deixando 288744 bytes para variáveis locais. O máximo são 327680 bytes.
esptool.py v3.0-dev
Serial port COM3
Connecting…….._____….._____….._____….._____….._____….._____….._____
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header
A fatal error occurred: Failed to connect to ESP32: Timed out waiting for packet header”
After that I didn’t get load any firmware, even that one I’ve already loaded with success.
Did anyone saw this failure before ? Can help me how to solve it ?
Hi.
Are you using an ESP32-CAM?
Take a look at the ESP32-CAM troubleshooting guide: https://randomnerdtutorials.com/esp32-cam-troubleshooting-guide/
Check bullet number 1.
Regards,
Sara
I tried to make a BLE server protected so adding encryption and security. They are just 5 general SLOCs more and, eventually, one SLOC for each characteristics but it does not work. Testing with my smartphones (Andorid 8 and 10) usinf nrfConnect, they require the static PIN to pair/bond with the ESP32, they connect but they disconnect soon also, just matter a couple of seconds. Do you have any report about that?
This is my test code:
#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID “4fafc201-1fb5-459e-8fcc-c5c9c331914b”
#define SSID_CHAR_UUID “1afb81ce-36e1-4688-b7f5-ea07361b26a8”
#define PSWD_CHAR_UUID “1be107e0-2d9e-4091-a0d3-6407e01b2a30”
#define WIFI_CHAR_UUID “1afb81ce-a705-4ab5-aaab-294269ce9a52”
#define IP_CHAR_UUID “beb5483e-36e1-4688-b7f5-ea07361b26a8”
const char* ssid = “ESP32-TT22”;
const char* password = “pippo21931”;
char g_strWiFiEnabled[] = “0”;
const char* ip = “192.168.4.1”;
void setupBLE()
{
BLEDevice::init(“Long name works now”);
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharSSID = pService->createCharacteristic(
SSID_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
BLECharacteristic *pCharPSWD = pService->createCharacteristic(
PSWD_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
BLECharacteristic *pCharWIFI = pService->createCharacteristic(
WIFI_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
BLECharacteristic *pCharIP = pService->createCharacteristic(
IP_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharSSID->setValue(ssid);
pCharPSWD->setValue(password);
pCharWIFI->setValue(g_strWiFiEnabled);
pCharIP->setValue(ip);
pService->start();
#define COND 1
#if COND == 0
// [MM] enable encrypting PIN protected pairing
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
#endif
#if COND == 0
// [MM] enable encrypting PIN protected pairing
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
#endif
// [MM] setup the encrypted PIN for protected pairing
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setStaticPIN(2193);
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println(“Characteristic defined! Now you can read it in your phone!”);
}
//—————————————————————
void setup()
{
Serial.begin(115200);
Serial.println(“Starting BLE work!”);
setupBLE();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
Nice explanation on BLE. I currently use Home Assistant and ESPHOME to “program” esp32 devices. No example or suggestions how to get BLE server running in ESPHOME. I do find hardly anything on that topic
Hi.
Unfortunately, we don’t have any tutorials on that subject.
Regards,
Sara
Great tutorial. I am working on a project where a BLE TV remote control working as the BLE server over HID and the ESP32 is the client and acts as a bridge to WIFI. The battery life of the remote control is greatly reduced which let me believe the ESP32 does not allow the Remote Control to go into sleep mode. If I disconnect from the server to preserve battery, the first data package is lost. Is the best practice to disconect or is there another way to let the BLE server go into sleep mode
Hi.
Very useful post but I think it misses the content that all miss.
Bluetooth ble is used for low energy and that part is always forgotten.
I’ve search all over the web and still didn’t find any explanation of how it work.
As an example, do you save more power if you read from another device or if it’s written on the Arduino.
Another is how the intervals work.
Can it be that 2 devices get out of sync and never exchange information.
I’m trying to send strings from my android to the Arduino to show on a display. The Arduino is battery powered and I’m doing this once per second.
Would be nice if you can do an example of this. Because everywhere is just the same blink example that anyone can load from the ide.
Even to find how to send a long string was a nightmare.
Thanks!
hi, nice intro, but how to send sensors readings (dht22, hx711…)?
thanx for ideas.
Hi.
MAybe this tutorial will help: https://randomnerdtutorials.com/esp32-ble-server-client/
Regards,
Sara
This didn’t work right for me. First, in the phone app, I couldn’t connect to the ESP32. It disconnected immediately. Also, nothing appears under client. There are 2 Generic Attributes under Server though. Furthermore, the UUIDs I see there are short (e.g. 0x1801). I don’t see the characteristic I defined.
Second issue: the example code doesn’t match the more detailed explanation. It doesn’t include this statement. pAdvertising->start();
Hello,
a very nice informative website.
I will probably spend some more time here to learn.
I already have one question. Can a BLE client also send commands to the BLE server to make the BLE server send certain values?
Greetings….
Hi, im trying to transfer data from a DH11 Sensor with a ESP8266MOD-12F-Module per Bluetooth on my iPhone.
Could you please be so kind to send me an Arduino-Code and a circuit diagram?
Kind regards
Samuel
Hi, I’m trying to make a device that allows two ESP32 devices to detect each others presence within a certain proximity. Is it possible to make them alternate between acting as servers and receivers?
Thanks, Chris.
Hi great examples.
Ho can I send over mqtt data belonging from a ibeacon like battery and rssi?
Very handy tutorial and working fine! However, with this solution BLE is turning into BHE (high energy) because the ESP32 does not go into sleep after each transmission. Are there ways to do that, wakeup again and keeping the BLE connection alive?
Thanks,
Tjerk
Cool, thanks a lot: It helped me to get started but it also sent me into the wrong direction!
The described functionality works on the ESP32 only and I think it would usually be better to use the official ArduinoBLE library which is incompatible with this.
This would have the great advantage that you need to write your code only once and it runs on many other platforms as well….
Compilation error: ‘init’ is not a member of ‘BLEDevice’
give me solve
Hi.
Make sure you selected an ESP32 board in Tools > Board before compiling the code.
Regards,
Sara
I am facing the same issue. The code was working properly a few weeks ago. I tried it yesterday and am having multiple errors in the BLE parts. If you found a solution please let me know
Regards
Hi.
Can you be more specific about the errors you’re getting?
Regards,
Sara
Hi Sara,
I used this exact code about a few weeks ago, it worked fine. But now the entire set of functions that are used to setup the BLE part, that gives an error for example.
Compilation error: ‘init’ is not a member of ‘BLEDevice’. This is the error for this part of the code
BLEDevice::init(“BLE Scanner”);
Similarly, the following part also gives an error.
error: ‘class BLECharacteristic’ has no member named ‘notify’
pCharacteristic->notify();
In short all the parts setting up the BLE gives an error of not having any such member in the files. My best bet is, it has something to do with the versions cause I had used the same code in November and it worked perfectly fine
My bet would be that you installed the incompatible ArduinoBLE library and therefore you are now running into naming conflicts
Hey Phil,
I am not using the ArduinoBLE library. I am using the ESP32_BLE one. Even the examples mentioned wont compile. Giving the same error as I mentioned, moreover, like I said this was working perfectly fine up until November (last time I implemented the code). I was checking it again yesterday and I get these errors.
Regards
So, did you double check that you did not install it by accident ?
Just having it installed will give you (these?) compile errors because Arduino can not distinguish between the two.
Hi.
check the version of the ESP32 boards you have installed in Tools > Board > Boards Manager > ESP32. You may need to update
Check that you have an ESP32 board selected in Tools > Board, and not any other board.
Regards,
Sara
Hello Rio Santos,
your Tutorial for ESP32 BLE is very good. I’ve working the BLE-Client which is connected to a EQ3-thermostat. That’s working 1 or 2 days and then I become the follow error
lld_pdu_get_tx_flush_nb HCI packet count mismatch (0, 1).
I’ve testing a lot of ESP32 WROOM, WROVER etc. It’s every the same error.
I can only do a restart about reset and than the cycle is the same.
Have you a idea, what I can do.
In google are a lot of articles with the same problem, but I found not a result for the problem.
Best regards
Rainhard
What ESP32 pins does this require? For example, using any WiFi requires GPIO pins 2,12-15.
Can you show how to code a speed test, especially one that uses the max data size allowed by BLE?
What happens if the connection is lost? How does the BLE client or server know that the connection was lost, and how does they know which packets were received OK (IOW, is there an ACK signal?)
Hi!
Great tutorial, worked fine first try with Android!
That said, what is the methodology to add/change(?) the messages that are sent to a phone?
I have a LoRa GPS tracking system that will send coordinates to a base station and when I press a button on the base station, I’d like to send that coordinate to the phone, either changing the current BLE characteristic (text, like: “46.0594760,-073.6241978”), or creating a new one.
Ideas?