8000 Implementation of async BLEScan #496 · olehs/esp32-snippets@af664f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit af664f0

Browse files
committed
Implementation of async BLEScan nkolban#496
1 parent 2ae46aa commit af664f0

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

cpp_utils/BLEScan.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void BLEScan::handleGAPEvent(
7474
// asked to stop.
7575
case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
7676
m_stopped = true;
77+
if (m_scanCompleteCB != nullptr) {
78+
m_scanCompleteCB(m_scanResults);
79+
}
7780
m_semaphoreScanEnd.give();
7881
break;
7982
} // ESP_GAP_SEARCH_INQ_CMPL_EVT
@@ -186,10 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
186189
/**
187190
* @brief Start scanning.
188191
* @param [in] duration The duration in seconds for which to scan.
189-
* @return N/A.
192+
* @param [in] scanCompleteCB A function to be called when scanning has completed. This can
193+
* be supplied as nullptr (the default) in which case the call to start will block until scanning has
194+
* been completed.
195+
* @return The BLEScanResults. Only applicable if we are waiting for results.
190196
*/
191-
BLEScanResults BLEScan::start(uint32_t duration) {
197+
BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
192198
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
199+
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
193200

194201
m_semaphoreScanEnd.take(std::string("start"));
195202

@@ -213,7 +220,9 @@ BLEScanResults BLEScan::start(uint32_t duration) {
213220

214221
m_stopped = false;
215222

216-
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
223+
if (m_scanCompleteCB == nullptr) {
224+
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
225+
}
217226

218227
ESP_LOGD(LOG_TAG, "<< start()");
219228
return m_scanResults;

cpp_utils/BLEScan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class BLEScan {
5353
bool wantDuplicates = false);
5454
void setInterval(uint16_t intervalMSecs);
5555
void setWindow(uint16_t windowMSecs);
56-
BLEScanResults start(uint32_t duration);
56+
BLEScanResults start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults) = nullptr);
5757
void stop();
5858

5959
private:
@@ -71,6 +71,7 @@ class BLEScan {
7171
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
7272
BLEScanResults m_scanResults;
7373
bool m_wantDuplicates;
74+
void (*m_scanCompleteCB)(BLEScanResults scanResults);
7475
}; // BLEScan
7576

7677
#endif /* CONFIG_BT_ENABLED */
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Perform an async scanning for BLE advertised servers.
3+
*/
4+
#include "BLEUtils.h"
5+
#include "BLEScan.h"
6+
#include <string>
7+
8+
#include "BLEDevice.h"
9+
#include "BLEAdvertisedDevice.h"
10+
#include "sdkconfig.h"
11+
12+
/**
13+
* Callback for each detected advertised device.
14+
*/
15+
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
16+
void onResult(BLEAdvertisedDevice advertisedDevice) {
17+
printf("Advertised Device: %s\n", advertisedDevice.toString().c_str());
18+
}
19+
};
20+
21+
22+
/**
23+
* Callback invoked when scanning has completed.
24+
*/
25+
static void scanCompleteCB(BLEScanResults scanResults) {
26+
printf("Scan complete!\n");
27+
printf("We found %d devices\n", scanResults.getCount());
28+
scanResults.dump();
29+
} // scanCompleteCB
30+
31+
/**
32+
* Run the sample.
33+
*/
34+
static void run() {
35+
printf("Async Scanning sample starting\n");
36+
BLEDevice::init("");
37+
38+
BLEScan* pBLEScan = BLEDevice::getScan();
39+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), false);
40+
pBLEScan->setActiveScan(true);
41+
printf("About to start scanning for 10 seconds\n");
42 67F4 +
pBLEScan->start(10, scanCompleteCB);
43+
printf("Now scanning in the background ... scanCompleteCB() will be called when done.\n");
44+
45+
//
46+
// Now going into a loop logging that we are still alive.
47+
//
48+
while(1) {
49+
printf("Tick! - still alive\n");
50+
FreeRTOS::sleep(1000);
51+
}
52+
printf("Scanning sample ended\n");
53+
}
54+
55+
void SampleAsyncScan(void)
56+
{
57+
run();
58+
} // app_main

cpp_utils/tests/BLETests/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
// The list of sample entry points.
1010
void Sample_MLE_15(void);
1111
void Sample1(void);
12+
void SampleAsyncScan(void);
1213
void SampleClient(void);
1314
void SampleClient_Notify(void);
1415
void SampleClientAndServer(void);
@@ -27,6 +28,7 @@ void SampleWrite(void);
2728
void app_main(void) {
2829
//Sample_MLE_15();
2930
//Sample1();
31+
//SampleAsyncScan();
3032
//SampleClient();
3133
//SampleClient_Notify();
3234
//SampleClientAndServer();

tools/bootloaderExamine/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/a.out
2+
/app-template.bin

0 commit comments

Comments
 (0)
0