forked from h2zero/esp-nimble-cpp
[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`
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
|
||||
extern "C" {void app_main(void);}
|
||||
|
||||
static NimBLEAdvertisedDevice* advDevice;
|
||||
static const NimBLEAdvertisedDevice* advDevice;
|
||||
|
||||
static bool doConnect = false;
|
||||
static uint32_t scanTime = 0; /** scan time in milliseconds, 0 = scan forever */
|
||||
@@ -67,7 +67,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
|
||||
|
||||
/** Define a class to handle the callbacks when advertisments are received */
|
||||
class scanCallbacks: public NimBLEScanCallbacks {
|
||||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
|
||||
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
|
||||
{
|
||||
@@ -82,8 +82,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
|
||||
}
|
||||
|
||||
/** Callback to process the results of the completed scan or restart it */
|
||||
void onScanEnd(NimBLEScanResults results) {
|
||||
printf("Scan Ended\n");
|
||||
void onScanEnd(const NimBLEScanResults& results, int reason) {
|
||||
printf("Scan Ended, reason: %d, device count: %d\n", reason, results.getCount());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(Continuous_scan)
|
||||
@@ -0,0 +1,4 @@
|
||||
set(COMPONENT_SRCS "main.cpp")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
|
||||
register_component()
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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 March 24 2020
|
||||
* Author: H2zero
|
||||
*
|
||||
*/
|
||||
|
||||
#include "NimBLEDevice.h"
|
||||
|
||||
static constexpr uint32_t scanTime = 30 * 1000; // 30 seconds scan time.
|
||||
|
||||
class scanCallbacks : public NimBLEScanCallbacks {
|
||||
// Initial discovery, advertisement data only.
|
||||
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) override {
|
||||
printf("Discovered Device: %s\n", advertisedDevice->toString().c_str());
|
||||
}
|
||||
|
||||
// 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 {
|
||||
printf("Device result: %s\n", advertisedDevice->toString().c_str());
|
||||
}
|
||||
|
||||
void onScanEnd(const NimBLEScanResults& results, int reason) override {
|
||||
printf("Scan ended reason = %d; restarting scan\n", reason);
|
||||
NimBLEDevice::getScan()->start(scanTime, false, true);
|
||||
}
|
||||
} scanCallbacks; // create a callback class instance.
|
||||
|
||||
extern "C" void app_main() {
|
||||
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.
|
||||
printf("Scanning...\n");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
# Override some defaults so BT stack is enabled
|
||||
# in this example
|
||||
|
||||
#
|
||||
# BT config
|
||||
#
|
||||
CONFIG_BT_ENABLED=y
|
||||
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
|
||||
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
@@ -25,7 +25,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
|
||||
} clientCB;
|
||||
|
||||
class scanCallbacks : public NimBLEScanCallbacks {
|
||||
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
|
||||
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
|
||||
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
|
||||
printf("Found Our Device\n");
|
||||
|
||||
@@ -24,7 +24,7 @@ static bool doConnect = false;
|
||||
static bool connected = false;
|
||||
static bool doScan = false;
|
||||
static BLERemoteCharacteristic* pRemoteCharacteristic;
|
||||
static BLEAdvertisedDevice* myDevice;
|
||||
static const BLEAdvertisedDevice* myDevice;
|
||||
|
||||
static void notifyCallback(
|
||||
BLERemoteCharacteristic* pBLERemoteCharacteristic,
|
||||
@@ -137,7 +137,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||
|
||||
/*** Only a reference to the advertised device is passed now
|
||||
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
|
||||
void onResult(BLEAdvertisedDevice* advertisedDevice) {
|
||||
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
|
||||
printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str());
|
||||
|
||||
// We have found a device, let us now see if it contains the service we are looking for.
|
||||
|
||||
@@ -21,7 +21,7 @@ int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
|
||||
BLEScan* pBLEScan;
|
||||
|
||||
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||
void onResult(BLEAdvertisedDevice* advertisedDevice) {
|
||||
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
|
||||
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
|
||||
}
|
||||
};
|
||||
@@ -35,7 +35,7 @@ void scanTask (void * parameter){
|
||||
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
|
||||
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
|
||||
}
|
||||
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user