This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Advertising timeout event catch with SDK16

Hi everyonoe,

I would like to set a timeout for advertising, and catch this event. So I have

// Initialize advertising parameters (used when starting advertising).
		memset(&m_adv_params, 0, sizeof(m_adv_params));

		m_adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
		m_adv_params.p_peer_addr     = NULL;    // Undirected advertisement.
		m_adv_params.filter_policy   = BLE_GAP_ADV_FP_ANY;
    	m_adv_params.duration        = 500;       // 500*10ms = 5s
		m_adv_params.interval 		 = MSEC_TO_UNITS(4000, UNIT_0_625_MS); // 9s advertising so timeout occurs before, and SoftDevice accepts the value

According to ble_gap.h :

  uint16_t                 duration;                /**< Advertising duration in 10 ms units. When timeout is reached,
                                                         an event of type @ref BLE_GAP_EVT_ADV_SET_TERMINATED is raised.

So in the handler I registered with NRF_SDH_BLE_OBSERVER, I have :

static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:

        	ble_evt_ConnectedEvent_handler();
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_DISCONNECTED:

        	ble_evt_DisconnectedEvent_handler();
            // LED indication will be changed when advertising starts.
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
//            NRF_LOG_DEBUG("PHY update request.");
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
            // Pairing not supported
            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_SYS_ATTR_MISSING:
            // No system attributes have been stored.
            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;
            
        case BLE_GAP_EVT_ADV_SET_TERMINATED:
        	printf("AdvTimeout!");
        	break;
        	
            default:
            break;
            
    }
}

My issue is : after 5s, I don't have "Timeout!" printed in my console. But if I set an advertising interval greater than 5s, "Timeout!" is printed immediately at boot, several time, until it reboots. The other events works well (connection, deconnection...)

Is there something I missed?

Thank you for your help!

++

  • Hi,

    m_adv_params.interval 		 = MSEC_TO_UNITS(4000, UNIT_0_625_MS); // 9s advertising so timeout occurs before, and SoftDevice accepts the value
    

    When declared like this the advertising interval is set to 4 seconds, since:

    /**@brief Macro for converting milliseconds to ticks.
     *
     * @param[in] TIME          Number of milliseconds to convert.
     * @param[in] RESOLUTION    Unit to be converted to in [us/ticks].
     */
    #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))

    NIT_0_625_MS = 625,        /**< Number of microseconds in 0.625 milliseconds. */

    The advertising duration should be declared in units of 10 ms, so you should use UNIT_10_MS:

    m_adv_params.duration        = MSEC_TO_UNITS(5000, UNIT_10_MS);       
    

    Best regards,

    Marjeris

  • Hello, thank you for your help

    You are right about the 4s, the comment is wrong.

    In between, I found another solution with external timers, but I'll eventually test the following :

    m_adv_params.duration        = MSEC_TO_UNITS(5000, UNIT_10_MS); 

    As you suggest.

    I didn't though my conversion could be the issue there, your advice is relevant, thank you.

Related