@@ -30,6 +30,7 @@ BLEScan::BLEScan() {
30
30
m_pAdvertisedDeviceCallbacks = nullptr ;
31
31
m_stopped = true ;
32
32
m_wantDuplicates = false ;
33
+ m_shouldParse = true ;
33
34
setInterval (100 );
34
35
setWindow (100 );
35
36
} // BLEScan
@@ -90,15 +91,18 @@ void BLEScan::handleGAPEvent(
90
91
// ignore it.
91
92
BLEAddress advertisedAddress (param->scan_rst .bda );
92
93
bool found = false ;
93
-
94
- if (m_scanResults.m_vectorAdvertisedDevices .count (advertisedAddress.toString ()) != 0 ) {
95
- found = true ;
96
- }
97
-
98
- if (found && !m_wantDuplicates) { // If we found a previous entry AND we don't want duplicates, then we are done.
99
- log_d (" Ignoring %s, already seen it." , advertisedAddress.toString ().c_str ());
100
- vTaskDelay (1 ); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
101
- break ;
94
+ bool shouldDelete = true ;
95
+
96
+ if (!m_wantDuplicates) {
97
+ if (m_scanResults.m_vectorAdvertisedDevices .count (advertisedAddress.toString ()) != 0 ) {
98
+ found = true ;
99
+ }
100
+
101
+ if (found) { // If we found a previous entry AND we don't want duplicates, then we are done.
102
+ log_d (" Ignoring %s, already seen it." , advertisedAddress.toString ().c_str ());
103
+ vTaskDelay (1 ); // <--- allow to switch task in case we scan infinity and dont have new devices to report, or we are blocked here
104
+ break ;
105
+ }
102
106
}
103
107
104
108
// We now construct a model of the advertised device that we have just found for the first
@@ -109,19 +113,23 @@ void BLEScan::handleGAPEvent(
109
113
advertisedDevice->setAddress (advertisedAddress);
110
114
advertisedDevice->setRSSI (param->scan_rst .rssi );
111
115
advertisedDevice->setAdFlag (param->scan_rst .flag );
112
- advertisedDevice->parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv , param->scan_rst .adv_data_len + param->scan_rst .scan_rsp_len );
116
+ if (m_shouldParse) {
117
+ advertisedDevice->parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv , param->scan_rst .adv_data_len + param->scan_rst .scan_rsp_len );
118
+ } else {
119
+ advertisedDevice->setPayload ((uint8_t *)param->scan_rst .ble_adv , param->scan_rst .adv_data_len + param->scan_rst .scan_rsp_len );
120
+ }
113
121
advertisedDevice->setScan (this );
114
122
advertisedDevice->setAddressType (param->scan_rst .ble_addr_type );
115
123
116
- if (!found) { // If we have previously seen this device, don't record it again.
117
- m_scanResults.m_vectorAdvertisedDevices .insert (std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString (), advertisedDevice));
118
- }
119
-
120
- if (m_pAdvertisedDeviceCallbacks) {
124
+ if (m_pAdvertisedDeviceCallbacks) { // if has callback, no need to record to vector
121
125
m_pAdvertisedDeviceCallbacks->onResult (*advertisedDevice);
126
+ } else if (!m_wantDuplicates && !found) { // if no callback and not want duplicate, and not already in vector, record it
127
+ m_scanResults.m_vectorAdvertisedDevices .insert (std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString (), advertisedDevice));
128
+ shouldDelete = false ;
122
129
}
123
- if (found)
130
+ if (shouldDelete) {
124
131
delete advertisedDevice;
132
+ }
125
133
126
134
break ;
127
135
} // ESP_GAP_SEARCH_INQ_RES_EVT
@@ -161,13 +169,14 @@ void BLEScan::setActiveScan(bool active) {
161
169
* @brief Set the call backs to be invoked.
162
170
* @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked.
163
171
* @param [in] wantDuplicates True if we wish to be called back with duplicates. Default is false.
172
+ * @param [in] shouldParse True if we wish to parse advertised package or raw payload. Default is true.
164
173
*/
165
- void BLEScan::setAdvertisedDeviceCallbacks (BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates) {
174
+ void BLEScan::setAdvertisedDeviceCallbacks (BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates, bool shouldParse ) {
166
175
m_wantDuplicates = wantDuplicates;
167
176
m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
177
+ m_shouldParse = shouldParse;
168
178
} // setAdvertisedDeviceCallbacks
169
179
170
-
171
180
/* *
172
181
* @brief Set the interval to scan.
173
182
* @param [in] The interval in msecs.
0 commit comments