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

BLE standard defines two ways to transfer data for the server to the client: notification and indication. Notifications and indications are initiated by the Server but enabled by the Client. Notification doesn’t need to be acknowledged, so they are faster and an efficient way to read data continuously. Hence, a server does not know if the message reaches to the client. Indication needs to be acknowledged for communicating. The client sent a confirmation message back to the server, this way server knows that message reached the client. Notification process similar to the reading of data.

ble_notify

The server is not able to send indications or notifications at the beginning of the communication. First, the client must enable notifications and indications permissions on the server side, so, the server is allowed to send indications or notifications. This procedure involves the client to write the Client Characteristic Configuration Descriptor (CCCD) of the characteristic that will be notified/indicated.

Code snippet

The libraries used here are

Define service UUID and characteristics UUID.

All this does is set the “deviceConnected” flag true or false when you connect or disconnect from the ESP32. The callback function that handles receiving data being sent from the client(phone) and Bluetooth connection status.

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

Create BLE server

Create BLE service using the service UUID.

And add characteristics.PROPERTY_READ, PROPERTY_WRITE, PROPERTY_NOTIFY, PROPERTY_INDICATE are the properties of the characteristics.

Start BLE service. Starting a service also means that we can create the corresponding characteristics. Characteristics have various attributes such as UUID, properties, permissions, and values. Properties describe what can be done with characteristics, such as read, write, notify and indicate. Permission describe what value can be allowed eg: read and write.

Start advertising

The assigned value of descriptor for client characteristics configuration is 0x2902.

The Client Characteristic Configuration descriptor defines how the characteristic may be configured by a specific client. The client characteristics configuration descriptor is unique for each client. A client may read and write this descriptor to determine and set the configuration for that client. Authentication and authorization may be required by the server to write this descriptor. The default value for the Client Characteristic Configuration descriptor is 0x00.
Sent the notification in every second after the device connected.

ble_notify1

BLE_notify2

 

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