@@ -48,22 +48,21 @@ void BLEScan::handleGAPEvent(
48
48
esp_gap_ble_cb_event_t event,
49
49
esp_ble_gap_cb_param_t * param) {
50
50
51
- switch (event) {
52
-
53
- // ESP_GAP_BLE_SCAN_RESULT_EVT
54
- // ---------------------------
55
- // scan_rst:
56
- // esp_gap_search_evt_t search_evt
57
- // esp_bd_addr_t bda
58
- // esp_bt_dev_type_t dev_type
59
- // esp_ble_addr_type_t ble_addr_type
60
- // esp_ble_evt_type_t ble_evt_type
61
- // int rssi
62
- // uint8_t ble_adv[ESP_BLE_ADV_DATA_LEN_MAX]
63
- // int flag
64
- // int num_resps
65
- // uint8_t adv_data_len
66
- // uint8_t scan_rsp_len
51
+ switch (event) {
52
+
53
+ // ---------------------------
54
+ // scan_rst:
55
+ // esp_gap_search_evt_t search_evt
56
+ // esp_bd_addr_t bda
57
+ // esp_bt_dev_type_t dev_type
58
+ // esp_ble_addr_type_t ble_addr_type
59
+ // esp_ble_evt_type_t ble_evt_type
60
+ // int rssi
61
+ // uint8_t ble_adv[ESP_BLE_ADV_DATA_LEN_MAX]
62
+ // int flag
63
+ // int num_resps
64
+ // uint8_t adv_data_len
65
+ // uint8_t scan_rsp_len
67
66
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
68
67
69
68
switch (param->scan_rst .search_evt ) {
@@ -73,11 +72,12 @@ void BLEScan::handleGAPEvent(
73
72
// Event that indicates that the duration allowed for the search has completed or that we have been
74
73
// asked to stop.
75
74
case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
75
+ ESP_LOGW (LOG_TAG, " ESP_GAP_SEARCH_INQ_CMPL_EVT" );
76
76
m_stopped = true ;
77
+ m_semaphoreScanEnd.give ();
77
78
if (m_scanCompleteCB != nullptr ) {
78
79
m_scanCompleteCB (m_scanResults);
79
80
}
80
- m_semaphoreScanEnd.give ();
81
81
break ;
82
82
} // ESP_GAP_SEARCH_INQ_CMPL_EVT
83
83
@@ -90,52 +90,58 @@ void BLEScan::handleGAPEvent(
90
90
break ;
91
91
}
92
92
93
- // Examine our list of previously scanned addresses and, if we found this one already,
94
- // ignore it.
93
+ // Examine our list of previously scanned addresses and, if we found this one already,
94
+ // ignore it.
95
95
BLEAddress advertisedAddress (param->scan_rst .bda );
96
96
bool found = false ;
97
97
98
- for (uint32_t i = 0 ; i < m_scanResults.getCount (); i++) {
99
- if (m_scanResults.getDevice (i).getAddress ().equals (advertisedAddress)) {
100
- found = true ;
101
- break ;
102
- }
98
+ if (m_scanResults.m_vectorAdvertisedDevices .count (advertisedAddress.toString ()) != 0 ) {
99
+ found = true ;
103
100
}
101
+
104
102
if (found && !m_wantDuplicates) { // If we found a previous entry AND we don't want duplicates, then we are done.
105
103
ESP_LOGD (LOG_TAG, " Ignoring %s, already seen it." , advertisedAddress.toString ().c_str ());
104
+ vTaskDelay (1 ); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we will blocked here
106
105
break ;
107
106
}
108
107
109
108
// We now construct a model of the advertised device that we have just found for the first
110
109
// time.
111
- BLEAdvertisedDevice advertisedDevice;
112
- advertisedDevice.setAddress (advertisedAddress);
113
- advertisedDevice.setRSSI (param->scan_rst .rssi );
114
- advertisedDevice.setAdFlag (param->scan_rst .flag );
115
- advertisedDevice.parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv );
116
- advertisedDevice.setScan (this );
110
+ // ESP_LOG_BUFFER_HEXDUMP(LOG_TAG, (uint8_t*)param->scan_rst.ble_adv, param->scan_rst.adv_data_len + param->scan_rst.scan_rsp_len, ESP_LOG_DEBUG);
111
+ // ESP_LOGW(LOG_TAG, "bytes length: %d + %d, addr type: %d", param->scan_rst.adv_data_len, param->scan_rst.scan_rsp_len, param->scan_rst.ble_addr_type);
112
+ BLEAdvertisedDevice *advertisedDevice = new BLEAdvertisedDevice ();
113
+ advertisedDevice->setAddress (advertisedAddress);
114
+ advertisedDevice->setRSSI (param->scan_rst .rssi );
115
+ advertisedDevice->setAdFlag (param->scan_rst .flag );
116
+ advertisedDevice->parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv , param->scan_rst .adv_data_len + param->scan_rst .scan_rsp_len );
117
+ advertisedDevice->setScan (this );
118
+ advertisedDevice->setAddressType (param->scan_rst .ble_addr_type );
117
119
118
- if (m_pAdvertisedDeviceCallbacks ) {
119
- m_pAdvertisedDeviceCallbacks-> onResult ( advertisedDevice);
120
+ if (!found ) { // If we have previously seen this device, don't record it again.
121
+ m_scanResults. m_vectorAdvertisedDevices . insert (std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress. toString (), advertisedDevice) );
120
122
}
121
123
122
- if (!found ) { // If we have previously seen this device, don't record it again.
123
- m_scanResults. m_vectorAdvertisedDevices . push_back ( advertisedDevice);
124
+ if (m_pAdvertisedDeviceCallbacks ) {
125
+ m_pAdvertisedDeviceCallbacks-> onResult (* advertisedDevice);
124
126
}
127
+ if (found)
128
+ delete advertisedDevice;
125
129
126
130
break ;
127
131
} // ESP_GAP_SEARCH_INQ_RES_EVT
128
132
129
- default :
133
+ default : {
130
134
break ;
135
+ }
131
136
} // switch - search_evt
132
137
133
138
134
139
break ;
135
140
} // ESP_GAP_BLE_SCAN_RESULT_EVT
136
141
137
- default :
142
+ default : {
138
143
break ;
144
+ } // default
139
145
} // End switch
140
146
} // gapEventHandler
141
147
@@ -188,15 +194,23 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
188
194
* @brief Start scanning.
189
195
* @param [in] duration The duration in seconds for which to scan.
190
196
* @param [in] scanCompleteCB A function to be called when scanning has completed.
197
+ * @param [in] are we continue scan (true) or we want to clear stored devices (false)
191
198
* @return True if scan started or false if there was an error.
192
199
*/
193
- bool BLEScan::start (uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
200
+ bool BLEScan::start (uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue ) {
194
201
ESP_LOGD (LOG_TAG, " >> start(duration=%d)" , duration);
195
202
196
203
m_semaphoreScanEnd.take (std::string (" start" ));
197
204
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
198
205
199
- m_scanResults.m_vectorAdvertisedDevices .clear ();
206
+ // if we are connecting to devices that are advertising even after being connected, multiconnecting peripherals
207
+ // then we should not clear map or we will connect the same device few times
208
+ if (!is_continue) {
209
+ for (auto _dev : m_scanResults.m_vectorAdvertisedDevices ){
210
+ delete _dev.second ;
211
+ }
212
+ m_scanResults.m_vectorAdvertisedDevices .clear ();
213
+ }
200
214
201
215
esp_err_t errRc = ::esp_ble_gap_set_scan_params (&m_scan_params);
202
216
@@ -226,8 +240,8 @@ bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
226
240
* @param [in] duration The duration in seconds for which to scan.
227
241
* @return The BLEScanResults.
228
242
*/
229
- BLEScanResults BLEScan::start (uint32_t duration) {
230
- if (start (duration, nullptr )) {
243
+ BLEScanResults BLEScan::start (uint32_t duration, bool is_continue ) {
244
+ if (start (duration, nullptr , is_continue )) {
231
245
m_semaphoreScanEnd.wait (" start" ); // Wait for the semaphore to release.
232
246
}
233
247
return m_scanResults;
@@ -244,24 +258,31 @@ void BLEScan::stop() {
244
258
esp_err_t errRc = ::esp_ble_gap_stop_scanning ();
245
259
246
260
m_stopped = true ;
261
+ m_semaphoreScanEnd.give ();
247
262
248
263
if (errRc != ESP_OK) {
249
264
ESP_LOGE (LOG_TAG, " esp_ble_gap_stop_scanning: err: %d, text: %s" , errRc, GeneralUtils::errorToString (errRc));
250
265
return ;
251
266
}
252
267
253
- m_semaphoreScanEnd.give ();
254
-
255
268
ESP_LOGD (LOG_TAG, " << stop()" );
256
269
} // stop
257
270
271
+ // delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address
272
+ void BLEScan::erase (BLEAddress address) {
273
+ ESP_LOGI (LOG_TAG, " erase device: %s" , address.toString ().c_str ());
274
+ BLEAdvertisedDevice *advertisedDevice = m_scanResults.m_vectorAdvertisedDevices .find (address.toString ())->second ;
275
+ m_scanResults.m_vectorAdvertisedDevices .erase (address.toString ());
276
+ delete advertisedDevice;
277
+ }
278
+
258
279
259
280
/* *
260
281
* @brief Dump the scan results to the log.
261
282
*/
262
283
void BLEScanResults::dump () {
263
284
ESP_LOGD (LOG_TAG, " >> Dump scan results:" );
264
- for (uint32_t i = 0 ; i < getCount (); i++) {
285
+ for (int i= 0 ; i< getCount (); i++) {
265
286
ESP_LOGD (LOG_TAG, " - %s" , getDevice (i).toString ().c_str ());
266
287
}
267
288
} // dump
@@ -283,7 +304,14 @@ int BLEScanResults::getCount() {
283
304
* @return The device at the specified index.
284
305
*/
285
306
BLEAdvertisedDevice BLEScanResults::getDevice (uint32_t i) {
286
- return m_vectorAdvertisedDevices.at (i);
307
+ uint32_t x = 0 ;
308
+ BLEAdvertisedDevice dev = *m_vectorAdvertisedDevices.begin ()->second ;
309
+ for (auto it = m_vectorAdvertisedDevices.begin (); it != m_vectorAdvertisedDevices.end (); it++) {
310
+ dev = *it->second ;
311
+ if (x==i) break ;
312
+ x++;
313
+ }
314
+ return dev;
287
315
}
288
316
289
317
0 commit comments