Skip to content

Commit 99750cd

Browse files
authored
Adds BLE Characteristic User Description 0x2901 Descriptor (#9883)
Adds a class for 0x2901 - Characteristic User Descriptor. This Descriptor is usual in BLE and describes with text what each characteristic is about. Improve Notify.ino example by adding the 0x2901 descriptor
1 parent 6b22339 commit 99750cd

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ set(ARDUINO_LIBRARY_WiFiProv_SRCS libraries/WiFiProv/src/WiFiProv.cpp)
236236
set(ARDUINO_LIBRARY_Wire_SRCS libraries/Wire/src/Wire.cpp)
237237

238238
set(ARDUINO_LIBRARY_BLE_SRCS
239+
libraries/BLE/src/BLE2901.cpp
239240
libraries/BLE/src/BLE2902.cpp
240241
libraries/BLE/src/BLE2904.cpp
241242
libraries/BLE/src/BLEAddress.cpp

libraries/BLE/examples/Notify/Notify.ino

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
#include <BLEServer.h>
2424
#include <BLEUtils.h>
2525
#include <BLE2902.h>
26+
#include <BLE2901.h>
2627

2728
BLEServer *pServer = NULL;
2829
BLECharacteristic *pCharacteristic = NULL;
30+
BLE2901 *descriptor_2901 = NULL;
31+
2932
bool deviceConnected = false;
3033
bool oldDeviceConnected = false;
3134
uint32_t value = 0;
@@ -65,9 +68,13 @@ void setup() {
6568
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
6669
);
6770

68-
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
69-
// Create a BLE Descriptor
71+
// Creates BLE Descriptor 0x2902: Client Characteristic Configuration Descriptor (CCCD)
7072
pCharacteristic->addDescriptor(new BLE2902());
73+
// Adds also the Characteristic User Description - 0x2901 descriptor
74+
descriptor_2901 = new BLE2901();
75+
descriptor_2901->setDescription("My own description for this characteristic.");
76+
descriptor_2901->setAccessPermissions(ESP_GATT_PERM_READ); // enforce read only - default is Read|Write
77+
pCharacteristic->addDescriptor(descriptor_2901);
7178

7279
// Start the service
7380
pService->start();
@@ -87,7 +94,7 @@ void loop() {
8794
pCharacteristic->setValue((uint8_t *)&value, 4);
8895
pCharacteristic->notify();
8996
value++;
90-
delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
97+
delay(500);
9198
}
9299
// disconnecting
93100
if (!deviceConnected && oldDeviceConnected) {

libraries/BLE/src/BLE2901.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
BLE2901.h
3+
4+
GATT Descriptor 0x2901 Characteristic User Description
5+
6+
The value of this description is a user-readable string
7+
describing the characteristic.
8+
9+
The Characteristic User Description descriptor
10+
provides a textual user description for a characteristic
11+
value.
12+
If the Writable Auxiliary bit of the Characteristics
13+
Properties is set then this descriptor is written. Only one
14+
User Description descriptor exists in a characteristic
15+
definition.
16+
*/
17+
18+
#include "soc/soc_caps.h"
19+
#if SOC_BLE_SUPPORTED
20+
21+
#include "sdkconfig.h"
22+
#if defined(CONFIG_BLUEDROID_ENABLED)
23+
24+
#include "BLE2901.h"
25+
26+
BLE2901::BLE2901() : BLEDescriptor(BLEUUID((uint16_t)0x2901)) {} // BLE2901
27+
28+
/**
29+
* @brief Set the Characteristic User Description
30+
*/
31+
void BLE2901::setDescription(String userDesc) {
32+
if (userDesc.length() > ESP_GATT_MAX_ATTR_LEN) {
33+
log_e("Size %d too large, must be no bigger than %d", userDesc.length(), ESP_GATT_MAX_ATTR_LEN);
34+
return;
35+
}
36+
setValue(userDesc);
37+
}
38+
39+
#endif
40+
#endif /* SOC_BLE_SUPPORTED */

libraries/BLE/src/BLE2901.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
BLE2901.h
3+
4+
GATT Descriptor 0x2901 Characteristic User Description
5+
6+
The value of this description is a user-readable string
7+
describing the characteristic.
8+
9+
The Characteristic User Description descriptor
10+
provides a textual user description for a characteristic
11+
value.
12+
If the Writable Auxiliary bit of the Characteristics
13+
Properties is set then this descriptor is written. Only one
14+
User Description descriptor exists in a characteristic
15+
definition.
16+
17+
*/
18+
19+
#ifndef COMPONENTS_CPP_UTILS_BLE2901_H_
20+
#define COMPONENTS_CPP_UTILS_BLE2901_H_
21+
#include "soc/soc_caps.h"
22+
#if SOC_BLE_SUPPORTED
23+
24+
#include "sdkconfig.h"
25+
#if defined(CONFIG_BLUEDROID_ENABLED)
26+
27+
#include "BLEDescriptor.h"
28+
29+
class BLE2901 : public BLEDescriptor {
30+
public:
31+
BLE2901();
32+
void setDescription(String desc);
33+
}; // BLE2901
34+
35+
#endif /* CONFIG_BLUEDROID_ENABLED */
36+
#endif /* SOC_BLE_SUPPORTED */
37+
#endif /* COMPONENTS_CPP_UTILS_BLE2901_H_ */

0 commit comments

Comments
 (0)