mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-24 14:10:55 +01:00
46e7bb9302
This adds a callback that is called when a device is initially discovered. It is always called wether scanning in passive or active mode. This allows for active scanning to receive device detection information in the case where the scan response is not sent/received. This callback is optional, the application may decide to implement it or not.
48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
/*
|
|
* NimBLE Scan active/passive switching demo
|
|
*
|
|
* Demonstrates the use of the scan callbacks while alternating between passive and active scanning.
|
|
*/
|
|
|
|
#include "NimBLEDevice.h"
|
|
int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
|
|
BLEScan* pBLEScan;
|
|
|
|
bool active = false;
|
|
|
|
class scanCallbacks: public NimBLEScanCallbacks {
|
|
|
|
void onDiscovered(NimBLEAdvertisedDevice* advertisedDevice) {
|
|
Serial.printf("Discovered Advertised Device: %s \n", advertisedDevice->toString().c_str());
|
|
}
|
|
|
|
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
|
Serial.printf("Advertised Device Result: %s \n", advertisedDevice->toString().c_str());
|
|
}
|
|
|
|
void onScanEnd(NimBLEScanResults results){
|
|
Serial.println("Scan Ended");
|
|
active = !active;
|
|
pBLEScan->setActiveScan(active);
|
|
Serial.printf("scan start, active = %u\n", active);
|
|
pBLEScan->start(scanTime);
|
|
}
|
|
};
|
|
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("Scanning...");
|
|
|
|
NimBLEDevice::init("");
|
|
pBLEScan = NimBLEDevice::getScan();
|
|
pBLEScan->setScanCallbacks(new scanCallbacks());
|
|
pBLEScan->setActiveScan(active);
|
|
pBLEScan->setInterval(100);
|
|
pBLEScan->setWindow(99);
|
|
pBLEScan->start(scanTime);
|
|
}
|
|
|
|
void loop() {
|
|
}
|