Concurrent Scan and Advertise NRF_ERROR_NOT_FOUND on ble_advertise_start

I am trying to build an application where the device scans and on receipt of a packet with certain data in it, advertises for a short while, using both advertising and scan response data in both the scan and advertisement.

I have been stuck for a few days now with the error ERROR 5 [NRF_ERROR_NOT_FOUND]

This is returned by sd_ble_gap_adv_start, which seems to indicate that either the adv_handle or conn_cfg_tag is not found.

I have tried any number of things, including stopping scanning before starting advertising, adjusting scan window, interval, advertising interval, using the default conn_cfg_tag, using the app conn_cfg_tag, using the NONCONNECTABLE advertising mode etc.

Initialization:

void bleStack_init(void)
{
   ret_code_t errCode = nrf_sdh_enable_request();
   APP_ERROR_CHECK(errCode);
   
   // Configure the BLE stack using the default settings. Fetch the start address of the application RAM.
   u32 ramStart = 0U;
   errCode = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ramStart);
   APP_ERROR_CHECK(errCode);

   // Enable BLE stack.
   errCode = nrf_sdh_ble_enable(&ramStart);
   APP_ERROR_CHECK(errCode);
   
   NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, &eventhandler, NULL);
   
   NRF_LOG_INFO("Stack Initialised!");
}

void bleScanDrv_init(void)
{
   ret_code_t          result;
   nrf_ble_scan_init_t bleScanInitStruct;
   memset(&bleScanInitStruct, 0, sizeof(bleScanInitStruct));

   bleScanInitStruct.p_scan_param     = &bleScanParamStruct;
   bleScanInitStruct.connect_if_match = false;
   bleScanInitStruct.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;

   result = nrf_ble_scan_init(&bleScanInstance, &bleScanInitStruct, bleScanEventHandler);
   APP_ERROR_CHECK(result);

   #define ENTITY_DEVICE_UUID {0x42,0x00,0x74,0xA9,0xFF,0x52,0x10,0x9B,0x33,0x49,0x35,0x9B,0x8A,0xCA,0x68,0xEF}
   #define ENTITY_DEVICE_UUID_SHORT ((u16)0xCA8A)

   BleUuidShort entityDeviceUuid = 
   {
      .uuid = ENTITY_DEVICE_UUID_SHORT,
      .type = BLE_UUID_TYPE_VENDOR_BEGIN,
   };

   BleUuid entityDeviceUuidBase = ENTITY_DEVICE_UUID;

   result =  sd_ble_uuid_vs_add(&entityDeviceUuidBase, &entityDeviceUuid.type);
   APP_ERROR_CHECK(result);

   result = nrf_ble_scan_filter_set(&bleScanInstance, SCAN_UUID_FILTER, &entityDeviceUuid);
   APP_ERROR_CHECK(result);

   result = nrf_ble_scan_filters_enable(&bleScanInstance, NRF_BLE_SCAN_UUID_FILTER, false);
   APP_ERROR_CHECK(result);
}

void bleAdvertiseDrv_init(void)
{
   // Register gateway advDataHandler
   bleGatewaySvc_registerAdvDataHandler(gatewayAdvDataHandler);

   //DECL_INIT_VAR(ble_advertising_init_t, advInitStruct);
   memset(&advInitStruct, 0u, sizeof(advInitStruct));

   // Set advertising speeds and timeouts
   advInitStruct.config.ble_adv_on_disconnect_disabled     = false,                      /**< Enable or disable automatic return to advertising upon disconnecting.*/
   advInitStruct.config.ble_adv_directed_enabled           = false,                      /**< Enable or disable direct advertising mode. */
   advInitStruct.config.ble_adv_fast_enabled               = true,                       /**< Enable or disable fast advertising mode. */
   advInitStruct.config.ble_adv_slow_enabled               = false,                      /**< Enable or disable slow advertising mode. */
   advInitStruct.config.ble_adv_fast_interval              = ADV_INTERVAL_MS_TO_VALUE(APP_ADV_INTERVAL_FAST_MS),   /**< Advertising interval for fast advertising. */
   advInitStruct.config.ble_adv_fast_timeout               = ADV_DURATION_MS_TO_VALUE(APP_ADV_DURATION_FAST_MS),   /**< Time-out (in units of 10ms) for fast advertising. */

   advInitStruct.evt_handler                               = &advertiseEventHandler;
   advInitStruct.error_handler                             = &advertiseErrorHandler;

   const u8 deviceNameDefault[] = "Device";
   
   ble_gap_conn_sec_mode_t sec_mode;
   BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
   u32 result = sd_ble_gap_device_name_set(&sec_mode, (const u8 *)&(deviceNameDefault[0]), 6u);
   APP_ERROR_CHECK(result);

   // Update advertisement data
   s8 txPowerDefault = 0u;
   
   //memset(&advData, 0, sizeof(advData));
   advInitStruct.advdata.name_type                       = BLE_ADVDATA_FULL_NAME;
   advInitStruct.advdata.include_appearance              = false;
   advInitStruct.advdata.flags                           = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
   advInitStruct.advdata.p_tx_power_level = &txPowerDefault;

   // Update scan response data
   advInitStruct.srdata.name_type             = BLE_ADVDATA_NO_NAME;
   
   ret_code_t errCode = ble_advertising_init(&advInstance, &advInitStruct);
   APP_ERROR_CHECK(errCode);

   ble_advertising_conn_cfg_tag_set(&advInstance, APP_BLE_CONN_CFG_TAG);
   didTxPowerDrv_setAdvertisePower(advInstance.adv_handle, didTxPowerDrv_Level_4dBm);
}

Advertising start:

// Message from scanResponse event handler to start advertising
static void scanResponseHandler(void *data, u16 size)
{
   if(!advStarted)
   {
      // Using this in order to update advertising data
      u32 result = ble_advertising_init(&advInstance, &advInitStruct);
      APP_ERROR_CHECK(result);
      
      // Start advertising
      result = ble_advertising_start(&advInstance, BLE_ADV_MODE_FAST); // NRF_ERROR_NOT_FOUND occurs here
      APP_ERROR_CHECK(result);

      advStarted = true;
   }
}

Related