@@ -32,21 +32,19 @@ BLEScan::BLEScan() {
32
32
} // BLEScan
33
33
34
34
35
- BLEScan::~BLEScan () {
36
- clearAdvertisedDevices ();
37
- } // ~BLEScan
38
-
39
35
40
36
/* *
41
37
* @brief Clear the history of previously detected advertised devices.
42
38
* @return N/A
43
39
*/
40
+ /*
44
41
void BLEScan::clearAdvertisedDevices() {
45
42
for (int i=0; i<m_vectorAvdertisedDevices.size(); i++) {
46
43
delete m_vectorAvdertisedDevices[i];
47
44
}
48
45
m_vectorAvdertisedDevices.clear();
49
46
} // clearAdvertisedDevices
47
+ */
50
48
51
49
52
50
/* *
@@ -92,31 +90,41 @@ void BLEScan::gapEventHandler(
92
90
// ignore it.
93
91
BLEAddress advertisedAddress (param->scan_rst .bda );
94
92
bool found = false ;
93
+ /*
95
94
for (int i=0; i<m_vectorAvdertisedDevices.size(); i++) {
96
95
if (m_vectorAvdertisedDevices[i]->getAddress().equals(advertisedAddress)) {
97
96
found = true;
98
97
break;
99
98
}
100
99
}
100
+ */
101
+ for (int i=0 ; i<m_scanResults.getCount (); i++) {
102
+ if (m_scanResults.getDevice (i).getAddress ().equals (advertisedAddress)) {
103
+ found = true ;
104
+ break ;
105
+ }
106
+ }
101
107
if (found) {
102
108
ESP_LOGD (LOG_TAG, " Ignoring %s, already seen it." , advertisedAddress.toString ().c_str ());
103
109
break ;
104
110
}
105
111
106
112
// We now construct a model of the advertised device that we have just found for the first
107
113
// time.
108
- BLEAdvertisedDevice* pAdvertisedDevice = new BLEAdvertisedDevice () ;
109
- pAdvertisedDevice-> setAddress (advertisedAddress);
110
- pAdvertisedDevice-> setRSSI (param->scan_rst .rssi );
111
- pAdvertisedDevice-> setAdFlag (param->scan_rst .flag );
112
- pAdvertisedDevice-> parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv );
113
- pAdvertisedDevice-> setScan (this );
114
-
115
- m_vectorAvdertisedDevices.push_back (pAdvertisedDevice);
114
+ BLEAdvertisedDevice advertisedDevice ;
115
+ advertisedDevice. setAddress (advertisedAddress);
116
+ advertisedDevice. setRSSI (param->scan_rst .rssi );
117
+ advertisedDevice. setAdFlag (param->scan_rst .flag );
118
+ advertisedDevice. parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv );
119
+ advertisedDevice. setScan (this );
120
+
121
+ // m_vectorAvdertisedDevices.push_back(pAdvertisedDevice);
116
122
if (m_pAdvertisedDeviceCallbacks) {
117
- m_pAdvertisedDeviceCallbacks->onResult (pAdvertisedDevice );
123
+ m_pAdvertisedDeviceCallbacks->onResult (advertisedDevice );
118
124
}
119
125
126
+ m_scanResults.m_vectorAdvertisedDevices .push_back (advertisedDevice);
127
+
120
128
break ;
121
129
} // ESP_GAP_SEARCH_INQ_RES_EVT
122
130
@@ -160,8 +168,8 @@ void BLEScan::setActiveScan(bool active) {
160
168
161
169
162
170
/* *
163
- * @brief Set the callbacks to be invoked.
164
- * @param [in] pAdvertisedDeviceCallbacks Callbacks to be invoked.
171
+ * @brief Set the call backs to be invoked.
172
+ * @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked.
165
173
*/
166
174
void BLEScan::setAdvertisedDeviceCallbacks (BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks) {
167
175
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
@@ -191,27 +199,27 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
191
199
* @param [in] duration The duration in seconds for which to scan.
192
200
* @return N/A.
193
201
*/
194
- std::vector<BLEAdvertisedDevice*> BLEScan::start (uint32_t duration) {
202
+ BLEScanResults BLEScan::start (uint32_t duration) {
195
203
ESP_LOGD (LOG_TAG, " >> start(%d)" , duration);
196
204
197
205
m_semaphoreScanEnd.take (" start" );
198
206
199
- clearAdvertisedDevices ();
207
+ m_scanResults. m_vectorAdvertisedDevices . empty ();
200
208
201
209
esp_err_t errRc = ::esp_ble_gap_set_scan_params (&m_scan_params);
202
210
203
211
if (errRc != ESP_OK) {
204
212
ESP_LOGE (LOG_TAG, " esp_ble_gap_set_scan_params: err: %d, text: %s" , errRc, GeneralUtils::errorToString (errRc));
205
213
m_semaphoreScanEnd.give ();
206
- return m_vectorAvdertisedDevices ;
214
+ return m_scanResults ;
207
215
}
208
216
209
217
errRc = ::esp_ble_gap_start_scanning (duration);
210
218
211
219
if (errRc != ESP_OK) {
212
220
ESP_LOGE (LOG_TAG, " esp_ble_gap_start_scanning: err: %d, text: %s" , errRc, GeneralUtils::errorToString (errRc));
213
221
m_semaphoreScanEnd.give ();
214
- return m_vectorAvdertisedDevices ;
222
+ return m_scanResults ;
215
223
}
216
224
217
225
m_stopped = false ;
@@ -220,7 +228,7 @@ std::vector<BLEAdvertisedDevice*> BLEScan::start(uint32_t duration) {
220
228
m_semaphoreScanEnd.give ();
221
229
222
230
ESP_LOGD (LOG_TAG, " << start()" );
223
- return m_vectorAvdertisedDevices ;
231
+ return m_scanResults ;
224
232
} // start
225
233
226
234
@@ -245,4 +253,24 @@ void BLEScan::stop() {
245
253
ESP_LOGD (LOG_TAG, " << stop()" );
246
254
} // stop
247
255
256
+
257
+ /* *
258
+ * @brief Return the count of devices found in the last scan.
259
+ * @return The number of devices found in the last scan.
260
+ */
261
+ int BLEScanResults::getCount () {
262
+ return m_vectorAdvertisedDevices.size ();
263
+ } // getCount
264
+
265
+
266
+ /* *
267
+ * @brief Return the specified device at the given index.
268
+ * The index should be between 0 and getCount()-1.
269
+ * @param [in] i The index of the device.
270
+ * @return The device at the specified index.
271
+ */
272
+ BLEAdvertisedDevice BLEScanResults::getDevice (uint32_t i) {
273
+ return m_vectorAdvertisedDevices.at (i);
274
+ }
275
+
248
276
#endif /* CONFIG_BT_ENABLED */
0 commit comments