Skip to content

Commit a9f1702

Browse files
authored
Merge pull request #45 from matthias-bs/20251014-feat-ble-scan-abort-callback
Added callback to abort BLE scanning early
2 parents 57a7d69 + a69ef2a commit a9f1702

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/BleSensors/BleSensors.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
// 20250926 Moved decoding from callback to getData() to prevent WDT reset
4444
// Modified getData() to return number of known sensors found
4545
// 20251013 Added abort of scanning by touch sensor
46+
// 20251014 Added optional callback to abort scanning early
47+
// Replaced TouchTriggered by callback function pointer
4648
//
4749
// ToDo:
4850
// -
@@ -53,8 +55,6 @@
5355

5456
#include "BleSensors.h"
5557

56-
extern bool TouchTriggered(void);
57-
5858
namespace BleSensorsCallbacks
5959
{
6060
constexpr size_t JSON_SERIALIZATION_BUFFER_SIZE = 256; //!< Buffer size for JSON serialization
@@ -68,6 +68,7 @@ namespace BleSensorsCallbacks
6868
std::vector<std::string> m_knownBLEAddresses; //!< MAC addresses of known sensors
6969
std::vector<ble_sensors_t> *m_sensorData; //!< Sensor data
7070
NimBLEScan *m_pBLEScan; //!< Pointer to the BLE scan object
71+
bool (*m_stopScanCb)(); //!< Pointer to optional callback function to stop scan early
7172

7273
// Raw JSON payloads collected during scan for later decoding.
7374
std::vector<std::string> m_rawBLEJsons;
@@ -140,9 +141,10 @@ namespace BleSensorsCallbacks
140141
cb_log_v("Known BLE device queued for decoding at index %d", found_index);
141142
}
142143

143-
// Abort scanning by touch sensor
144-
if (TouchTriggered()) {
145-
log_i("Touch interrupt!");
144+
// Abort scanning if requested by callback
145+
if (m_stopScanCb && m_stopScanCb())
146+
{
147+
log_i("Scan aborted.");
146148
m_pBLEScan->stop();
147149
}
148150

@@ -199,6 +201,7 @@ unsigned BleSensors::getData(uint32_t scanTime, bool activeScan)
199201
scanCallbacks.m_knownBLEAddresses = _known_sensors;
200202
scanCallbacks.m_sensorData = &data;
201203
scanCallbacks.m_pBLEScan = _pBLEScan;
204+
scanCallbacks.m_stopScanCb = _stopScanCb;
202205

203206
// Ensure previous scan data cleared
204207
scanCallbacks.m_rawBLEJsons.clear();

src/BleSensors/BleSensors.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
// 20250121 Updated for NimBLE-Arduino v2.x
4141
// 20250808 Added specific logging macros in scan callback to avoid WDT reset
4242
// 20250926 Changed getData() to return number of known sensors found
43+
// 20251014 Added optional callback to abort scanning early
4344
//
4445
// ToDo:
4546
// -
@@ -102,15 +103,18 @@ class BleSensors {
102103
*
103104
* \param known_sensors Vector of BLE MAC addresses of known sensors, e.g. {"11:22:33:44:55:66", "AA:BB:CC:DD:EE:FF"}
104105
*/
105-
BleSensors(std::vector<std::string> known_sensors) {
106+
BleSensors(std::vector<std::string> known_sensors, bool (*stopScanCb)() = nullptr) {
106107
_known_sensors = known_sensors;
107108
data.resize(known_sensors.size());
109+
_stopScanCb = stopScanCb;
108110
};
109111

110112
/*!
111113
* \brief Constructor.
112114
*/
113-
BleSensors(void) {
115+
BleSensors(bool (*stopScanCb)() = nullptr) {
116+
data.resize(0);
117+
_stopScanCb = stopScanCb;
114118
};
115119

116120
/*!
@@ -155,7 +159,8 @@ class BleSensors {
155159
std::vector<ble_sensors_t> data;
156160

157161
protected:
158-
std::vector<std::string> _known_sensors; /// MAC addresses of known sensors
159-
NimBLEScan* _pBLEScan; /// NimBLEScan object
162+
std::vector<std::string> _known_sensors; /// MAC addresses of known sensors
163+
NimBLEScan* _pBLEScan; /// NimBLEScan object
164+
bool (*_stopScanCb)(); /// Pointer to optional callback function to stop scan early
160165
};
161166
#endif

src/LocalInterface.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
// 20250308 Updated NimBLE-Arduino to v2.2.3
3737
// 20250725 Replaced BLE code by src/BleSensors/BleSensors.h/.cpp
3838
// 20251013 Updated SCD4x driver
39+
// 20251014 Changed bleSensors to local variable
40+
// Added TouchTriggered to CTOR as callback to abort BLE scanning
3941
//
4042
// ToDo:
4143
// -
@@ -61,11 +63,6 @@ static const int bleScanMode = 1; //!< BLE scan mode: 0=passive, 1=active
6163
static std::vector<std::string> knownBLEAddresses = KNOWN_BLE_ADDRESSES;
6264
#endif
6365

64-
#ifdef THEENGSDECODER_EN
65-
/// Bluetooth Low Energy sensors
66-
BleSensors bleSensors;
67-
#endif
68-
6966
// Get local sensor data
7067
void LocalInterface::GetLocalData(void)
7168
{
@@ -142,7 +139,7 @@ void LocalInterface::GetLocalData(void)
142139
#endif
143140

144141
#ifdef THEENGSDECODER_EN
145-
bleSensors = BleSensors(KNOWN_BLE_ADDRESSES);
142+
BleSensors bleSensors = BleSensors(KNOWN_BLE_ADDRESSES, &TouchTriggered);
146143
if (bleSensors.data.size() > 0)
147144
{
148145
bleSensors.getData(bleScanTime, bleScanMode);

0 commit comments

Comments
 (0)