Professional Documents
Culture Documents
BLE Server
BLE Server
16-20 minutes
The BLE client discovers and listens to BLE devices that are A server advertises on the radio frequency by advertising
advertising. That means the BLE client initiates the process of connectable advertisement packets, its device name, and unique
establishing a connection to the BLE servers. UUID then on another in a process known as frequency hopping.
In doing so, it reduces radio interference created from reflected
signals or other devices.
Scanning
Connecting
Attribute Profile (GATT) At the top of the hierarchy, you can find the Profile which
consists of one or more Services. These are the set of Services
BLE GATT Profile that are defined by the designers or by Bluetooth Special Interest
Group (SIG).
BLE clients and servers devices transmit and receive data
through Generic Attribute Profile (GATT). In this protocol, the GATT Services
central devices act as clients and any peripheral device is the
server. The server has Characteristics, Services, and a particular These are sets of simple data for example readings from a
ATT table that is used in the transmission and reception of data. sensor and are divided into subcategories known
The GATT protocol consists of a hierarchy in which there are as Characteristics. Pre-defined Services by Bluetooth SIG are
different sections known as Profiles, Services, and available such as reading heart rate, Weight Scale, Blood
Characteristics. The figure below shows these different sections pressure, etc. To view all the official services, click here.
in their particular order and positions. UUID or Universally Unique Identifier is the unique ID code of a
particular Service. It can be 16 or 128 bits depending on the
service. We can use the preset UUIDs or customize our own for
a particular service. To obtain a unique UUID go to the online
website UUIDGenerator.
Characteristics
Characteristic value. They contain a different type of metadata #define BatteryService BLEUUID((uint16_t)0x180F)
through which they expand the Characteristic. The Properties
exercise the role of permitting operations including read, notify, BLECharacteristic
write, indicate, or broadcast. These are the functions of a BatteryLevelCharacteristic(BLEUUID((uint16_t)0x2A19),
particular value that is defined by property. This property is then BLECharacteristic::PROPERTY_READ |
linked with a particular Characteristic. BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor
After getting to know about the necessary BLE terminologies, let
BatteryLevelDescriptor(BLEUUID((uint16_t)0x2901));
us move ahead and program our ESP32 module as a BLE
server. We will create a battery level indicator using the GATT
service. For this we will require the nRF android application on
class MyServerCallbacks : public BLEServerCallbacks {
our smartphone. Moreover, we will need a phone which supports
void onConnect(BLEServer* pServer) {
BLE connection.
_BLEClientConnected = true;
We will use Arduino IDE to program our ESP32 development
};
board. Thus, you should have the latest version of Arduino IDE.
Additionally, you also need to install the ESP32 plugin. If your
IDE does not have the plugin installed you can visit the link
void onDisconnect(BLEServer* pServer) {
below:
_BLEClientConnected = false;
Installing ESP32 library in Arduino IDE and upload code. }
Open your Arduino IDE and go to File > New. Copy the following };
pServer->setCallbacks(new MyServerCallbacks());
bool _BLEClientConnected = false;
uint8_t battery_level = 0;
// Create the BLE Service
BLEService *pBattery = void loop() {
pServer->createService(BatteryService); BatteryLevelCharacteristic.setValue(&battery_level,
1);
pBattery->addCharacteristic(&BatteryLevelCharacteristic BatteryLevelCharacteristic.notify();
BatteryLevelDescriptor.setValue("Percentage 0 - delay(5000);
100"); battery_level++;
Serial.println(int(battery_level));
BatteryLevelCharacteristic.addDescriptor(&BatteryLevelDescriptor if (int(battery_level) == 100)
BatteryLevelCharacteristic.addDescriptor(new battery_level = 0;
BLE2902()); }
pBattery->addCharacteristic(&BatteryLevelCharacteristic
pServer->getAdvertising()->addServiceUUID(BatteryService BatteryLevelDescriptor.setValue("Percentage 0 -
pBattery->start(); 100");
First, define your BLE device by specifying the name of your After the service has started, we will use getAdvertising() to
choice in the init command. In our case, we our device name is initiate the advertising process so that nearby devices would be
‘BLE Battery.’ able to spot our server and connect with it. After a connection
had been made, the Battery Service is started. We will then
BLEDevice::init("BLE Battery");
launch the server, using the start() command.
Next, we create the BLE server by using
pServer->getAdvertising()->addServiceUUID(BatteryService
the createServer() function and set the server callback.
pBattery->start();
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks()); setup()
Then, the BLE GATT service is created. After that, we will add
Inside the setup() function, we open the serial communication at
the Characteristic on our server. By using the setValue() method,
we will set the ‘Value’ of the characteristic with the a baud rate of 115200 and print “BLE Battery Level Indicator” on
the serial monitor. Then InitBLE() function is called which
text: “Percentage 0-100.” You can change this value according to
initializes our BLE device.
your liking. After we start the BLE GATT service, the descriptor
along with the characteristics is joined and the values are set. void setup() {
Additionally, BLE2902 service is also included. Serial.begin(115200);
Serial.println("BLE Battery Level Indicator");
BLEService *pBattery =
InitBLE();
pServer->createService(BatteryService);
}
loop()
uint8_t battery_level = 0;
void loop() {
BatteryLevelCharacteristic.setValue(&battery_level,
1);
BatteryLevelCharacteristic.notify();
delay(5000);
battery_level++;
Serial.println(int(battery_level));
if (int(battery_level) == 100)
battery_level = 0;
}
Then, go to Tools > Port and select the appropriate port through
Demonstration which your board is connected.
Choose the correct board and COM port before uploading your
code to the board. Therefore go to Tools > Board and
select ESP32 Dev Module.
In your Arduino IDE, open up the serial monitor and set the baud
rate to 115200. You will be able to view the battery value. It will
increment after every 5 seconds.
Before proceeding further, enable the Bluetooth on your Now, open the nRF Connect for Mobile application and tap the
smartphone and pair it with “BLE Battery” which is the name of Scan button. This will scan for all the devices which are close to
our BLE device. the cell phone. Thus, our BLE device will also get scanned and
you will be able to see ‘BLE Battery’ written among the scanned
devices as shown below. Connect with it.
Now, tap the Connect button and the following appears on the
cell phone screen.