Skip to content

Commit 9f3fe00

Browse files
committed
fix(nimble): Fix examples and permissions
1 parent 8f31681 commit 9f3fe00

File tree

6 files changed

+59
-26
lines changed

6 files changed

+59
-26
lines changed

libraries/BLE/examples/Client/Client.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static boolean doScan = false;
1919
static BLERemoteCharacteristic *pRemoteCharacteristic;
2020
static BLEAdvertisedDevice *myDevice;
2121

22+
// Callback function to handle notifications
2223
static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) {
2324
Serial.print("Notify callback for characteristic ");
2425
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
@@ -80,6 +81,7 @@ bool connectToServer() {
8081
}
8182

8283
if (pRemoteCharacteristic->canNotify()) {
84+
// Register/Subscribe for notifications
8385
pRemoteCharacteristic->registerForNotify(notifyCallback);
8486
}
8587

libraries/BLE/examples/Notify/Notify.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ void setup() {
6969
);
7070

7171
// Creates BLE Descriptor 0x2902: Client Characteristic Configuration Descriptor (CCCD)
72+
// Descriptor 2902 is not required when using NimBLE as it is automatically added based on the characteristic properties
7273
pCharacteristic->addDescriptor(new BLE2902());
7374
// Adds also the Characteristic User Description - 0x2901 descriptor
7475
descriptor_2901 = new BLE2901();

libraries/BLE/examples/Server_multiconnect/Server_multiconnect.ino

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
updated by chegewara
66
77
Create a BLE server that, once we receive a connection, will send periodic notifications.
8+
The server will continue advertising for more connections after the first one and will notify
9+
the value of a counter to all connected clients.
10+
811
The service advertises itself as: 4fafc201-1fb5-459e-8fcc-c5c9c331914b
912
And has a characteristic of: beb5483e-36e1-4688-b7f5-ea07361b26a8
1013
@@ -26,8 +29,8 @@
2629

2730
BLEServer *pServer = NULL;
2831
BLECharacteristic *pCharacteristic = NULL;
32+
int connectedClients = 0;
2933
bool deviceConnected = false;
30-
bool oldDeviceConnected = false;
3134
uint32_t value = 0;
3235

3336
// See the following for generating UUIDs:
@@ -38,12 +41,17 @@ uint32_t value = 0;
3841

3942
class MyServerCallbacks : public BLEServerCallbacks {
4043
void onConnect(BLEServer *pServer) {
41-
deviceConnected = true;
44+
connectedClients++;
45+
Serial.print("Client connected. Total clients: ");
46+
Serial.println(connectedClients);
47+
// Continue advertising for more connections
4248
BLEDevice::startAdvertising();
4349
};
4450

4551
void onDisconnect(BLEServer *pServer) {
46-
deviceConnected = false;
52+
connectedClients--;
53+
Serial.print("Client disconnected. Total clients: ");
54+
Serial.println(connectedClients);
4755
}
4856
};
4957

@@ -66,8 +74,7 @@ void setup() {
6674
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
6775
);
6876

69-
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
70-
// Create a BLE Descriptor
77+
// Descriptor 2902 is not required when using NimBLE as it is automatically added based on the characteristic properties
7178
pCharacteristic->addDescriptor(new BLE2902());
7279

7380
// Start the service
@@ -79,27 +86,38 @@ void setup() {
7986
pAdvertising->setScanResponse(false);
8087
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
8188
BLEDevice::startAdvertising();
82-
Serial.println("Waiting a client connection to notify...");
89+
Serial.println("Waiting for client connections to notify...");
8390
}
8491

8592
void loop() {
86-
// notify changed value
87-
if (deviceConnected) {
93+
// Notify changed value to all connected clients
94+
if (connectedClients > 0) {
95+
Serial.print("Notifying value: ");
96+
Serial.print(value);
97+
Serial.print(" to ");
98+
Serial.print(connectedClients);
99+
Serial.println(" client(s)");
88100
pCharacteristic->setValue((uint8_t *)&value, 4);
89101
pCharacteristic->notify();
90102
value++;
91-
delay(10); // 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
103+
// Bluetooth stack will go into congestion, if too many packets are sent.
104+
// In 6 hours of testing, I was able to go as low as 3ms.
105+
// When using core debug level "debug" or "verbose", the delay can be increased in
106+
// order to reduce the number of debug messages in the serial monitor.
107+
delay(100);
92108
}
93-
// disconnecting
94-
if (!deviceConnected && oldDeviceConnected) {
109+
110+
// Disconnecting - restart advertising when no clients are connected
111+
if (connectedClients == 0 && deviceConnected) {
95112
delay(500); // give the bluetooth stack the chance to get things ready
96113
pServer->startAdvertising(); // restart advertising
97-
Serial.println("start advertising");
98-
oldDeviceConnected = deviceConnected;
114+
Serial.println("No clients connected, restarting advertising");
115+
deviceConnected = false;
99116
}
100-
// connecting
101-
if (deviceConnected && !oldDeviceConnected) {
102-
// do stuff here on connecting
103-
oldDeviceConnected = deviceConnected;
117+
118+
// Connecting - update state when first client connects
119+
if (connectedClients > 0 && !deviceConnected) {
120+
// do stuff here on first connecting
121+
deviceConnected = true;
104122
}
105123
}

libraries/BLE/examples/UART/UART.ino

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ uint8_t txValue = 0;
4040
class MyServerCallbacks : public BLEServerCallbacks {
4141
void onConnect(BLEServer *pServer) {
4242
deviceConnected = true;
43+
Serial.println("Device connected");
4344
};
4445

4546
void onDisconnect(BLEServer *pServer) {
4647
deviceConnected = false;
48+
Serial.println("Device disconnected");
4749
}
4850
};
4951

@@ -80,6 +82,7 @@ void setup() {
8082
// Create a BLE Characteristic
8183
pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);
8284

85+
// Descriptor 2902 is not required when using NimBLE as it is automatically added based on the characteristic properties
8386
pTxCharacteristic->addDescriptor(new BLE2902());
8487

8588
BLECharacteristic *pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
@@ -97,22 +100,24 @@ void setup() {
97100
void loop() {
98101

99102
if (deviceConnected) {
103+
Serial.print("Notifying Value: ");
104+
Serial.println(txValue);
100105
pTxCharacteristic->setValue(&txValue, 1);
101106
pTxCharacteristic->notify();
102107
txValue++;
103-
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
108+
delay(1000); // Notifying every 1 second
104109
}
105110

106111
// disconnecting
107112
if (!deviceConnected && oldDeviceConnected) {
108113
delay(500); // give the bluetooth stack the chance to get things ready
109114
pServer->startAdvertising(); // restart advertising
110-
Serial.println("start advertising");
111-
oldDeviceConnected = deviceConnected;
115+
Serial.println("Started advertising again...");
116+
oldDeviceConnected = false;
112117
}
113118
// connecting
114119
if (deviceConnected && !oldDeviceConnected) {
115120
// do stuff here on connecting
116-
oldDeviceConnected = deviceConnected;
121+
oldDeviceConnected = true;
117122
}
118123
}

libraries/BLE/src/BLEDescriptor.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@
3939
***************************************************************************/
4040

4141
#if defined(CONFIG_NIMBLE_ENABLED)
42-
#include "host/ble_att.h"
42+
#include <host/ble_att.h>
4343
#include "BLEConnInfo.h"
44+
45+
#define ESP_GATT_PERM_READ BLE_ATT_F_READ
46+
#define ESP_GATT_PERM_WRITE BLE_ATT_F_WRITE
47+
#define ESP_GATT_PERM_READ_ENCRYPTED BLE_ATT_F_READ_ENC
48+
#define ESP_GATT_PERM_WRITE_ENCRYPTED BLE_ATT_F_WRITE_ENC
49+
#define ESP_GATT_PERM_READ_AUTHORIZATION BLE_ATT_F_READ_AUTHOR
50+
#define ESP_GATT_PERM_WRITE_AUTHORIZATION BLE_ATT_F_WRITE_AUTHOR
51+
#define ESP_GATT_PERM_READ_ENC_MITM BLE_ATT_F_READ_AUTHEN
52+
#define ESP_GATT_PERM_WRITE_ENC_MITM BLE_ATT_F_WRITE_AUTHEN
53+
4454
#endif
4555

4656
/***************************************************************************

libraries/BLE/src/BLEHIDDevice.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121

2222
#include "BLEHIDDevice.h"
2323
#include "BLE2904.h"
24+
#include "BLEDescriptor.h"
2425

2526
/***************************************************************************
2627
* NimBLE includes and definitions *
2728
***************************************************************************/
2829

2930
#ifdef CONFIG_NIMBLE_ENABLED
3031
#include <host/ble_att.h>
31-
#define ESP_GATT_PERM_READ BLE_ATT_F_READ
32-
#define ESP_GATT_PERM_WRITE BLE_ATT_F_WRITE
33-
#define ESP_GATT_PERM_READ_ENCRYPTED BLE_ATT_F_READ_ENC
34-
#define ESP_GATT_PERM_WRITE_ENCRYPTED BLE_ATT_F_WRITE_ENC
3532
#endif
3633

3734
/***************************************************************************

0 commit comments

Comments
 (0)