Skip to content

Commit 6ac6241

Browse files
committed
[BREAKING] - Refactor NimBLEScan
* General code cleanup * `NimBLEScan::start` will no longer clear cache or results if scanning is already in progress. * `NimBLEScan::clearResults` will now reset the vector capacity to 0. * `NimBLEScan::stop` will no longer call the `onScanEnd` callback as the caller should know its been stopped when this is called. * `NimBLEScan::clearDuplicateCache` has been removed as it was problematic and only for the esp32. Stop and start the scanner for the same effect. * `NimBLEScan::start` takes a new bool parameter `restart`, default `true`, that will restart an already in progress scan and clear the duplicate filter so all devices will be discovered again. * Scan response data that is received without advertisement first will now create the device and send a callback. * Added new method: `NimBLEAdvertisedDevice::isScannable()` that returns true if the device is scannable. * Added default callbacks for `NimBLEScanCallbacks` * `NimBLEScanCallbacks` function signatures updated: * - `onDiscovered` now takes a `const NimBLEAdvertisedDevice*` * - `onResult` now takes a `const NimBLEAdvertisedDevice*` * - `onScanEnd` now takes a `const NimBLEScanResults&` and `int reason` * Added new erase overload: `NimBLEScan::erase(const NimBLEAdvertisedDevice* device)` * `NimBLEScanResults::getDevice` methods now return `const NimBLEAdvertisedDevice*` * `NimBLEScanResults` iterators are now `const_iterator`
1 parent b719a39 commit 6ac6241

File tree

12 files changed

+299
-354
lines changed

12 files changed

+299
-354
lines changed

examples/Bluetooth_5/NimBLE_extended_client/NimBLE_extended_client.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define SERVICE_UUID "ABCD"
1717
#define CHARACTERISTIC_UUID "1234"
1818

19-
static NimBLEAdvertisedDevice* advDevice;
19+
static const NimBLEAdvertisedDevice* advDevice;
2020
static bool doConnect = false;
2121
static uint32_t scanTime = 10 * 1000; // In milliseconds, 0 = scan forever
2222

@@ -40,7 +40,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
4040
/* Define a class to handle the callbacks when advertisements are received */
4141
class scanCallbacks: public NimBLEScanCallbacks {
4242

43-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
43+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
4444
Serial.printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
4545
if(advertisedDevice->isAdvertisingService(NimBLEUUID("ABCD")))
4646
{
@@ -55,8 +55,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
5555
}
5656

5757
/** Callback to process the results of the completed scan or restart it */
58-
void onScanEnd(NimBLEScanResults results) {
59-
Serial.println("Scan Ended");
58+
void onScanEnd(const NimBLEScanResults& results, int reason) {
59+
Serial.print("Scan Ended; reason = "); Serial.println(reason);
6060
}
6161
};
6262

examples/NimBLE_Async_Client/NimBLE_Async_Client.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
2525
} clientCB;
2626

2727
class scanCallbacks : public NimBLEScanCallbacks {
28-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
28+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
2929
Serial.printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
3030
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
3131
Serial.println("Found Our Device");
@@ -48,8 +48,8 @@ class scanCallbacks : public NimBLEScanCallbacks {
4848
}
4949
}
5050

51-
void onScanEnd(NimBLEScanResults results) {
52-
Serial.println("Scan Ended");
51+
void onScanEnd(const NimBLEScanResults& results, int reason) {
52+
Serial.print("Scan Ended; reason = "); Serial.println(reason);
5353
NimBLEDevice::getScan()->start(scanTimeMs);
5454
}
5555
};

examples/NimBLE_Client/NimBLE_Client.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <NimBLEDevice.h>
1212

13-
static NimBLEAdvertisedDevice* advDevice;
13+
static const NimBLEAdvertisedDevice* advDevice;
1414

1515
static bool doConnect = false;
1616
static uint32_t scanTime = 0 * 1000; // In milliseconds, 0 = scan forever
@@ -85,7 +85,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
8585
/** Define a class to handle the callbacks when advertisments are received */
8686
class scanCallbacks: public NimBLEScanCallbacks {
8787

88-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
88+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
8989
Serial.print("Advertised Device found: ");
9090
Serial.println(advertisedDevice->toString().c_str());
9191
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
@@ -101,8 +101,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
101101
}
102102

103103
/** Callback to process the results of the completed scan or restart it */
104-
void onScanEnd(NimBLEScanResults results) {
105-
Serial.println("Scan Ended");
104+
void onScanEnd(const NimBLEScanResults& results, int reason) {
105+
Serial.print("Scan Ended; reason = "); Serial.println(reason);
106106
}
107107
};
108108

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,49 @@
1-
/** Example of continuous scanning for BLE advertisements.
2-
* This example will scan forever while consuming as few resources as possible
3-
* and report all advertisments on the serial monitor.
1+
/**
2+
* Continuous Scan Example
3+
*
4+
* This example demonstrates how to continuously scan for BLE devices.
5+
* When devices are found the onDiscovered and onResults callbacks will be called with the device data.
6+
* The scan will not store the results, only the callbacks will be used
7+
* When the scan timeout is reached the onScanEnd callback will be called and the scan will be restarted.
8+
* This will clear the duplicate cache in the controller and allow the same devices to be reported again.
49
*
510
* Created: on January 31 2021
611
* Author: H2zero
7-
*
812
*/
913

1014
#include "NimBLEDevice.h"
1115

12-
NimBLEScan* pBLEScan;
16+
static constexpr uint32_t scanTime = 30 * 1000; // 30 seconds scan time.
1317

14-
class scanCallbacks: public NimBLEScanCallbacks {
15-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
16-
Serial.printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
18+
class scanCallbacks : public NimBLEScanCallbacks {
19+
// Initial discovery, advertisement data only.
20+
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) override {
21+
Serial.printf("Discovered Device: %s\n", advertisedDevice->toString().c_str());
1722
}
18-
};
19-
20-
void setup() {
21-
Serial.begin(115200);
22-
Serial.println("Scanning...");
23-
24-
/** *Optional* Sets the filtering mode used by the scanner in the BLE controller.
25-
*
26-
* Can be one of:
27-
* CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE (0) (default)
28-
* Filter by device address only, advertisements from the same address will be reported only once.
29-
*
30-
* CONFIG_BTDM_SCAN_DUPL_TYPE_DATA (1)
31-
* Filter by data only, advertisements with the same data will only be reported once,
32-
* even from different addresses.
33-
*
34-
* CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE (2)
35-
* Filter by address and data, advertisements from the same address will be reported only once,
36-
* except if the data in the advertisement has changed, then it will be reported again.
37-
*
38-
* Can only be used BEFORE calling NimBLEDevice::init.
39-
*/
40-
NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE);
4123

42-
/** *Optional* Sets the scan filter cache size in the BLE controller.
43-
* When the number of duplicate advertisements seen by the controller
44-
* reaches this value it will clear the cache and start reporting previously
45-
* seen devices. The larger this number, the longer time between repeated
46-
* device reports. Range 10 - 1000. (default 20)
47-
*
48-
* Can only be used BEFORE calling NimBLEDevice::init.
49-
*/
50-
NimBLEDevice::setScanDuplicateCacheSize(200);
24+
// If active scanning the result here will have the scan response data.
25+
// If not active scanning then this will be the same as onDiscovered.
26+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
27+
Serial.printf("Device result: %s\n", advertisedDevice->toString().c_str());
28+
}
5129

52-
NimBLEDevice::init("");
30+
void onScanEnd(const NimBLEScanResults& results, int reason) override {
31+
Serial.printf("Scan ended reason = %d; restarting scan\n", reason);
32+
NimBLEDevice::getScan()->start(scanTime, false, true);
33+
}
34+
} scanCallbacks; // create a callback class instance.
5335

54-
pBLEScan = NimBLEDevice::getScan(); //create new scan
55-
// Set the callback for when devices are discovered, no duplicates.
56-
pBLEScan->setScanCallbacks(new scanCallbacks(), false);
57-
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser.
58-
pBLEScan->setInterval(97); // How often the scan occurs / switches channels; in milliseconds,
59-
pBLEScan->setWindow(37); // How long to scan during the interval; in milliseconds.
60-
pBLEScan->setMaxResults(0); // do not store the scan results, use callback only.
36+
void setup() {
37+
Serial.begin(115200);
38+
NimBLEDevice::init(""); // Initialize the device, you can specify a device name if you want.
39+
NimBLEScan* pBLEScan = NimBLEDevice::getScan(); // Create the scan object.
40+
pBLEScan->setScanCallbacks(&scanCallbacks, false); // Set the callback for when devices are discovered, no duplicates.
41+
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser.
42+
pBLEScan->setMaxResults(0); // Do not store the scan results, use callback only.
43+
pBLEScan->start(scanTime, false, true); // duration, not a continuation of last scan, restart to get all devices again.
44+
Serial.println("Scanning...");
6145
}
6246

6347
void loop() {
64-
// If an error occurs that stops the scan, it will be restarted here.
65-
if(pBLEScan->isScanning() == false) {
66-
// Start scan with: duration = 0 seconds(forever), no scan end callback, not a continuation of a previous scan.
67-
pBLEScan->start(0, false);
68-
}
69-
70-
delay(2000);
71-
}
48+
delay(2000);
49+
}

examples/NimBLE_active_passive_scan/NimBLE_active_passive_scan.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ bool active = false;
1212

1313
class scanCallbacks: public NimBLEScanCallbacks {
1414

15-
void onDiscovered(NimBLEAdvertisedDevice* advertisedDevice) {
15+
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) {
1616
Serial.printf("Discovered Advertised Device: %s \n", advertisedDevice->toString().c_str());
1717
}
1818

19-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
19+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
2020
Serial.printf("Advertised Device Result: %s \n", advertisedDevice->toString().c_str());
2121
}
2222

23-
void onScanEnd(NimBLEScanResults results){
24-
Serial.println("Scan Ended");
23+
void onScanEnd(const NimBLEScanResults& results, int reason) {
24+
Serial.print("Scan Ended; reason = "); Serial.println(reason);
2525
active = !active;
2626
pBLEScan->setActiveScan(active);
2727
Serial.printf("scan start, active = %u\n", active);

examples/Refactored_original_examples/BLE_client/BLE_client.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ static boolean doConnect = false;
2222
static boolean connected = false;
2323
static boolean doScan = false;
2424
static BLERemoteCharacteristic* pRemoteCharacteristic;
25-
static BLEAdvertisedDevice* myDevice;
25+
/* const required now */
26+
/* static BLEAdvertisedDevice* myDevice;*/
27+
static const BLEAdvertisedDevice* myDevice;
2628

2729
static void notifyCallback(
2830
BLERemoteCharacteristic* pBLERemoteCharacteristic,
@@ -128,9 +130,9 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
128130
* Called for each advertising BLE server.
129131
*/
130132

131-
/*** Only a reference to the advertised device is passed now
133+
/*** Only a const pointer to the advertised device is passed now
132134
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
133-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
135+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
134136
Serial.print("BLE Advertised Device found: ");
135137
Serial.println(advertisedDevice->toString().c_str());
136138

examples/Refactored_original_examples/BLE_scan/BLE_scan.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
1818
BLEScan* pBLEScan;
1919

2020
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
21-
/*** Only a reference to the advertised device is passed now
21+
/*** Only a const pointer to the advertised device is passed now
2222
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
23-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
23+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
2424
/** Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); **/
2525
Serial.printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
2626
}

src/NimBLEAdvertisedDevice.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,14 @@ bool NimBLEAdvertisedDevice::isConnectable() const {
757757
return (m_advType & BLE_HCI_ADV_CONN_MASK) || (m_advType & BLE_HCI_ADV_DIRECT_MASK);
758758
} // isConnectable
759759

760+
/**
761+
* @brief Check if this device is advertising as scannable.
762+
* @return True if the device is scannable.
763+
*/
764+
bool NimBLEAdvertisedDevice::isScannable() const {
765+
return isLegacyAdvertisement() && (m_advType == BLE_HCI_ADV_TYPE_ADV_IND || m_advType == BLE_HCI_ADV_TYPE_ADV_SCAN_IND);
766+
} // isScannable
767+
760768
/**
761769
* @brief Check if this advertisement is a legacy or extended type
762770
* @return True if legacy (Bluetooth 4.x), false if extended (bluetooth 5.x).

src/NimBLEAdvertisedDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class NimBLEAdvertisedDevice {
8282
bool haveType(uint16_t type) const;
8383
std::string toString() const;
8484
bool isConnectable() const;
85+
bool isScannable() const;
8586
bool isLegacyAdvertisement() const;
8687
# if CONFIG_BT_NIMBLE_EXT_ADV
8788
uint8_t getSetId() const;

src/NimBLEDevice.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,7 @@ void NimBLEDevice::onReset(int reason) {
723723

724724
m_synced = false;
725725

726-
NIMBLE_LOGE(LOG_TAG, "Resetting state; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));
727-
728-
# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
729-
if (m_initialized) {
730-
if (m_pScan != nullptr) {
731-
m_pScan->onHostReset();
732-
}
733-
}
734-
# endif
726+
NIMBLE_LOGE(LOG_TAG, "Host reset; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));
735727
} // onReset
736728

737729
/**

0 commit comments

Comments
 (0)