Bluetooth Low Energy is a slightly different protocol than traditional 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. The BLE devices are detected through a procedure based on broadcasting advertising packets.
Now in order to know which server and client to connect to, both the server and clients use a “service UUID” which describes the overarching service. Inside this service, there can be several “characteristics” which are defined by characteristic UUID’s. Communicating with a BLE device is performed so-called Characteristics in which some characteristics are read-only and others are write-only.BLE_Write

 

We use two different characteristics, TX and RX under the overarching “service” to send data to and receive data from a client (Android device) via these two channels. 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.

Code snippet

So we using the libraries are

Next, we define the service and characteristic UUIDs for both TX and RX (two-way communication).

Similarly, there’s another callback function callback function that handles receiving data being sent from the client(phone). please remember that you should call back, before starting the write operations.

Initialize the ESP32 as a BLE device and set its name.

Create BLE server

Create BLE service using the service UUID.

And add characteristics, that will allow Clients to send a message to the Server.

Start BLE service. Starting a service also means that we can create the corresponding characteristics.

And start advertising.

So, to create a UUID for your custom services and characteristics you would:

  1. Use a website such as https://www.uuidgenerator.net to generate a UUID (or multiple UUIDs)
  2. Make sure it does not conflict with a reserved UUID (avoid any containing XXXXXXXX-0000-1000-8000-00805F9B34FB where XXXXXXXX is any number).

Here using a BLE scanner app to send or receive the data.BLE_Write1

BLE_Write2

Liked this article! You'll love our video courses.
Checkout our comprehensive pre-recorded ESP32 based Video Course for Internet of Things
More details
Spread the love, share this