@@ -30,6 +30,7 @@ BLEScan::BLEScan() {
3030m_pAdvertisedDeviceCallbacks = nullptr ;
3131m_stopped = true ;
3232m_wantDuplicates = false ;
33+ m_shouldParse = true ;
3334setInterval (100 );
3435setWindow (100 );
3536} // BLEScan
@@ -90,15 +91,18 @@ void BLEScan::handleGAPEvent(
9091// ignore it.
9192BLEAddress advertisedAddress (param->scan_rst .bda );
9293bool 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+ }
102106}
103107
104108// We now construct a model of the advertised device that we have just found for the first
@@ -109,19 +113,23 @@ void BLEScan::handleGAPEvent(
109113advertisedDevice->setAddress (advertisedAddress);
110114advertisedDevice->setRSSI (param->scan_rst .rssi );
111115advertisedDevice->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+ }
113121advertisedDevice->setScan (this );
114122advertisedDevice->setAddressType (param->scan_rst .ble_addr_type );
115123
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
121125m_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 ;
122129}
123- if (found)
130+ if (shouldDelete) {
124131delete advertisedDevice;
132+ }
125133
126134break ;
127135} // ESP_GAP_SEARCH_INQ_RES_EVT
@@ -161,13 +169,14 @@ void BLEScan::setActiveScan(bool active) {
161169 * @brief Set the call backs to be invoked.
162170 * @param [in] pAdvertisedDeviceCallbacks Call backs to be invoked.
163171 * @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.
164173 */
165- void BLEScan::setAdvertisedDeviceCallbacks (BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates) {
174+ void BLEScan::setAdvertisedDeviceCallbacks (BLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates, bool shouldParse ) {
166175m_wantDuplicates = wantDuplicates;
167176m_pAdvertisedDeviceCallbacks = pAdvertisedDeviceCallbacks;
177+ m_shouldParse = shouldParse;
168178} // setAdvertisedDeviceCallbacks
169179
170-
171180/* *
172181 * @brief Set the interval to scan.
173182 * @param [in] The interval in msecs.
0 commit comments