2024-11-28 17:17:42 +01:00
|
|
|
/**
|
|
|
|
* NimBLE Extended Scanner Demo:
|
|
|
|
*
|
|
|
|
* Demonstrates the Bluetooth 5.x scanning capabilities of the NimBLE library.
|
|
|
|
*
|
|
|
|
* Created: on November 28, 2024
|
|
|
|
* Author: H2zero
|
2024-12-09 20:58:14 +01:00
|
|
|
*/
|
2024-11-28 17:17:42 +01:00
|
|
|
|
|
|
|
#include <NimBLEDevice.h>
|
|
|
|
|
2024-12-14 22:37:09 +01:00
|
|
|
static uint32_t scanTimeMs = 10 * 1000; // In milliseconds, 0 = scan forever
|
|
|
|
static NimBLEScan::Phy scanPhy = NimBLEScan::Phy::SCAN_ALL;
|
2024-11-28 17:17:42 +01:00
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Define a class to handle the callbacks when advertisements are received */
|
|
|
|
class ScanCallbacks : public NimBLEScanCallbacks {
|
2024-11-28 17:17:42 +01:00
|
|
|
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
|
2024-12-09 20:58:14 +01:00
|
|
|
printf("Advertised Device found: %s\n PHY1: %d\n PHY2: %d\n",
|
|
|
|
advertisedDevice->toString().c_str(),
|
|
|
|
advertisedDevice->getPrimaryPhy(),
|
|
|
|
advertisedDevice->getSecondaryPhy());
|
2024-11-28 17:17:42 +01:00
|
|
|
}
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Callback to process the results of the completed scan or restart it */
|
2024-11-28 17:17:42 +01:00
|
|
|
void onScanEnd(const NimBLEScanResults& scanResults, int reason) {
|
|
|
|
printf("Scan Ended, reason: %d; found %d devices\n", reason, scanResults.getCount());
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Try Different PHY's */
|
2024-11-28 17:17:42 +01:00
|
|
|
switch (scanPhy) {
|
|
|
|
case NimBLEScan::Phy::SCAN_ALL:
|
|
|
|
printf("Scanning only 1M PHY\n");
|
|
|
|
scanPhy = NimBLEScan::Phy::SCAN_1M;
|
|
|
|
break;
|
|
|
|
case NimBLEScan::Phy::SCAN_1M:
|
|
|
|
printf("Scanning only CODED PHY\n");
|
|
|
|
scanPhy = NimBLEScan::Phy::SCAN_CODED;
|
|
|
|
break;
|
|
|
|
case NimBLEScan::Phy::SCAN_CODED:
|
|
|
|
printf("Scanning all PHY's\n");
|
|
|
|
scanPhy = NimBLEScan::Phy::SCAN_ALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NimBLEScan* pScan = NimBLEDevice::getScan();
|
|
|
|
pScan->setPhy(scanPhy);
|
2024-12-14 22:37:09 +01:00
|
|
|
pScan->start(scanTimeMs);
|
2024-11-28 17:17:42 +01:00
|
|
|
}
|
2024-12-09 20:58:14 +01:00
|
|
|
} scanCallbacks;
|
2024-11-28 17:17:42 +01:00
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
extern "C" void app_main(void) {
|
2024-11-28 17:17:42 +01:00
|
|
|
printf("Starting Extended Scanner\n");
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Initialize NimBLE and set the device name */
|
|
|
|
NimBLEDevice::init("NimBLE Extended Scanner");
|
2024-11-28 17:17:42 +01:00
|
|
|
NimBLEScan* pScan = NimBLEDevice::getScan();
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Set the callbacks that the scanner will call on events. */
|
|
|
|
pScan->setScanCallbacks(&scanCallbacks);
|
2024-11-28 17:17:42 +01:00
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Use active scanning to obtain scan response data from advertisers */
|
2024-11-28 17:17:42 +01:00
|
|
|
pScan->setActiveScan(true);
|
|
|
|
|
2024-12-09 20:58:14 +01:00
|
|
|
/** Set the initial PHY's to scan on, default is SCAN_ALL */
|
2024-11-28 17:17:42 +01:00
|
|
|
pScan->setPhy(scanPhy);
|
|
|
|
|
2024-12-14 22:37:09 +01:00
|
|
|
/** Start scanning for scanTimeMs */
|
|
|
|
pScan->start(scanTimeMs);
|
2024-11-28 17:17:42 +01:00
|
|
|
printf("Scanning for peripherals\n");
|
|
|
|
}
|