central connection error

Hi Everyone.

IDE : Vs Code

SDK : nrfConnect SDK

I have tried to connect peripheral device based on address. User gives input , address of ble device and based on that, I would start ble connection request.

I was successfull to connect, If I call below api inside scan_response function. But, If I wait for user input and call for same api, the connection callback throws error -2 and sometimes by luck it connects.

I suppose central can only start connection request while peripheral is still adversting.

Now, I wish to know how to start a connection at my will, and make sure it connects. Also, what should be min max intervals, so that connection request would wait for sometime.

 

tm_bool_t bt_connect(const bt_addr_le_t *addr,struct bt_conn **conn)
{ 
  int err;
  struct bt_le_conn_param param2,*param;
  param = BT_LE_CONN_PARAM_DEFAULT;
//   param->latency = 400;
//   param->timeout = 400;
param2.interval_max = 0x28;
param2.interval_min = 0x18;
param2.latency = 10;
param2.timeout = 400;

 
  err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
      &param2,  conn);
  if (err) 
  {
    sprintf(tx_buf,"Create conn failed (err %d)\n", err);
    DPRINTF(tx_buf); /* Debug print in uart */
    return FALSE;
    
  }
  sprintf(tx_buf,"Create conn success (err %d)\n", err);
    DPRINTF(tx_buf);
  return TRUE;

}

Edit:

If connection timeout's I get error 2 inside connection_callback;

After that If I try  again to connect using same address, I get -EINVAL(22) response  and connection doesn't create. 

if (bt_conn_exists_le(BT_ID_DEFAULT, peer)) {
		return -EINVAL;
	}

how to prevent this happening again?

  • I got the problem fixed.

    FYI: I am working on BLE CENTRAL role.

    1.I would get connection callback with err : 2, which means connection timeout occured .

    2.connection create error : -22, which means there's already one existing connection with same address.

    FIX:

    Once you get timeout error

    ->Clear struct bt_conn * Variable, which you have passed to bt_conn_le_create();

    This will allow to retry with same address again, otherwise you get error -22.

    Thank you.

    Correct me If I'm wrong, Nordic Team.

    Regards

    Visweswara Sarma

  • That should do it. The BLE stack should free any buffer used for the connection automatically before the application receives the disconnection event. So you should be free to just delete the connection data.

Related