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

getting BLE_ERROR_INVALID_CONN_HANDLE when calling sd_ble_gap_disconnect (CENTRAL)

Hi there

I am having issues to perform disconnections from my central device when calling sd_ble_gap_disconnect()

most of the time I am able to disconnect properly using sd_ble_gap_disconnect() where it returns NRF_SUCCESS, but some times it returns BLE_ERROR_INVALID_CONN_HANDLE  from my understanding it means that for some reason the conn handle changed to something else different than 0. why it happens? and how to work around it? I am only using one link connection per time.

What I was thinking Is I was already disconnected at the moment calling sd_ble_gap_disconnect(), but I am tracking the connection status using a global variable and the conn handle at on_ble_evt(ble_evt_t * p_ble_evt) before call sd_ble_gap_disconnect();

questions:

1) A conn handle = BLE_CONN_HANDLE_INVALID can be taken as indicative I am already disconnected?

2) why  err_code = sd_ble_gap_disconnect(connhandle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); // connhandle= 0 and not BLE_CONN_HANDLE_INVALID and it returns BLE_ERROR_INVALID_CONN_HANDLE, where the my variable where I track the connection still says I am connected

3) How can I know which is the connhandle for the question above? I tried a loop putting all different connhandles from 0x0000 to 0xFFFF and still in each returns BLE_ERROR_INVALID_CONN_HANDLE, that means I was already disconnected??

3) After receiving a BLE_ERROR_INVALID_CONN_HANDLE  how can I get the proper connhandle for that "connection"? because it is not being reported at on_ble_evt (the last received was connhandle = 0)

4) after calling sd_ble_gap_disconnect() regarding which conn handle it uses and if it returns NRF_SUCCESS, will the connhande changed = BLE_CONN_HANDLE_INVALID ?

5) is there any mechanism to know the connection status in the SDK13?

details

SDK 13.3

Role Central

  • Hello,

    Where do you get the parameter "connhandle" from?

    If this returns BLE_ERROR_INVALID_CONN_HANDLE it means that this conn_handle is not currently connected. Perhaps it has disconnected from another function call as well? Or that the peripheral has just disconnected by its own?

    After this error is received, if you choose not to pass it onto an APP_ERROR_CHECK(err_code); do you get a BLE_GAP_EVT_DISCONNECTED event just after this perhaps?

     

    4) after calling sd_ble_gap_disconnect() regarding which conn handle it uses and if it returns NRF_SUCCESS, will the connhande changed = BLE_CONN_HANDLE_INVALID ?

     Not by itself. This will be handled by the application. You can see how this is typically handled in the BLE_GAP_EVT_DISCONNECTED event in the main.c file's ble evt handler in most of our examples from the SDK.

    There is not a common conn_handle variable for all the files, so this depends on your application.

    Best regards,

    Edvin

  • Hi Edvin,

    my connhandle comes from  on_ble_evt(ble_evt_t * p_ble_evt) in main.c  where every this function pass an event the connhandle is updated. (I used this variable just in the main.c)

    I am not receiving any BLE_GAP_EVT_DISCONNECTED  when BLE_ERROR_INVALID_CONN_HANDLE  I am skipping the APP_ERROR_CHECK(err_code);

  • Is there any way for me to reproduce what you are seeing on a DK?

  • I could give a try disabling some sensors because I am testing it in a custom board using a Rigado Module (which I believe are not related to the issue), I let you know later if I am able to replicate it using just a DK

    #update I couldn't replicate the issue when on the DK disabling sensors used in the devboard.

    seems it is pointing to how I manage my sensors, they are I2C/TWI I read them using a scheduler and to be used only when there is free times slots and the device is not connected to any peripheral. digging a little bit

    I noticed enabling nrf_log I got the following when connected:

    Connected

    nrf_ble_gatt:ERROR:sd_ble_gattc_exchange_mtu_request() returned unexpected value 0x3002.
00> nrf_ble_gatt:ERROR:sd_ble_gap_data_length_update() (request) on connection 0x0 returned unexpected value 0x3002.

    TWISensor: QueueEvent device is connected skipping measurement

    TWISensor: QueueEvent device is connected skipping measurement

    TWISensor: QueueEvent device is connected skipping measurement

    TWISensor: QueueEvent device is connected skipping measurement

    TWISensor: QueueEvent device is connected skipping measurement

    TWISensor: QueueEvent device is connected skipping measurement

    then there I got BLE_ERROR_INVALID_CONN_HANDLE  sd_ble_gap_disconnect() is called because since the connection started It is not receiving any package

  • it is strange, because to read the TWI sensors, I am using app_timer then it creates an interruption every second and puts it in a queue then exit.

    When the queue event fires (I understand it is when there are time slots available), I check first If the device is connected or not, if connected skip the sensor read, otherwise the device is not connected and if it is time to read (e.g 10seconds) then proceed with TWI.

    my logs show TWI reads are not being called because the device is connected which is OK, but the connection for some reason is taking longer, then my own routine to handle connection timeouts (times, where I am not receiving anything) it calls sd_ble_gap_disconnect() 

    in this behavior, I am getting BLE_ERROR_INVALID_CONN_HANDLE

    maybe something related to the queue size? currently, it is 3, and in this time I am adding more often to the scheduler queue, I increased the queue size. testing it 

Related