Some content is hidden

Large Commits have some content hidden by default. Use the searcx below for content that may be hidden.

72 files changed

+9910
-3201
lines changed
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "esp32-hal-bt.h"
1616

1717
#if SOC_BT_SUPPORTED
18-
#if defined(CONFIG_BT_BLUEDROID_ENABLED) && __has_include("esp_bt.h")
18+
#if (defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)) && __has_include("esp_bt.h")
1919

2020
#if CONFIG_IDF_TARGET_ESP32
2121
bool btInUse() {
@@ -116,7 +116,7 @@ bool btStop() {
116116
return false;
117117
}
118118

119-
#else // CONFIG_BT_ENABLED
119+
#else // !__has_include("esp_bt.h") || !(defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED))
120120
bool btStarted() {
121121
return false;
122122
}
@@ -129,6 +129,6 @@ bool btStop() {
129129
return false;
130130
}
131131

132-
#endif /* CONFIG_BT_ENABLED */
132+
#endif /* !__has_include("esp_bt.h") || !(defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)) */
133133

134134
#endif /* SOC_BT_SUPPORTED */
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#include "esp_ota_ops.h"
2626
#endif //CONFIG_APP_ROLLBACK_ENABLE
2727
#include "esp_private/startup_internal.h"
28-
#if defined(CONFIG_BT_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED && __has_include("esp_bt.h")
28+
#if (defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)) && SOC_BT_SUPPORTED && __has_include("esp_bt.h")
2929
#include "esp_bt.h"
30-
#endif //CONFIG_BT_BLUEDROID_ENABLED
30+
#endif
3131
#include <sys/time.h>
3232
#include "soc/rtc.h"
3333
#if !defined(CONFIG_IDF_TARGET_ESP32C2) && !defined(CONFIG_IDF_TARGET_ESP32C6) && !defined(CONFIG_IDF_TARGET_ESP32H2) && !defined(CONFIG_IDF_TARGET_ESP32P4) && !defined(CONFIG_IDF_TARGET_ESP32C5)
@@ -245,7 +245,7 @@ bool verifyRollbackLater() {
245245
}
246246
#endif
247247

248-
#ifdef CONFIG_BT_BLUEDROID_ENABLED
248+
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
249249
#if CONFIG_IDF_TARGET_ESP32
250250
//overwritten in esp32-hal-bt.c
251251
bool btInUse() __attribute__((weak));
@@ -307,7 +307,7 @@ void initArduino() {
307307
if (err) {
308308
log_e("Failed to initialize NVS! Error: %u", err);
309309
}
310-
#if defined(CONFIG_BT_BLUEDROID_ENABLED) && SOC_BT_SUPPORTED
310+
#if (defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)) && SOC_BT_SUPPORTED
311311
if (!btInUse()) {
312312
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
313313
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# ESP32 BLE for Arduino
22
The Arduino IDE provides an excellent library package manager where versions of libraries can be downloaded and installed. This project provides the repository for the ESP32 BLE support for Arduino.
33

4-
The original source of the project, **which is not maintained anymore**, can be found here: https://.com/nkolban/esp32-snippets
4+
The original source of the Bluedroid project, **which is not maintained anymore**, can be found here: https://.com/nkolban/esp32-snippets
55

6-
Issues and questions should be raised here: https://.com/espressif/arduino-esp32/issues <br> (please don't use https://.com/nkolban/esp32-snippets/issues!)
6+
Some parts of the NimBLE implementation are based on the work of h2zero, which can be found here: https://.com/h2zero/NimBLE-Arduino
7+
8+
Issues and questions should be raised here: https://.com/espressif/arduino-esp32/issues <br> (please don't use https://.com/nkolban/esp32-snippets/issues or https://.com/h2zero/NimBLE-Arduino/issues!)
79

810
Documentation for using the library can be found here: https://.com/nkolban/esp32-snippets/tree/master/Documentation
11+
12+
For a more customizable and feature-rich implementation of the NimBLE stack, you can use the [NimBLE-Arduino](https://.com/h2zero/NimBLE-Arduino) library.
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
88
author: chegewara
99
*/
10-
#ifndef SOC_BLE_50_SUPPORTED
11-
#warning "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
10+
#ifndef CONFIG_BLUEDROID_ENABLED
11+
#error "NimBLE does not support extended scan yet. Try using Bluedroid."
12+
#elif !defined(SOC_BLE_50_SUPPORTED)
13+
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
1214
#else
1315

1416
#include <BLEDevice.h>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fqbn_append": "PartitionScheme=huge_app",
33
"requires": [
4-
"CONFIG_SOC_BLE_50_SUPPORTED=y"
4+
"CONFIG_SOC_BLE_50_SUPPORTED=y",
5+
"CONFIG_BLUEDROID_ENABLED=y"
56
]
67
}
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
author: chegewara
77
*/
88

9-
#ifndef CONFIG_BT_BLE_50_FEATURES_SUPPORTED
9+
#ifndef CONFIG_BLUEDROID_ENABLED
10+
#error "NimBLE does not support multi advertising yet. Try using Bluedroid."
11+
#elif !defined(CONFIG_BT_BLE_50_FEATURES_SUPPORTED)
1012
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
1113
#else
1214

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fqbn_append": "PartitionScheme=huge_app",
33
"requires": [
4-
"CONFIG_SOC_BLE_50_SUPPORTED=y"
4+
"CONFIG_SOC_BLE_50_SUPPORTED=y",
5+
"CONFIG_BLUEDROID_ENABLED=y"
56
]
67
}
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
author: chegewara
66
*/
77

8-
#ifndef CONFIG_BT_BLE_50_FEATURES_SUPPORTED
8+
#ifndef CONFIG_BLUEDROID_ENABLED
9+
#error "NimBLE does not support periodic advertising yet. Try using Bluedroid."
10+
#elif !defined(CONFIG_BT_BLE_50_FEATURES_SUPPORTED)
911
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
1012
#else
1113
#include <BLEDevice.h>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fqbn_append": "PartitionScheme=huge_app",
33
"requires": [
4-
"CONFIG_SOC_BLE_50_SUPPORTED=y"
4+
"CONFIG_SOC_BLE_50_SUPPORTED=y",
5+
"CONFIG_BLUEDROID_ENABLED=y"
56
]
67
}
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
88
author: chegewara
99
*/
10-
#ifndef SOC_BLE_50_SUPPORTED
11-
#warning "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
10+
#ifndef CONFIG_BLUEDROID_ENABLED
11+
#error "NimBLE does not support periodic sync yet. Try using Bluedroid."
12+
#elif !defined(SOC_BLE_50_SUPPORTED)
13+
#error "This SoC does not support BLE5. Try using ESP32-C3, or ESP32-S3"
1214
#else
1315
#include <BLEDevice.h>
1416
#include <BLEUtils.h>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"fqbn_append": "PartitionScheme=huge_app",
33
"requires": [
4-
"CONFIG_SOC_BLE_50_SUPPORTED=y"
4+
"CONFIG_SOC_BLE_50_SUPPORTED=y",
5+
"CONFIG_BLUEDROID_ENABLED=y"
56
]
67
}
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,33 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
3636
if (advertisedDevice.haveManufacturerData() == true) {
3737
String strManufacturerData = advertisedDevice.getManufacturerData();
3838

39-
uint8_t cManufacturerData[100];
40-
memcpy(cManufacturerData, strManufacturerData.c_str(), strManufacturerData.length());
39+
// Buffer to store manufacturer data (BLE max is 255 bytes)
40+
uint8_t cManufacturerData[255];
41+
size_t dataLength = strManufacturerData.length();
4142

42-
if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00) {
43-
Serial.println("Found an iBeacon!");
44-
BLEBeacon oBeacon = BLEBeacon();
45-
oBeacon.setData(strManufacturerData);
46-
Serial.printf("iBeacon Frame\n");
47-
Serial.printf(
48-
"ID: %04X Major: %d Minor: %d UUID: %s Power: %d\n", oBeacon.getManufacturerId(), ENDIAN_CHANGE_U16(oBeacon.getMajor()),
49-
ENDIAN_CHANGE_U16(oBeacon.getMinor()), oBeacon.getProximityUUID().toString().c_str(), oBeacon.getSignalPower()
50-
);
51-
} else {
52-
Serial.println("Found another manufacturers beacon!");
53-
Serial.printf("strManufacturerData: %d ", strManufacturerData.length());
54-
for (int i = 0; i < strManufacturerData.length(); i++) {
55-
Serial.printf("[%X]", cManufacturerData[i]);
43+
// Bounds checking to prevent buffer overflow
44+
if (dataLength <= sizeof(cManufacturerData)) {
45+
memcpy(cManufacturerData, strManufacturerData.c_str(), dataLength);
46+
47+
if (dataLength == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00) {
48+
Serial.println("Found an iBeacon!");
49+
BLEBeacon oBeacon = BLEBeacon();
50+
oBeacon.setData(strManufacturerData);
51+
Serial.printf("iBeacon Frame\n");
52+
Serial.printf(
53+
"ID: %04X Major: %d Minor: %d UUID: %s Power: %d\n", oBeacon.getManufacturerId(), ENDIAN_CHANGE_U16(oBeacon.getMajor()),
54+
ENDIAN_CHANGE_U16(oBeacon.getMinor()), oBeacon.getProximityUUID().toString().c_str(), oBeacon.getSignalPower()
55+
);
56+
} else {
57+
Serial.println("Found another manufacturers beacon!");
58+
Serial.printf("strManufacturerData: %d ", dataLength);
59+
for (int i = 0; i < dataLength; i++) {
60+
Serial.printf("[%X]", cManufacturerData[i]);
61+
}
62+
Serial.printf("\n");
5663
}
57-
Serial.printf("\n");
64+
} else {
65+
Serial.printf("Manufacturer data too large (%d bytes), skipping\n", dataLength);
5866
}
5967
}
6068

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

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();
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
}
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
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name=BLE
22
version=3.2.0
33
author=Neil Kolban <[email protected]>
4-
maintainer=Dariusz Krempa <[email protected]>
4+
maintainer=lucasssvaz
55
sentence=BLE functions for ESP32
66
paragraph=This library provides an implementation Bluetooth Low Energy support for the ESP32 using the Arduino platform.
77
category=Communication

0 commit comments

Comments
 (0)