-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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`
- Loading branch information
Showing
12 changed files
with
299 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 34 additions & 56 deletions
90
examples/NimBLE_Scan_Continuous/NimBLE_Scan_Continuous.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,49 @@ | ||
/** Example of continuous scanning for BLE advertisements. | ||
* This example will scan forever while consuming as few resources as possible | ||
* and report all advertisments on the serial monitor. | ||
/** | ||
* Continuous Scan Example | ||
* | ||
* This example demonstrates how to continuously scan for BLE devices. | ||
* When devices are found the onDiscovered and onResults callbacks will be called with the device data. | ||
* The scan will not store the results, only the callbacks will be used | ||
* When the scan timeout is reached the onScanEnd callback will be called and the scan will be restarted. | ||
* This will clear the duplicate cache in the controller and allow the same devices to be reported again. | ||
* | ||
* Created: on January 31 2021 | ||
* Author: H2zero | ||
* | ||
*/ | ||
|
||
#include "NimBLEDevice.h" | ||
|
||
NimBLEScan* pBLEScan; | ||
static constexpr uint32_t scanTime = 30 * 1000; // 30 seconds scan time. | ||
|
||
class scanCallbacks: public NimBLEScanCallbacks { | ||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) { | ||
Serial.printf("Advertised Device: %s \n", advertisedDevice->toString().c_str()); | ||
class scanCallbacks : public NimBLEScanCallbacks { | ||
// Initial discovery, advertisement data only. | ||
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) override { | ||
Serial.printf("Discovered Device: %s\n", advertisedDevice->toString().c_str()); | ||
} | ||
}; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println("Scanning..."); | ||
|
||
/** *Optional* Sets the filtering mode used by the scanner in the BLE controller. | ||
* | ||
* Can be one of: | ||
* CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE (0) (default) | ||
* Filter by device address only, advertisements from the same address will be reported only once. | ||
* | ||
* CONFIG_BTDM_SCAN_DUPL_TYPE_DATA (1) | ||
* Filter by data only, advertisements with the same data will only be reported once, | ||
* even from different addresses. | ||
* | ||
* CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE (2) | ||
* Filter by address and data, advertisements from the same address will be reported only once, | ||
* except if the data in the advertisement has changed, then it will be reported again. | ||
* | ||
* Can only be used BEFORE calling NimBLEDevice::init. | ||
*/ | ||
NimBLEDevice::setScanFilterMode(CONFIG_BTDM_SCAN_DUPL_TYPE_DEVICE); | ||
|
||
/** *Optional* Sets the scan filter cache size in the BLE controller. | ||
* When the number of duplicate advertisements seen by the controller | ||
* reaches this value it will clear the cache and start reporting previously | ||
* seen devices. The larger this number, the longer time between repeated | ||
* device reports. Range 10 - 1000. (default 20) | ||
* | ||
* Can only be used BEFORE calling NimBLEDevice::init. | ||
*/ | ||
NimBLEDevice::setScanDuplicateCacheSize(200); | ||
// If active scanning the result here will have the scan response data. | ||
// If not active scanning then this will be the same as onDiscovered. | ||
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override { | ||
Serial.printf("Device result: %s\n", advertisedDevice->toString().c_str()); | ||
} | ||
|
||
NimBLEDevice::init(""); | ||
void onScanEnd(const NimBLEScanResults& results, int reason) override { | ||
Serial.printf("Scan ended reason = %d; restarting scan\n", reason); | ||
NimBLEDevice::getScan()->start(scanTime, false, true); | ||
} | ||
} scanCallbacks; // create a callback class instance. | ||
|
||
pBLEScan = NimBLEDevice::getScan(); //create new scan | ||
// Set the callback for when devices are discovered, no duplicates. | ||
pBLEScan->setScanCallbacks(new scanCallbacks(), false); | ||
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser. | ||
pBLEScan->setInterval(97); // How often the scan occurs / switches channels; in milliseconds, | ||
pBLEScan->setWindow(37); // How long to scan during the interval; in milliseconds. | ||
pBLEScan->setMaxResults(0); // do not store the scan results, use callback only. | ||
void setup() { | ||
Serial.begin(115200); | ||
NimBLEDevice::init(""); // Initialize the device, you can specify a device name if you want. | ||
NimBLEScan* pBLEScan = NimBLEDevice::getScan(); // Create the scan object. | ||
pBLEScan->setScanCallbacks(&scanCallbacks, false); // Set the callback for when devices are discovered, no duplicates. | ||
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser. | ||
pBLEScan->setMaxResults(0); // Do not store the scan results, use callback only. | ||
pBLEScan->start(scanTime, false, true); // duration, not a continuation of last scan, restart to get all devices again. | ||
Serial.println("Scanning..."); | ||
} | ||
|
||
void loop() { | ||
// If an error occurs that stops the scan, it will be restarted here. | ||
if(pBLEScan->isScanning() == false) { | ||
// Start scan with: duration = 0 seconds(forever), no scan end callback, not a continuation of a previous scan. | ||
pBLEScan->start(0, false); | ||
} | ||
|
||
delay(2000); | ||
} | ||
delay(2000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.