mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-23 05:30:54 +01:00
2151386057
* 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`
52 lines
1.7 KiB
C++
52 lines
1.7 KiB
C++
/*
|
|
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
|
|
Ported to Arduino ESP32 by Evandro Copercini
|
|
Refactored back to IDF by H2zero
|
|
*/
|
|
|
|
/** NimBLE differences highlighted in comment blocks **/
|
|
|
|
/*******original********
|
|
#include <BLEDevice.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLEScan.h>
|
|
#include <BLEAdvertisedDevice.h>
|
|
***********************/
|
|
|
|
#include <NimBLEDevice.h>
|
|
|
|
extern "C"{void app_main(void);}
|
|
|
|
int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
|
|
BLEScan* pBLEScan;
|
|
|
|
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
|
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
|
|
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
|
|
}
|
|
};
|
|
|
|
void scanTask (void * parameter){
|
|
for(;;) {
|
|
// put your main code here, to run repeatedly:
|
|
BLEScanResults foundDevices = pBLEScan->getResults(scanTime, false);
|
|
printf("Devices found: %d\n", foundDevices.getCount());
|
|
printf("Scan done!\n");
|
|
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
|
|
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
void app_main(void) {
|
|
printf("Scanning...\n");
|
|
|
|
BLEDevice::init("");
|
|
pBLEScan = BLEDevice::getScan(); //create new scan
|
|
pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks());
|
|
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
|
pBLEScan->setInterval(100);
|
|
pBLEScan->setWindow(99); // less or equal setInterval value
|
|
xTaskCreate(scanTask, "scanTask", 5000, NULL, 1, NULL);
|
|
}
|