Soft Device Scan Parms not being set correctly from struct

I set my scan params in the following init code.

 

/**@brief Function for initializing the scanning and setting the filters.
 */
static void scan_init(void)
{   
    ret_code_t          err_code;
    nrf_ble_scan_init_t init_scan;
    ble_gap_scan_params_t const m_scan_param =
    {
      .active        = 0x01,
      .interval      = NRF_BLE_SCAN_SCAN_INTERVAL,
      .window        = NRF_BLE_SCAN_SCAN_WINDOW,
      .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, //BLE_GAP_SCAN_FP_WHITELIST,
      .timeout       = 0,
      .scan_phys     = BLE_GAP_PHY_1MBPS,
    };

    memset(&init_scan, 0, sizeof(init_scan));
    /**< Scan parameters requested for scanning and connection. */
    init_scan.p_scan_param     = &m_scan_param;
    init_scan.connect_if_match = false;
    init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;

    err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

}

The interval and scan window are both set to 100. However, when I watch the variables in debug mode, the variables are as follows. 

Any idea how they are being set so wrong? No other references to these variables in the code anywhere except here and when I start scanning following the advertising period ending (60 seconds).

Parents
  • Hi,

    Any idea how they are being set so wrong? No other references to these variables in the code anywhere except here and when I start scanning following the advertising period ending (60 seconds).

    You should use a static variable for the ble_gap_scan_params_t structure used for the p_scan_param pointer in the p_init argument to nrf_ble_scan_init().

    In your implementation, the variable is not static, so when your function scan_init() returns the pointer points to a location in stack memory that will later be overwritten by the local variables of other functions.

    For an example of working initialization with custom scan parameters, see the Advanced initialization section of the Scanning Module documentation.

    Regards,
    Terje

Reply
  • Hi,

    Any idea how they are being set so wrong? No other references to these variables in the code anywhere except here and when I start scanning following the advertising period ending (60 seconds).

    You should use a static variable for the ble_gap_scan_params_t structure used for the p_scan_param pointer in the p_init argument to nrf_ble_scan_init().

    In your implementation, the variable is not static, so when your function scan_init() returns the pointer points to a location in stack memory that will later be overwritten by the local variables of other functions.

    For an example of working initialization with custom scan parameters, see the Advanced initialization section of the Scanning Module documentation.

    Regards,
    Terje

Children
Related