[BREAKING] Replace advertised device callbacks with scan callbacks. (#389)

This replaces NimBLEAdvertisedDeviceCallbacks with NimBLEScanCallbacks and adds a onScanEnd callback.

The callback parameter for NimBLEScan::start has been removed and the blocking overload for NimBLEScan::start
has been replaced by an overload of NimBLEScan::getResults with the same parameters.
This commit is contained in:
h2zero 2022-08-27 13:13:27 -06:00 committed by h2zero
parent 562a32eda6
commit 6a2f558ea5
9 changed files with 114 additions and 130 deletions

View file

@ -162,7 +162,7 @@ void app_main(void)
NimBLEDevice::init(""); NimBLEDevice::init("");
NimBLEScan *pScan = NimBLEDevice::getScan(); NimBLEScan *pScan = NimBLEDevice::getScan();
NimBLEScanResults results = pScan->start(10 * 1000); NimBLEScanResults results = pScan->getResults(10 * 1000);
} }
``` ```
<br/> <br/>

View file

@ -11,8 +11,6 @@
extern "C" {void app_main(void);} extern "C" {void app_main(void);}
void scanEndedCB(NimBLEScanResults results);
static NimBLEAdvertisedDevice* advDevice; static NimBLEAdvertisedDevice* advDevice;
static bool doConnect = false; static bool doConnect = false;
@ -31,13 +29,13 @@ class ClientCallbacks : public NimBLEClientCallbacks {
* Min interval: 120 * 1.25ms = 150, Max interval: 120 * 1.25ms = 150, 0 latency, 45 * 10ms = 450ms timeout * Min interval: 120 * 1.25ms = 150, Max interval: 120 * 1.25ms = 150, 0 latency, 45 * 10ms = 450ms timeout
*/ */
pClient->updateConnParams(120,120,0,45); pClient->updateConnParams(120,120,0,45);
}; }
void onDisconnect(NimBLEClient* pClient, int reason) { void onDisconnect(NimBLEClient* pClient, int reason) {
printf("%s Disconnected, reason = %d - Starting scan\n", printf("%s Disconnected, reason = %d - Starting scan\n",
pClient->getPeerAddress().toString().c_str(), reason); pClient->getPeerAddress().toString().c_str(), reason);
NimBLEDevice::getScan()->start(scanTime, scanEndedCB); NimBLEDevice::getScan()->start(scanTime);
}; }
/********************* Security handled here ********************** /********************* Security handled here **********************
****** Note: these are the same return values as defaults ********/ ****** Note: these are the same return values as defaults ********/
@ -45,29 +43,28 @@ class ClientCallbacks : public NimBLEClientCallbacks {
printf("Client Passkey Request\n"); printf("Client Passkey Request\n");
/** return the passkey to send to the server */ /** return the passkey to send to the server */
return 123456; return 123456;
}; }
bool onConfirmPIN(uint32_t pass_key){ bool onConfirmPIN(uint32_t pass_key){
printf("The passkey YES/NO number: %d\n", pass_key); printf("The passkey YES/NO number: %d\n", pass_key);
/** Return false if passkeys don't match. */ /** Return false if passkeys don't match. */
return true; return true;
}; }
/** Pairing process complete, we can check the results in ble_gap_conn_desc */ /** Pairing process complete, we can check the results in connInfo */
void onAuthenticationComplete(ble_gap_conn_desc* desc){ void onAuthenticationComplete(NimBLEConnInfo& connInfo){
if(!desc->sec_state.encrypted) { if(!connInfo.isEncrypted()) {
printf("Encrypt connection failed - disconnecting\n"); printf("Encrypt connection failed - disconnecting\n");
/** Find the client with the connection handle provided in desc */ /** Find the client with the connection handle provided in desc */
NimBLEDevice::getClientByID(desc->conn_handle)->disconnect(); NimBLEDevice::getClientByID(connInfo.getConnHandle())->disconnect();
return; return;
} }
}; }
}; };
/** Define a class to handle the callbacks when advertisments are received */ /** Define a class to handle the callbacks when advertisments are received */
class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks { class scanCallbacks: public NimBLEScanCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) { void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str()); printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD"))) if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
@ -80,7 +77,12 @@ class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
/** Ready to connect now */ /** Ready to connect now */
doConnect = true; doConnect = true;
} }
}; }
/** Callback to process the results of the completed scan or restart it */
void onScanEnd(NimBLEScanResults results) {
printf("Scan Ended\n");
}
}; };
@ -95,11 +97,6 @@ void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData,
printf("%s\n", str.c_str()); printf("%s\n", str.c_str());
} }
/** Callback to process the results of the last scan or restart it */
void scanEndedCB(NimBLEScanResults results){
printf("Scan Ended\n");
}
/** Create a single global instance of the callback class to be used by all clients */ /** Create a single global instance of the callback class to be used by all clients */
static ClientCallbacks clientCB; static ClientCallbacks clientCB;
@ -312,7 +309,7 @@ void connectTask (void * parameter){
printf("Failed to connect, starting scan\n"); printf("Failed to connect, starting scan\n");
} }
NimBLEDevice::getScan()->start(scanTime,scanEndedCB); NimBLEDevice::getScan()->start(scanTime);
} }
vTaskDelay(10/portTICK_PERIOD_MS); vTaskDelay(10/portTICK_PERIOD_MS);
} }
@ -351,7 +348,7 @@ void app_main (void){
NimBLEScan* pScan = NimBLEDevice::getScan(); NimBLEScan* pScan = NimBLEDevice::getScan();
/** create a callback that gets called when advertisers are found */ /** create a callback that gets called when advertisers are found */
pScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks()); pScan->setScanCallbacks (new scanCallbacks());
/** Set scan interval (how often) and window (how long) in milliseconds */ /** Set scan interval (how often) and window (how long) in milliseconds */
pScan->setInterval(400); pScan->setInterval(400);
@ -364,7 +361,7 @@ void app_main (void){
/** Start scanning for advertisers for the scan time specified (in seconds) 0 = forever /** Start scanning for advertisers for the scan time specified (in seconds) 0 = forever
* Optional callback for when scanning stops. * Optional callback for when scanning stops.
*/ */
pScan->start(scanTime, scanEndedCB); pScan->start(scanTime);
printf("Scanning for peripherals\n"); printf("Scanning for peripherals\n");

View file

@ -11,8 +11,6 @@
extern "C" void app_main(void); extern "C" void app_main(void);
void scanEndedCB(NimBLEScanResults results);
#define SERVICE_UUID "ABCD" #define SERVICE_UUID "ABCD"
#define CHARACTERISTIC_UUID "1234" #define CHARACTERISTIC_UUID "1234"
@ -32,14 +30,13 @@ class ClientCallbacks : public NimBLEClientCallbacks {
void onDisconnect(NimBLEClient* pClient, int reason) { void onDisconnect(NimBLEClient* pClient, int reason) {
printf("%s Disconnected, reason = %d - Starting scan\n", printf("%s Disconnected, reason = %d - Starting scan\n",
pClient->getPeerAddress().toString().c_str(), reason); pClient->getPeerAddress().toString().c_str(), reason);
NimBLEDevice::getScan()->start(scanTime, scanEndedCB); NimBLEDevice::getScan()->start(scanTime);
}; };
}; };
/* Define a class to handle the callbacks when advertisements are received */ /* Define a class to handle the callbacks when advertisements are received */
class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks { class scanCallbacks: public NimBLEScanCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) { void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str()); printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
if(advertisedDevice->isAdvertisingService(NimBLEUUID("ABCD"))) if(advertisedDevice->isAdvertisingService(NimBLEUUID("ABCD")))
@ -52,17 +49,13 @@ class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
/* stop scan before connecting */ /* stop scan before connecting */
NimBLEDevice::getScan()->stop(); NimBLEDevice::getScan()->stop();
} }
};
};
/* Callback to process the results of the last scan or restart it */
void scanEndedCB(NimBLEScanResults results){
printf("Scan Ended\n");
if (!doConnect) { /* Don't start the scan while connecting */
NimBLEDevice::getScan()->start(scanTime, scanEndedCB);
} }
}
/** Callback to process the results of the completed scan or restart it */
void onScanEnd(NimBLEScanResults results) {
printf("Scan Ended\n");
}
};
/* Handles the provisioning of clients and connects / interfaces with the server */ /* Handles the provisioning of clients and connects / interfaces with the server */
@ -132,7 +125,7 @@ void connectTask (void * parameter){
} }
doConnect = false; doConnect = false;
NimBLEDevice::getScan()->start(scanTime, scanEndedCB); NimBLEDevice::getScan()->start(scanTime);
} }
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(10));
} }
@ -150,7 +143,7 @@ void app_main (void) {
NimBLEScan* pScan = NimBLEDevice::getScan(); NimBLEScan* pScan = NimBLEDevice::getScan();
/* create a callback that gets called when advertisers are found */ /* create a callback that gets called when advertisers are found */
pScan->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallbacks()); pScan->setScanCallbacks(new scanCallbacks());
/* Set scan interval (how often) and window (how long) in milliseconds */ /* Set scan interval (how often) and window (how long) in milliseconds */
pScan->setInterval(97); pScan->setInterval(97);
@ -164,7 +157,7 @@ void app_main (void) {
/* Start scanning for advertisers for the scan time specified (in seconds) 0 = forever /* Start scanning for advertisers for the scan time specified (in seconds) 0 = forever
* Optional callback for when scanning stops. * Optional callback for when scanning stops.
*/ */
pScan->start(scanTime, scanEndedCB); pScan->start(scanTime);
printf("Scanning for peripherals\n"); printf("Scanning for peripherals\n");
} }

View file

@ -191,7 +191,7 @@ void app_main(void) {
// have detected a new device. Specify that we want active scanning and start the // have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds. // scan to run for 5 seconds.
BLEScan* pBLEScan = BLEDevice::getScan(); BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349); pBLEScan->setInterval(1349);
pBLEScan->setWindow(449); pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true); pBLEScan->setActiveScan(true);

View file

@ -29,7 +29,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void scanTask (void * parameter){ void scanTask (void * parameter){
for(;;) { for(;;) {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false); BLEScanResults foundDevices = pBLEScan->getResults(scanTime, false);
printf("Devices found: %d\n", foundDevices.getCount()); printf("Devices found: %d\n", foundDevices.getCount());
printf("Scan done!\n"); printf("Scan done!\n");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
@ -44,7 +44,7 @@ void app_main(void) {
BLEDevice::init(""); BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100); pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value pBLEScan->setWindow(99); // less or equal setInterval value

View file

@ -175,24 +175,5 @@ private:
std::vector<uint8_t> m_payload; std::vector<uint8_t> m_payload;
}; };
/**
* @brief A callback handler for callbacks associated device scanning.
*
* When we are performing a scan as a %BLE client, we may wish to know when a new device that is advertising
* has been found. This class can be sub-classed and registered such that when a scan is performed and
* a new advertised device has been found, we will be called back to be notified.
*/
class NimBLEAdvertisedDeviceCallbacks {
public:
virtual ~NimBLEAdvertisedDeviceCallbacks() {}
/**
* @brief Called when a new scan result is detected.
*
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
* device that was found. During any individual scan, a device will only be detected one time.
*/
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
};
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */ #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
#endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */ #endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */

View file

@ -62,7 +62,7 @@
#define BLEAddress NimBLEAddress #define BLEAddress NimBLEAddress
#define BLEUtils NimBLEUtils #define BLEUtils NimBLEUtils
#define BLEClientCallbacks NimBLEClientCallbacks #define BLEClientCallbacks NimBLEClientCallbacks
#define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks #define BLEAdvertisedDeviceCallbacks NimBLEScanCallbacks
#define BLEScanResults NimBLEScanResults #define BLEScanResults NimBLEScanResults
#define BLEServer NimBLEServer #define BLEServer NimBLEServer
#define BLEService NimBLEService #define BLEService NimBLEService

View file

@ -35,7 +35,7 @@ NimBLEScan::NimBLEScan() {
m_scan_params.window = 0; // The duration of the LE scan. LE_Scan_Window shall be less than or equal to LE_Scan_Interval (units=0.625 msec) m_scan_params.window = 0; // The duration of the LE scan. LE_Scan_Window shall be less than or equal to LE_Scan_Interval (units=0.625 msec)
m_scan_params.limited = 0; // If set, only discover devices in limited discoverable mode. m_scan_params.limited = 0; // If set, only discover devices in limited discoverable mode.
m_scan_params.filter_duplicates = 1; // If set, the controller ignores all but the first advertisement from each device. m_scan_params.filter_duplicates = 1; // If set, the controller ignores all but the first advertisement from each device.
m_pAdvertisedDeviceCallbacks = nullptr; m_pScanCallbacks = nullptr;
m_ignoreResults = false; m_ignoreResults = false;
m_pTaskData = nullptr; m_pTaskData = nullptr;
m_duration = BLE_HS_FOREVER; // make sure this is non-zero in the event of a host reset m_duration = BLE_HS_FOREVER; // make sure this is non-zero in the event of a host reset
@ -55,7 +55,8 @@ NimBLEScan::~NimBLEScan() {
* @param [in] event The event type for this event. * @param [in] event The event type for this event.
* @param [in] param Parameter data for this event. * @param [in] param Parameter data for this event.
*/ */
/*STATIC*/int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) { /*STATIC*/
int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
(void)arg; (void)arg;
NimBLEScan* pScan = NimBLEDevice::getScan(); NimBLEScan* pScan = NimBLEDevice::getScan();
@ -133,7 +134,7 @@ NimBLEScan::~NimBLEScan() {
advertisedDevice->setPayload(disc.data, disc.length_data, (isLegacyAdv && advertisedDevice->setPayload(disc.data, disc.length_data, (isLegacyAdv &&
event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP)); event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP));
if (pScan->m_pAdvertisedDeviceCallbacks) { if (pScan->m_pScanCallbacks) {
if (pScan->m_scan_params.filter_duplicates && advertisedDevice->m_callbackSent) { if (pScan->m_scan_params.filter_duplicates && advertisedDevice->m_callbackSent) {
return 0; return 0;
} }
@ -145,12 +146,12 @@ NimBLEScan::~NimBLEScan() {
advertisedDevice->getAdvType() != BLE_HCI_ADV_TYPE_ADV_SCAN_IND)) advertisedDevice->getAdvType() != BLE_HCI_ADV_TYPE_ADV_SCAN_IND))
{ {
advertisedDevice->m_callbackSent = true; advertisedDevice->m_callbackSent = true;
pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); pScan->m_pScanCallbacks->onResult(advertisedDevice);
// Otherwise, wait for the scan response so we can report the complete data. // Otherwise, wait for the scan response so we can report the complete data.
} else if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) { } else if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
advertisedDevice->m_callbackSent = true; advertisedDevice->m_callbackSent = true;
pScan->m_pAdvertisedDeviceCallbacks->onResult(advertisedDevice); pScan->m_pScanCallbacks->onResult(advertisedDevice);
} }
// If not storing results and we have invoked the callback, delete the device. // If not storing results and we have invoked the callback, delete the device.
if(pScan->m_maxResults == 0 && advertisedDevice->m_callbackSent) { if(pScan->m_maxResults == 0 && advertisedDevice->m_callbackSent) {
@ -166,10 +167,10 @@ NimBLEScan::~NimBLEScan() {
// If a device advertised with scan response available and it was not received // If a device advertised with scan response available and it was not received
// the callback would not have been invoked, so do it here. // the callback would not have been invoked, so do it here.
if(pScan->m_pAdvertisedDeviceCallbacks) { if(pScan->m_pScanCallbacks) {
for(auto &it : pScan->m_scanResults.m_advertisedDevicesVector) { for(auto &it : pScan->m_scanResults.m_advertisedDevicesVector) {
if(!it->m_callbackSent) { if(!it->m_callbackSent) {
pScan->m_pAdvertisedDeviceCallbacks->onResult(it); pScan->m_pScanCallbacks->onResult(it);
} }
} }
} }
@ -178,8 +179,8 @@ NimBLEScan::~NimBLEScan() {
pScan->clearResults(); pScan->clearResults();
} }
if (pScan->m_scanCompleteCB != nullptr) { if (pScan->m_pScanCallbacks != nullptr) {
pScan->m_scanCompleteCB(pScan->m_scanResults); pScan->m_pScanCallbacks->onScanEnd(pScan->m_scanResults);
} }
if(pScan->m_pTaskData != nullptr) { if(pScan->m_pTaskData != nullptr) {
@ -263,14 +264,13 @@ void NimBLEScan::setMaxResults(uint8_t maxResults) {
/** /**
* @brief Set the call backs to be invoked. * @brief Set the call backs to be invoked.
* @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked. * @param [in] pScanCallbacks Call backs to be invoked.
* @param [in] wantDuplicates True if we wish to be called back with duplicates. Default is false. * @param [in] wantDuplicates True if we wish to be called back with duplicates. Default is false.
*/ */
void NimBLEScan::setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, void NimBLEScan::setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool wantDuplicates) {
bool wantDuplicates) {
setDuplicateFilter(!wantDuplicates); setDuplicateFilter(!wantDuplicates);
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks; m_pScanCallbacks = pScanCallbacks;
} // setAdvertisedDeviceCallbacks } // setScanCallbacks
/** /**
@ -303,15 +303,12 @@ bool NimBLEScan::isScanning() {
/** /**
* @brief Start scanning. * @brief Start scanning.
* @param [in] duration The duration in milliseconds for which to scan. * @param [in] duration The duration in milliseconds for which to scan.
* @param [in] scanCompleteCB A function to be called when scanning has completed.
* @param [in] is_continue Set to true to save previous scan results, false to clear them. * @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @return True if scan started or false if there was an error. * @return True if scan started or false if there was an error.
*/ */
bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue) { bool NimBLEScan::start(uint32_t duration, bool is_continue) {
NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration); NIMBLE_LOGD(LOG_TAG, ">> start: duration=%" PRIu32, duration);
// Save the callback to be invoked when the scan completes.
m_scanCompleteCB = scanCompleteCB;
// Save the duration in the case that the host is reset so we can reuse it. // Save the duration in the case that the host is reset so we can reuse it.
m_duration = duration; m_duration = duration;
@ -386,34 +383,6 @@ bool NimBLEScan::start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResul
} // start } // start
/**
* @brief Start scanning and block until scanning has been completed.
* @param [in] duration The duration in seconds for which to scan.
* @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @return The NimBLEScanResults.
*/
NimBLEScanResults NimBLEScan::start(uint32_t duration, bool is_continue) {
if(duration == 0) {
NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever");
}
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {nullptr, cur_task, 0, nullptr};
m_pTaskData = &taskData;
if(start(duration, nullptr, is_continue)) {
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
m_pTaskData = nullptr;
return m_scanResults;
} // start
/** /**
* @brief Stop an in progress scan. * @brief Stop an in progress scan.
* @return True if successful. * @return True if successful.
@ -431,8 +400,8 @@ bool NimBLEScan::stop() {
clearResults(); clearResults();
} }
if (rc != BLE_HS_EALREADY && m_scanCompleteCB != nullptr) { if (rc != BLE_HS_EALREADY && m_pScanCallbacks != nullptr) {
m_scanCompleteCB(m_scanResults); m_pScanCallbacks->onScanEnd(m_scanResults);
} }
if(m_pTaskData != nullptr) { if(m_pTaskData != nullptr) {
@ -487,11 +456,40 @@ void NimBLEScan::onHostReset() {
void NimBLEScan::onHostSync() { void NimBLEScan::onHostSync() {
m_ignoreResults = false; m_ignoreResults = false;
if(m_duration == 0 && m_pAdvertisedDeviceCallbacks != nullptr) { if(m_duration == 0 && m_pScanCallbacks != nullptr) {
start(m_duration, m_scanCompleteCB); start(0, false);
} }
} }
/**
* @brief Start scanning and block until scanning has been completed.
* @param [in] duration The duration in seconds for which to scan.
* @param [in] is_continue Set to true to save previous scan results, false to clear them.
* @return The scan results.
*/
NimBLEScanResults NimBLEScan::getResults(uint32_t duration, bool is_continue) {
if(duration == 0) {
NIMBLE_LOGW(LOG_TAG, "Blocking scan called with duration = forever");
}
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
ble_task_data_t taskData = {nullptr, cur_task, 0, nullptr};
m_pTaskData = &taskData;
if(start(duration, is_continue)) {
#ifdef ulTaskNotifyValueClear
// Clear the task notification value to ensure we block
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
m_pTaskData = nullptr;
return m_scanResults;
} // getResults
/** /**
* @brief Get the results of the scan. * @brief Get the results of the scan.
* @return NimBLEScanResults object. * @return NimBLEScanResults object.

View file

@ -31,7 +31,7 @@
class NimBLEDevice; class NimBLEDevice;
class NimBLEScan; class NimBLEScan;
class NimBLEAdvertisedDevice; class NimBLEAdvertisedDevice;
class NimBLEAdvertisedDeviceCallbacks; class NimBLEScanCallbacks;
class NimBLEAddress; class NimBLEAddress;
/** /**
@ -62,10 +62,9 @@ private:
*/ */
class NimBLEScan { class NimBLEScan {
public: public:
bool start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue = false); bool start(uint32_t duration, bool is_continue = false);
NimBLEScanResults start(uint32_t duration, bool is_continue = false);
bool isScanning(); bool isScanning();
void setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates = false); void setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool wantDuplicates = false);
void setActiveScan(bool active); void setActiveScan(bool active);
void setInterval(uint16_t intervalMSecs); void setInterval(uint16_t intervalMSecs);
void setWindow(uint16_t windowMSecs); void setWindow(uint16_t windowMSecs);
@ -76,6 +75,7 @@ public:
bool stop(); bool stop();
void clearResults(); void clearResults();
NimBLEScanResults getResults(); NimBLEScanResults getResults();
NimBLEScanResults getResults(uint32_t duration, bool is_continue = false);
void setMaxResults(uint8_t maxResults); void setMaxResults(uint8_t maxResults);
void erase(const NimBLEAddress &address); void erase(const NimBLEAddress &address);
@ -89,8 +89,7 @@ private:
void onHostReset(); void onHostReset();
void onHostSync(); void onHostSync();
NimBLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr; NimBLEScanCallbacks* m_pScanCallbacks;
void (*m_scanCompleteCB)(NimBLEScanResults scanResults);
ble_gap_disc_params m_scan_params; ble_gap_disc_params m_scan_params;
bool m_ignoreResults; bool m_ignoreResults;
NimBLEScanResults m_scanResults; NimBLEScanResults m_scanResults;
@ -99,5 +98,21 @@ private:
uint8_t m_maxResults; uint8_t m_maxResults;
}; };
/**
* @brief A callback handler for callbacks associated device scanning.
*/
class NimBLEScanCallbacks {
public:
virtual ~NimBLEScanCallbacks() {}
/**
* @brief Called when a new scan result is detected.
*
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
* device that was found. During any individual scan, a device will only be detected one time.
*/
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) {};
virtual void onScanEnd(NimBLEScanResults scanResults) {};
};
#endif /* CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER */ #endif /* CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER */
#endif /* COMPONENTS_NIMBLE_SCAN_H_ */ #endif /* COMPONENTS_NIMBLE_SCAN_H_ */