@@ -726,6 +726,10 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
726726
727727 DEBUG_PRINT ("Advertisement matched, waiting for scan response\n" );
728728
729+ // Copy data to allow virtual re-connect in a new user program.
730+ peri -> config .match_adv_data_len = gap_event_advertising_report_get_data_length (event_packet );
731+ memcpy (peri -> config .match_adv_data , gap_event_advertising_report_get_data (event_packet ), peri -> config .match_adv_data_len );
732+
729733 // Wait for advertising response unless timed out or cancelled.
730734 PBIO_OS_AWAIT_UNTIL (state , scan_timed_out || peri -> cancel ||
731735 (hci_event_is_type (event_packet , GAP_EVENT_ADVERTISING_REPORT ) && ({
@@ -748,12 +752,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
748752 // that event data for processing.
749753 const uint8_t * data = gap_event_advertising_report_get_data (event_packet );
750754 uint8_t data_len = gap_event_advertising_report_get_data_length (event_packet );
751- if (peri -> config .match_adv_rsp (peri -> user , data , data_len )) {
752- // Copy name for later use.
753- if (data [1 ] == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME ) {
754- memcpy (peri -> name , & data [2 ], sizeof (peri -> name ));
755- }
756- } else {
755+ if (!peri -> config .match_adv_rsp (peri -> user , data , data_len )) {
757756 // We got a valid scan response from the device that matched our
758757 // advertising filter, but it did not match the response filter (e.g.
759758 // requested name did not match), so scan again.
@@ -763,6 +762,14 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
763762
764763 DEBUG_PRINT ("Scan response matched, initiate connection to %s.\n" , bd_addr_to_str (peri -> bdaddr ));
765764
765+ // Copy name for later use.
766+ if (data [1 ] == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME ) {
767+ memcpy (peri -> name , & data [2 ], sizeof (peri -> name ));
768+ }
769+ // Copy response data to allow virtual re-connect in a new user program.
770+ peri -> config .match_adv_rsp_data_len = data_len ;
771+ memcpy (peri -> config .match_adv_rsp_data , data , data_len );
772+
766773 // We can stop scanning now.
767774 gap_stop_scan ();
768775
0 commit comments