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

How to reconnect faster after a disconnection

Hello there,

I am working on a project in which two devices(one is central and other is peripheral ) uses Nordic bluetooth controller  for connection and data exchange. nrf51822 for peripheral and nrf51422 for the central. We are using SDK 12.3 and S130.
As in our product, there are high chances of peripheral going out of range and reconnecting back quite frequently. Now the reconnection takes alonger time.
I wanted to understand how the reconnection can be made faster.
Currently in our code, at the time disconnection
    case BLE_GAP_EVT_DISCONNECTED:  
   err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  sd_ble_gap_scan_stop();
  scan_start();    
  sd_ble_gap_adv_stop ();
  sd_softdevice_disable()
  nrf_drv_clock_on_sd_disable()
  nrf_drv_rng_on_sd_disable();
  NVIC_DisableIRQ(TIMER2_IRQn);
  NRF_TIMER2->TASKS_STOP = 1; 
  NRF_TIMER2->POWER = 0; 
  nrf_power_pofcon_set(true, NRF_POWER_POFTHR_V27);

case BLE_GAP_EVT_CONNECTED:
 conn_handle=p_ble_evt->evt.gattc_evt.conn_handle;
 err_code = ble_db_discovery_start(&m_ble_db_discovery, p_ble_evt->evt.gap_evt.conn_handle);
Please suggest if this is the correct way of implementing connection and disconnection, so that we have a faster reconnection.
Thanks,
Thomas
  • Now the reconnection takes alonger time.

    Please clarify: do you just mean that it is a "long" time, or has something changed so that it is now "longer" than it once was?

    How long is it actually taking?

    How long do you need it to take?

    The peripheral's Advertising Interval will affect how quickly the Central can find it - and, thus, how quickly it can connect.

    If either the Central or the Peripheral spends time not scanning or advsrtising (eg, sleeping) that will obviously impact connection time.

    How to properly post source code:

  • What role is your device, Central or Peripheral?

    Also
     : 

    case BLE_GAP_EVT_DISCONNECTED:  
      /*    Why are you asking the SoftDevice to disconnect the link when 
            you just got told that the link is disconnected? */
      err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
      /*    Why do you not catch your error codes? An APP_ERROR_CHECK(err_code)
            would have raised an error and you would have been told by the log
            module that your previous SD call returned an error. The error code
            is defined in the SoftDevice API documentation for any given API call*/
      
      /* Why do you stop scanning? */
      sd_ble_gap_scan_stop();
      
      /* Why do you start scanning? */
      scan_start();    
      
      /* Why do you stop advertising? */
      sd_ble_gap_adv_stop (); 
      
      /* Why do you disable the SoftDevice? */
      sd_softdevice_disable()
      
      /* Why do you turn off clocks and a timer? */
      nrf_drv_clock_on_sd_disable()
      nrf_drv_rng_on_sd_disable();
      NVIC_DisableIRQ(TIMER2_IRQn);
      NRF_TIMER2->TASKS_STOP = 1; 
      NRF_TIMER2->POWER = 0; 
      
      /* Why do you turn on the POFW? */
      nrf_power_pofcon_set(true, NRF_POWER_POFTHR_V27);

  • The reconnection takes 2 -3 seconds after the central(remote) move into the range of peripheral(wheel).

    Is 2-3 seconds an ideal value or should it be lesser ?

    The code snippet shared above is from central.

    Thanks,

    Thomas

  • The code snippet shared above is from central. The earlier code snippet is from one of our products.

    We wanted to improve the connection/reconnection behaviour for this product.

    I think calling sd_ble_gap_disconnect() when it is disconnected is unnecessary.

    Also, the below code is not needed at the time of disconnection

    /* Why do you disable the SoftDevice? */
      sd_softdevice_disable()
     
      /* Why do you turn off clocks and a timer? */
      nrf_drv_clock_on_sd_disable()
      nrf_drv_rng_on_sd_disable();
      NVIC_DisableIRQ(TIMER2_IRQn);
      NRF_TIMER2->TASKS_STOP = 1;
      NRF_TIMER2->POWER = 0;
     
      /* Why do you turn on the POFW? */
      nrf_power_pofcon_set(true, NRF_POWER_POFTHR_V27);

    So currently my disconnection event handling code looks like

    case BLE_GAP_EVT_DISCONNECTED: 
      
      /* start scanning to connect with the peripheral */
      scan_start();   

    Is this the correct method.

Related