@@ -58,7 +58,7 @@ BLEScan::BLEScan() {
5858 m_scan_params.filter_policy = BLE_HCI_SCAN_FILT_NO_WL;
5959 m_scan_params.passive = 1 ; // If set, don’t send scan requests to advertisers (i.e., don’t request additional advertising data).
6060 m_scan_params.limited = 0 ; // If set, only discover devices in limited discoverable mode.
61- m_scan_params.filter_duplicates = 0 ; // If set, the controller ignores all but the first advertisement from each device.
61+ m_scan_params.filter_duplicates = 1 ; // If set, the controller ignores all but the first advertisement from each device.
6262 m_pAdvertisedDeviceCallbacks = nullptr ;
6363 m_ignoreResults = false ;
6464 m_pTaskData = nullptr ;
@@ -363,6 +363,7 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_
363363 advertisedDevice->setAddress (advertisedAddress);
364364 advertisedDevice->setRSSI (param->scan_rst .rssi );
365365 advertisedDevice->setAdFlag (param->scan_rst .flag );
366+ advertisedDevice->setAdvType (param->scan_rst .ble_evt_type );
366367 if (m_shouldParse) {
367368 advertisedDevice->parseAdvertisement ((uint8_t *)param->scan_rst .ble_adv , param->scan_rst .adv_data_len + param->scan_rst .scan_rsp_len );
368369 } else {
@@ -610,13 +611,16 @@ int BLEScan::handleGAPEvent(ble_gap_event *event, void *arg) {
610611 return 0 ;
611612 }
612613
613- BLEAddress advertisedAddress (event->disc .addr );
614+ const auto &disc = event->disc ;
615+ const auto event_type = disc.event_type ;
616+ const bool isLegacyAdv = true ;
617+ BLEAddress advertisedAddress (disc.addr );
614618
615- /* // Examine our list of ignored addresses and stop processing if we don't want to see it or are already connected
616- if (BLEDevice::isIgnored(advertisedAddress )) {
617- log_i("Ignoring device: address: %s ", advertisedAddress.toString().c_str());
619+ BLEClient *client = BLEDevice::getClientByAddress (advertisedAddress);
620+ if (client != nullptr && client-> isConnected ( )) {
621+ log_i (" Client %s connected - ignoring event " , advertisedAddress.toString ().c_str ());
618622 return 0 ;
619- } */
623+ }
620624
621625 BLEAdvertisedDevice *advertisedDevice = nullptr ;
622626
@@ -630,16 +634,21 @@ int BLEScan::handleGAPEvent(ble_gap_event *event, void *arg) {
630634
631635 // If we haven't seen this device before; create a new instance and insert it in the vector.
632636 // Otherwise just update the relevant parameters of the already known device.
633- if (advertisedDevice == nullptr && event-> disc . event_type != BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
637+ if (advertisedDevice == nullptr ) {
634638 // Check if we have reach the scan results limit, ignore this one if so.
635639 // We still need to store each device when maxResults is 0 to be able to append the scan results
636640 if (pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF && (pScan->m_scanResults .m_vectorAdvertisedDevices .size () >= pScan->m_maxResults )) {
637641 return 0 ;
638642 }
643+
644+ if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
645+ log_i (" Scan response without advertisement: %s" , advertisedAddress.toString ().c_str ());
646+ }
647+
639648 advertisedDevice = new BLEAdvertisedDevice ();
640649 advertisedDevice->setAddress (advertisedAddress);
641650 advertisedDevice->setAddressType (event->disc .addr .type );
642- advertisedDevice->setAdvType (event-> disc . event_type );
651+ advertisedDevice->setAdvType (event_type);
643652 pScan->m_scanResults .m_vectorAdvertisedDevices .insert (std::pair<std::string, BLEAdvertisedDevice *>(advertisedAddress.toString ().c_str (), advertisedDevice));
644653 log_i (" New advertiser: %s" , advertisedAddress.toString ().c_str ());
645654 } else if (advertisedDevice != nullptr ) {
@@ -654,28 +663,28 @@ int BLEScan::handleGAPEvent(ble_gap_event *event, void *arg) {
654663 if (pScan->m_shouldParse ) {
655664 advertisedDevice->parseAdvertisement ((uint8_t *)event->disc .data , event->disc .length_data );
656665 } else {
657- advertisedDevice->setPayload ((uint8_t *)event->disc .data , event->disc .length_data , event-> disc . event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP);
666+ advertisedDevice->setPayload ((uint8_t *)event->disc .data , event->disc .length_data , event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP);
658667 }
659668
660669 advertisedDevice->setScan (pScan);
661670
662671 if (pScan->m_pAdvertisedDeviceCallbacks ) {
663672 // If not active scanning or scan response is not available
664673 // report the result to the callback now.
665- if (pScan->m_scan_params .passive
666- || (advertisedDevice->getAdvType () != BLE_HCI_ADV_TYPE_ADV_IND && advertisedDevice->getAdvType () != BLE_HCI_ADV_TYPE_ADV_SCAN_IND)) {
674+ if (pScan->m_scan_params .passive || !isLegacyAdv || !advertisedDevice->isScannable ()) {
667675 advertisedDevice->m_callbackSent = true ;
668676 pScan->m_pAdvertisedDeviceCallbacks ->onResult (*advertisedDevice);
669677
670678 // Otherwise, wait for the scan response so we can report the complete data.
671- } else if (event-> disc . event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
679+ } else if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
672680 advertisedDevice->m_callbackSent = true ;
673681 pScan->m_pAdvertisedDeviceCallbacks ->onResult (*advertisedDevice);
674682 }
675- // If not storing results and we have invoked the callback, delete the device.
676- if (pScan->m_maxResults == 0 && advertisedDevice->m_callbackSent ) {
677- pScan->erase (advertisedAddress);
678- }
683+ }
684+
685+ // If not storing results and we have invoked the callback, delete the device.
686+ if (pScan->m_maxResults == 0 && advertisedDevice->m_callbackSent ) {
687+ pScan->erase (advertisedAddress);
679688 }
680689
681690 return 0 ;
@@ -724,6 +733,10 @@ int BLEScan::handleGAPEvent(ble_gap_event *event, void *arg) {
724733bool BLEScan::start (uint32_t duration, void (*scanCompleteCB)(BLEScanResults), bool is_continue) {
725734 log_d (" >> start(duration=%d)" , duration);
726735
736+ if (!is_continue) {
737+ clearResults ();
738+ }
739+
727740 // Save the callback to be invoked when the scan completes.
728741 m_scanCompleteCB = scanCompleteCB;
729742 // Save the duration in the case that the host is reset so we can reuse it.
@@ -746,11 +759,6 @@ bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults), b
746759
747760 switch (rc) {
748761 case 0 :
749- if (!is_continue) {
750- clearResults ();
751- }
752- break ;
753-
754762 case BLE_HS_EALREADY:
755763 break ;
756764
0 commit comments