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

After Disconnecting how to advertise after every 1 minute

Hi Nordic Team,

My requirement is-

Advertise -> Connect to 1 device -> Send packet -> Disconnect -> Delay(1min) -> Advertise -> Connect to 2 device...

I am using Timer to make some delay to restart the advertisement for connecting to another peripheral, But if 2 peripheral is not connected within ADVERTISING_INTERVAL, its going to nrf breakpoint condition. Timer is not repeating for advertising again.

/*Timer init */

static void timers_init(void)
{

    // Initialize timer module.
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    /*creating timer */
    err_code = app_timer_create(&advertising_timer_id, APP_TIMER_MODE_REPEATED, advertising_timeout_handler);
    APP_ERROR_CHECK(err_code);

}

/*Timer Handler */

static void advertising_timeout_handler(void * p_context)
{    
     UNUSED_PARAMETER(p_context);
     bool erase_bonds;
     advertising_start(erase_bonds);
}

/*BLE event handler*/

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

    ret_code_t err_code = NRF_SUCCESS;
    bool erase_bonds;
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
            printf("Disconnected.\n");
            NRF_LOG_INFO("Disconnected.");
       
            err_code = app_timer_start(advertising_timer_id, ADVERTISING_INTERVAL, NULL);
            APP_ERROR_CHECK(err_code);
      
            break;

        case BLE_GAP_EVT_CONNECTED:

            err_code = app_timer_stop(advertising_timer_id);
            APP_ERROR_CHECK(err_code);

            ...

  }

Kindly Suggest the correct way to use advertising start() for periodical advertisement.

  • Hi,

    The error code is BLE_ERROR_INVALID_CONN_HANDLE (0x3002). Likely you are not in a connection at the time you try to disconnect.

    I would recommend to add a delay between "Send packet -> Disconnect". Either a fixed delay of for instance 100ms, or that you wait for for BLE_GATTS_EVT_HVN_TX_COMPLETE event before you disconnect:
    https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v7.0.1/group___b_l_e___g_a_t_t_s___h_v_n___m_s_c.html

    In general it consumes less power to stay connected if you are transmitting one packet every 1 minute. For instance use a connection interval of 400ms and slave latency of 10 and supervisor timeout of 6 seconds (e.g. 15 empty dummy packets each 1 minute). Then that will draw total average current of 3.8uA while in a connection. I think that is less power than if you need to advertise, establish connection, possible execute MTU exhanges, encryption, send packets and then disconnect every 1minute.

    Best regards,
    Kenneth

Related