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
Parents
  • 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 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.

  • Yes. You should also look at your scan window and scan interval, as well as the peripherals advertisement interval. You can go as low as a few tenths of ms, but then you'll draw a lot of current in both devices. 

    What is your current scan window and interval, and advertisement interval? 

  • Hi Haakonsh,

    Thanks for your inputs.

    At the peripheral side, we have advertising interval as 200 ms and advertising timeout as 180 seconds.

    Minimum connection interval is 7.5 ms, max connection interval is 30 ms and supervision timeout is 4 seconds.

    And at the central, scan interval is 600 ms, scan window=300ms

    Min connection interval = 7.5ms, max connection interval=30 ms and supervision timeout=1 sec.

    Below the values as in our code.

    Peripheral side
    ---------------
    #define APP_ADV_INTERVAL                 320//320 means 200msec //80//240//240 means 150msec//304//64 means 190msec //80//152//320//80//32//320//150//320                                         /**< The advertising interval (in units of 0.625 ms. This value corresponds to 200 ms). */
    #define APP_ADV_INTERVAL_PAIRING_MODE    80                                          /**< The advertising interval (in units of 0.625 ms. This value corresponds to 50 ms). */
    #define APP_ADV_TIMEOUT_IN_SECONDS       180                                         /**< The advertising timeout in units of seconds. */

    #define MIN_CONN_INTERVAL                MSEC_TO_UNITS(7.5, UNIT_1_25_MS)//MSEC_TO_UNITS(400, UNIT_1_25_MS)            /**< Minimum acceptable connection interval (0.4 seconds). */
    #define MAX_CONN_INTERVAL                MSEC_TO_UNITS(30, UNIT_1_25_MS)            /**< Maximum acceptable connection interval (0.65 second). */
    #define SLAVE_LATENCY                    0                                           /**< Slave latency. */
    #define CONN_SUP_TIMEOUT                 MSEC_TO_UNITS(4000, UNIT_10_MS)             /**< Connection supervisory timeout (4 seconds). */
    Central side
    ---------------
    #define SCAN_INTERVAL               960                                 /**< Determines scan interval in units of 0.625 millisecond. 960 is equivalent to 600msec*/
    #define SCAN_WINDOW                 480                                 /**< Determines scan window in units of 0.625 millisecond. 480 is eauivalent to 300msec*/
    #define SCAN_TIMEOUT                0x0000                              /**< Timout when scanning. 0x0000 disables timeout. */
    #define MIN_CONNECTION_INTERVAL     MSEC_TO_UNITS(7.5, UNIT_1_25_MS)    /**< Determines minimum connection interval in millisecond. */
    #define MAX_CONNECTION_INTERVAL     MSEC_TO_UNITS(30, UNIT_1_25_MS)     /**< Determines maximum connection interval in millisecond. */
    #define SLAVE_LATENCY               0                                   /**< Determines slave latency in counts of connection events. */
    #define SUPERVISION_TIMEOUT         MSEC_TO_UNITS(1000, UNIT_10_MS)     /**< Determines supervision time-out in units of 10 millisecond. */

    Please suggest if these are the ideal values, if we want a faster connection'/reconnection.

    Our product will have frequent reconnections as it is common for the peripheral to go out of range more often.

    Thanks,

    Thomas

  • If you're using connection intervals of 7.5ms then I assume you expect to use a lot of energy for this connection, therefore I suggest that you drastically lower the advertisement and scan intervals. At least for a short duration after a disconnection event. 

    See Advertising modesble_adv_fast_interval and ble_adv_fast_timeout

Reply Children
No Data
Related