The ESP32 controller with Bluetooth Low Energy can act as either server and client. The server advertises its existence. And it can be found by other devices and it contains the data that the client can read. The BLE supports two types of modes such as Broadcast mode and Mesh network mode. In broadcast mode, the server transmits data to many clients that are connected and in a mesh network mode, all the devices are connected.
Both server and client have a “SERVICE UUID” to make a connection between server and client. Inside this service, there can be several “characteristics” which are defined by characteristic UUID’s. We use two characteristics TX and RX to send data to and receive data from the client. 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.
Here using the libraries are
#include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h>
Next, we define the service and characteristic UUIDs for both TX and RX (two-way communication).
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
Initialize the ESP32 as a BLE device and set its name.
BLEDevice::init("MyESP32");
Create BLE server
BLEServer *pServer = BLEDevice::createServer();
Create BLE service using the service UUID.
BLEService *pService = pServer->createService(SERVICE_UUID);
And add characteristics
BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE ); pCharacteristic->setValue("Hello World");
Start BLE service.
pService->start();
And start advertising. Advertising allows devices to broadcast information defining their intentions. Which means that when a mobile device has received a Bluetooth message, the recipient has the choice to either accept or decline the message. The recipient needs to positively indicate that they wish to receive marketing messages.
BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start();
So, to create a UUID for your custom services and characteristics you would:
Here using a BLE scanner app to send or receive the data.