Skip to content

Commit edc66e8

Browse files
committed
[BREAKING] - Refactor NimBLEAdvertising
* General code cleanup. * `NimBLEAdvertisementData` moved to it's own .h and .cpp files. * Added new method, `NimBLEAdvertising::setPreferredParams` that takes the min and max preferred connection parameters as an alternative for `setMinPreferred` and `setMaxPreferred`. * Added new method, `NimBLEAdvertising::setAdvertisingInterval` Sets the advertisement interval for min and max to the same value instead of calling `setMinInterval` and `setMaxInterval` separately if there is not value difference. * `NimBLEAdvertisementData` payload is now stored in `std::vector<uint8_t>` instead of `std::string`. * `NimBLEAdvertisementData::getPayload` now returns `std::vector<uint8_t>` instead of `std::string`. * `NimBLEAdvertisementData::addData` now takes either a `std::vector<uint8_t>` or `uint8_t* + length` instead of `std::string` or `char + length`. * `NimBLEAdvertisementData::setName` now takes an optional `bool` parameter to indicate if the name is complete or incomplete, default = complete. * `NimBLEAdvertising::start` No longer takes a callback pointer parameter, instead the new method `NimBLEAdvertising::setAdvertisingCompleteCallback` should be used. * `NimBLEAdvertising::setAdvertisementType` has been renamed to `NimBLEAdvertising::setConnectableMode` to better reflect it's function. * `NimBLEAdvertising::setScanResponse` has been renamed to `NimBLEAdvertising::enableScanResponse` to better reflect it's function. * Scan response is no longer enabled by default. * Added new method, `NimBLEAdvertising::setDiscoverableMode` to allow applications to control the discoverability of the advertiser. * Advertising the name and TX power of the device will no longer happen by default and should be set manually by the application. * Added overload for `NimBLEAdvertising::setManufacturerData` that takes a `const uint8_t*` and , size_t` paramter. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, ` const uint8_t* data`, ` size_t length` as parameters. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, `const std::vector<uint8_t>&` as parameters. * All `NimBLEAdvertisementData` functions that change data values now return `bool`, true = success. * All `NimBLEAdvertising` functions that change data values now return `bool`, true = success. * `NimBLEAdvertising::setMinPreferred` and `NimBLEAdvertising::setMaxPreferred` have been removed, use `NimBLEAdvertising::setPreferredParams` instead. * All advertising data is now stored in instances of `NimBLEAdvertisingData` and vectors removed from `NimBLEAdvertising`. * `NimBLEAdvertising::setAdvertisementData` and `NimBLEAdvertising::setScanResponseData` now return `bool`, true = success. * Added new method, `NimBLEAdvertisementData::removeData`, which takes a parameter `uint8_t type`, the data type to remove. * Added new method, `NimBLEAdvertisementData::toString`, which will print the data in hex. * Added new method, `NimBLEAdvertising::getAdvertisementData`, which returns a reference to the currently set advertisement data. * Added new method, `NimBLEAdvertising::getScanData`, which returns a reference to the currently set scan response data. * Added overloads for `NimBLEAdvertising::removeServiceUUID` and `NimBLEAdvertisementData::removeServiceUUID` to accept a `const char*` * Added new method, `NimBLEAdvertising::clearData`, which will clear the advertisement and scan response data.
1 parent 03c1e9c commit edc66e8

File tree

12 files changed

+1086
-938
lines changed

12 files changed

+1086
-938
lines changed

examples/BLE_EddystoneTLM_Beacon/BLE_EddystoneTLM_Beacon.ino

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
4. wait
1313
5. Stop advertising.
1414
6. deep sleep
15-
15+
1616
*/
1717

1818
#include "NimBLEDevice.h"
@@ -26,7 +26,7 @@
2626
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
2727

2828
// UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
29-
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d"
29+
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d"
3030

3131
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
3232
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
@@ -96,6 +96,7 @@ void setup()
9696
BLEDevice::setPower(ESP_PWR_LVL_N12);
9797

9898
pAdvertising = BLEDevice::getAdvertising();
99+
pAdvertising->enableScanResponse(true);
99100

100101
setBeacon();
101102
// Start advertising

examples/BLE_EddystoneURL_Beacon/BLE_EddystoneURL_Beacon.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define GPIO_DEEP_SLEEP_DURATION 10 // sleep x seconds and then wake up
2727

2828
// UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)
29-
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d"
29+
#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d"
3030

3131
RTC_DATA_ATTR static time_t last; // remember last boot in RTC Memory
3232
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory
@@ -168,6 +168,7 @@ void setup()
168168
BLEDevice::setPower(ESP_PWR_LVL_N12);
169169

170170
pAdvertising = BLEDevice::getAdvertising();
171+
pAdvertising->enableScanResponse(true);
171172

172173
setBeacon();
173174
// Start advertising

examples/NimBLE_Server/NimBLE_Server.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void setup() {
227227
/** If your device is battery powered you may consider setting scan response
228228
* to false as it will extend battery life at the expense of less data sent.
229229
*/
230-
pAdvertising->setScanResponse(true);
230+
pAdvertising->enableScanResponse(true);
231231
pAdvertising->start();
232232

233233
Serial.println("Advertising Started");

examples/NimBLE_Server_Whitelist/NimBLE_Server_Whitelist.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void setup() {
7171
// Start advertising
7272
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
7373
pAdvertising->addServiceUUID(SERVICE_UUID);
74-
pAdvertising->setScanResponse(false);
74+
pAdvertising->enableScanResponse(false);
7575
pAdvertising->start();
7676
Serial.println("Waiting a client connection to notify...");
7777
}

examples/NimBLE_server_get_client_name/NimBLE_server_get_client_name.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void setup() {
6767

6868
BLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
6969
pAdvertising->addServiceUUID(SERVICE_UUID);
70-
pAdvertising->setScanResponse(true);
70+
pAdvertising->enableScanResponse(true);
7171

7272
pAdvertising->start();
7373
Serial.println("Advertising started, connect with your phone.");

examples/Refactored_original_examples/BLE_notify/BLE_notify.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void setup() {
118118
// Start advertising
119119
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
120120
pAdvertising->addServiceUUID(SERVICE_UUID);
121-
pAdvertising->setScanResponse(false);
121+
pAdvertising->enableScanResponse(false);
122122
/** Note, this could be left out as that is the default value */
123123
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
124124

examples/Refactored_original_examples/BLE_server/BLE_server.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ void setup() {
2929
BLEService *pService = pServer->createService(SERVICE_UUID);
3030
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
3131
CHARACTERISTIC_UUID,
32-
/***** Enum Type NIMBLE_PROPERTY now *****
32+
/***** Enum Type NIMBLE_PROPERTY now *****
3333
BLECharacteristic::PROPERTY_READ |
34-
BLECharacteristic::PROPERTY_WRITE
34+
BLECharacteristic::PROPERTY_WRITE
3535
);
3636
*****************************************/
3737
NIMBLE_PROPERTY::READ |
38-
NIMBLE_PROPERTY::WRITE
38+
NIMBLE_PROPERTY::WRITE
3939
);
4040

4141
pCharacteristic->setValue("Hello World says Neil");
4242
pService->start();
4343
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
4444
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
4545
pAdvertising->addServiceUUID(SERVICE_UUID);
46-
pAdvertising->setScanResponse(true);
46+
pAdvertising->enableScanResponse(true);
4747
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
4848
pAdvertising->setMaxPreferred(0x12);
4949

examples/Refactored_original_examples/BLE_server_multiconnect/BLE_server_multiconnect.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void setup() {
122122
// Start advertising
123123
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
124124
pAdvertising->addServiceUUID(SERVICE_UUID);
125-
pAdvertising->setScanResponse(false);
125+
pAdvertising->enableScanResponse(false);
126126
/** Note, this could be left out as that is the default value */
127127
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
128128

0 commit comments

Comments
 (0)