Using scanning causes program crashes

static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
  ret_code_t err_code;
  NRF_LOG_INFO("Received BLE event: %d", p_ble_evt->header.evt_id);
  switch (p_ble_evt->header.evt_id)
  {

  case BLE_GAP_EVT_ADV_REPORT: // 29
  {
    NRF_LOG_INFO("Report_I");
    on_adv_report(&(p_ble_evt->evt.gap_evt));
    break;
  }

/**@brief Function called on BLE_GAP_EVT_ADV_REPORT event.
 *
 * @details Create a connection if received advertising packet corresponds to desired BLE device.
 *
 * @param[in] p_ble_gap_evt Advertising Report Event.
 */
void on_adv_report(const ble_gap_evt_t *const p_ble_gap_evt)
{
  uint32_t err_code = 0;
  uint8_t str[STRING_BUFFER_SIZE] = {0};
  NRF_LOG_INFO("Adv Report.");

  // Log the Bluetooth device address of advertisement packet received.
  // ble_address_to_string_convert(p_ble_gap_evt->params.adv_report.peer_addr, str);
  const uint8_t *address = p_ble_gap_evt->params.adv_report.peer_addr.addr;
  char _temp_str[250];
  memset(_temp_str, 0, 250);
  sprintf(_temp_str, "Advertising Report: Device Address: %02x%02x%02x%02x%02x%02x, ", address[5], address[4], address[3], address[2], address[1], address[0]);
  sprintf(_temp_str + strlen(_temp_str), "Addr Type: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_type);
  sprintf(_temp_str + strlen(_temp_str), "Addr ID Peer: 0x%d, ", p_ble_gap_evt->params.adv_report.peer_addr.addr_id_peer);
  sprintf(_temp_str + strlen(_temp_str), "Device Rssi: %d\n", p_ble_gap_evt->params.adv_report.rssi);
  usb_printf_variadic("%s\r\n", _temp_str);
  // usb_printf_variadic("Advertising Data: %s", p_ble_gap_evt->params.adv_report.data.p_data);
  NRF_LOG_INFO("SCAN_START_1");
  nrf_delay_us(100000);
  NRF_LOG_INFO("DELAY_END");
  err_code = sd_ble_gap_scan_start(NULL, &m_adv_report_buffer);
  NRF_LOG_INFO("SCAN_START_2");

  if (err_code != NRF_SUCCESS)
  {
    usb_printf_variadic("Error starting scan. Error code 0x%02X\r\n", err_code);
  }
  else
  {
    // //printf("Scan started\n");
    // usb_printf_variadic("GAP Scan Start\n");
  }
}

The above is my code.


[19:03:32.288]收←◆<info> app: SCAN_START_2
<info> app: Received BLE event: 29
<info> app: Report_I
<info> app: Adv Report.
<info> app: SCAN_START_1
<info> app: DELAY_END
<info> app: SCAN_START_2
<info> app: Received BLE event: 29
<info> app: Report_I
<info> app: Adv Report.
<info> app: SCAN_START_1
<info> app: DELAY_END
<info> app: SCAN_START_2

[19:03:32.443]收←◆<info> app: Received BLE event: 29

This is running log.

After scanning multiple times, my application may freeze, as shown in the log, and there will be no response after receiving the event.

Related