Skip to content

Commit 322dd8e

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 5f2730d commit 322dd8e

File tree

14 files changed

+321
-292
lines changed

14 files changed

+321
-292
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ jobs:
2222
example:
2323
- Advanced/NimBLE_Client
2424
- Advanced/NimBLE_Server
25-
- basic/BLE_client
26-
- basic/BLE_notify
27-
- basic/BLE_scan
28-
- basic/BLE_server
29-
- basic/BLE_uart
25+
- Continuous_scan
3026
- Bluetooth_5/NimBLE_extended_client
3127
- Bluetooth_5/NimBLE_extended_server
3228
- Bluetooth_5/NimBLE_multi_advertiser

examples/Advanced/NimBLE_Client/main/main.cpp

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

1212
extern "C" {void app_main(void);}
1313

14-
static NimBLEAdvertisedDevice* advDevice;
14+
static const NimBLEAdvertisedDevice* advDevice;
1515

1616
static bool doConnect = false;
1717
static uint32_t scanTime = 0; /** scan time in milliseconds, 0 = scan forever */
@@ -67,7 +67,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
6767

6868
/** Define a class to handle the callbacks when advertisments are received */
6969
class scanCallbacks: public NimBLEScanCallbacks {
70-
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
70+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
7171
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
7272
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
7373
{
@@ -82,8 +82,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
8282
}
8383

8484
/** Callback to process the results of the completed scan or restart it */
85-
void onScanEnd(NimBLEScanResults results) {
86-
printf("Scan Ended\n");
85+
void onScanEnd(const NimBLEScanResults& results, int reason) {
86+
printf("Scan Ended, reason: %d, device count: %d\n", reason, results.getCount());
8787
}
8888
};
8989

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(Continuous_scan)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(COMPONENT_SRCS "main.cpp")
2+
set(COMPONENT_ADD_INCLUDEDIRS ".")
3+
4+
register_component()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.
9+
*
10+
* Created: on March 24 2020
11+
* Author: H2zero
12+
*
13+
*/
14+
15+
#include "NimBLEDevice.h"
16+
17+
static constexpr uint32_t scanTime = 30 * 1000; // 30 seconds scan time.
18+
19+
class scanCallbacks : public NimBLEScanCallbacks {
20+
// Initial discovery, advertisement data only.
21+
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) override {
22+
printf("Discovered Device: %s\n", advertisedDevice->toString().c_str());
23+
}
24+
25+
// If active scanning the result here will have the scan response data.
26+
// If not active scanning then this will be the same as onDiscovered.
27+
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
28+
printf("Device result: %s\n", advertisedDevice->toString().c_str());
29+
}
30+
31+
void onScanEnd(const NimBLEScanResults& results, int reason) override {
32+
printf("Scan ended reason = %d; restarting scan\n", reason);
33+
NimBLEDevice::getScan()->start(scanTime, false, true);
34+
}
35+
} scanCallbacks; // create a callback class instance.
36+
37+
extern "C" void app_main() {
38+
NimBLEDevice::setScanDuplicateCacheSize(10); // Set max number of devices that can be stored in the cache.
39+
NimBLEDevice::init(""); // Initialize the device, you can specify a device name if you want.
40+
NimBLEScan* pBLEScan = NimBLEDevice::getScan(); // Create the scan object.
41+
pBLEScan->setScanCallbacks(&scanCallbacks, false); // Set the callback for when devices are discovered, no duplicates.
42+
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser.
43+
pBLEScan->setMaxResults(0); // Do not store the scan results, use callback only.
44+
pBLEScan->start(scanTime, false, true); // duration, not a continuation of last scan, restart to get all devices again.
45+
printf("Scanning...\n");
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Override some defaults so BT stack is enabled
2+
# in this example
3+
4+
#
5+
# BT config
6+
#
7+
CONFIG_BT_ENABLED=y
8+
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
9+
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
10+
CONFIG_BTDM_CTRL_MODE_BTDM=n
11+
CONFIG_BT_BLUEDROID_ENABLED=n
12+
CONFIG_BT_NIMBLE_ENABLED=y

examples/NimBLE_Async_Client/main/main.cpp

Lines changed: 1 addition & 1 deletion
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
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
3030
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
3131
printf("Found Our Device\n");

examples/basic/BLE_client/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static bool doConnect = false;
2424
static bool connected = false;
2525
static bool doScan = false;
2626
static BLERemoteCharacteristic* pRemoteCharacteristic;
27-
static BLEAdvertisedDevice* myDevice;
27+
static const BLEAdvertisedDevice* myDevice;
2828

2929
static void notifyCallback(
3030
BLERemoteCharacteristic* pBLERemoteCharacteristic,
@@ -137,7 +137,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
137137

138138
/*** Only a reference to the advertised device is passed now
139139
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
140-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
140+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
141141
printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str());
142142

143143
// We have found a device, let us now see if it contains the service we are looking for.

examples/basic/BLE_scan/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
2121
BLEScan* pBLEScan;
2222

2323
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
24-
void onResult(BLEAdvertisedDevice* advertisedDevice) {
24+
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
2525
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
2626
}
2727
};
@@ -35,7 +35,7 @@ void scanTask (void * parameter){
3535
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
3636
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
3737
}
38-
38+
3939
vTaskDelete(NULL);
4040
}
4141

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).

0 commit comments

Comments
 (0)