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

Data transmitted twice and BLE_NUS_EVT_TX_RDY event received twice

Hi,

I'm using a custom board with nrf52840 and sdk 15.1.0.

I have altered the ble central and peripheral example(ble_app_hrs_rscs_relay) to support nus central and peripheral. I'm able to send and receive data from both peripheral and central connections. 

But when I send data through a peripheral connection, the BLE_NUS_EVT_TX_RDY event in "nus_data_handler" is triggered twice and the data is send twice even though I'm calling "ble_nus_data_send" only once.

else if(p_evt->type == BLE_NUS_EVT_TX_RDY)
{
NRF_LOG_DEBUG("Tx Completed %d ", ++complete_count);
}

LOG Output is:

Tx Completed 1

Tx Completed 2

when I try to send the data repeatedly in 20 second interval, each time its transmitted twice.

Any suggestions on this will be of great help.

  • The only logical explanation is that you are sending it twice. I know you think you aren't, however you must be. 

  • Yes, I know it looks illogical. But I couldn't find the source of the problem. Or can any other function apart from  "ble_nus_data_send" trigger this event . I have placed a counter inside "ble_nus_data_send" and it increments only once. 

    
    uint32_t ble_nus_data_send(ble_nus_t * p_nus,
                               uint8_t   * p_data,
                               uint16_t  * p_length,
                               uint16_t    conn_handle)
    {
        ret_code_t                 err_code;
        ble_gatts_hvx_params_t     hvx_params;
        ble_nus_client_context_t * p_client;
        
        static uint16_t send_count = 0;
        
    
        VERIFY_PARAM_NOT_NULL(p_nus);
    
        NRF_LOG_DEBUG("Sent Count %d", ++send_count);           // This counter increments only ONCE
        
        err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage, conn_handle, (void *) &p_client);
        VERIFY_SUCCESS(err_code);
    
        if ((conn_handle == BLE_CONN_HANDLE_INVALID) || (p_client == NULL))
        {
            return NRF_ERROR_NOT_FOUND;
        }
    
        if (!p_client->is_notification_enabled)
        {
            return NRF_ERROR_INVALID_STATE;
        }
    
        if (*p_length > BLE_NUS_MAX_DATA_LEN)
        {
            return NRF_ERROR_INVALID_PARAM;
        }
    
        memset(&hvx_params, 0, sizeof(hvx_params));
    
        hvx_params.handle = p_nus->tx_handles.value_handle;
        hvx_params.p_data = p_data;
        hvx_params.p_len  = p_length;
        hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
    
        return sd_ble_gatts_hvx(conn_handle, &hvx_params);
    }
    
    
    // nus_data_handler event
    
        else if(p_evt->type == BLE_NUS_EVT_TX_RDY)
        {
           NRF_LOG_DEBUG("Tx Completed %x. ", ++complete_count);    // This counter increases twice
        }
    

    Thanks

  • I see. I have seen something similar in another custom project (I can't send the project files from that project). The issue was related to how the connection handles are handled in the main.c file and in ble_nus_c.c when trying to add the Nordic UART Service to the multilink example.

    That is, the changes in ble_nus_c.c was only related to that the BLE_NUS_EVT_TX_RDY gives a conn_handle that is not correct. This is because the conn_handle is not set in the on_hvx() function in ble_nus_c.c

    Add:

    ble_nus_c_evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;

    before

    p_ble_nus_c->evt_handler(p_ble_nus_c, &ble_nus_c_evt);

    So, regarding the events showing up two times:

    I really can't say anything other than that you have to do it exactly (!) as the ble_app_hrs_rscs_relay example. Everything that is related to conn_handles has to be used the same way as in the ble_app_multilink_central. Change all functions with lbs_c with the corresponding nus_c function calls. 

    Give it a go when you start from the ble_app_hrs_rscs_relay project, and replace all hrs functions with nus functions. Does it still happen then?

    Best regards,

    Edvin

  • Edvin,

    Have tried adding ble_nus_c_evt.conn_handle = p_ble_evt->evt.gap_evt.conn_handle but no luck..

    I haven't checked my project with ble_app_multilink_central. Will compare with that and let you know what happens. But I think its a problem on Ble peripheral and not on central. I do feel, have to double check on the connection-handle. Will update on that.

    Thanks.

  • Hello,

    I don't think you need to compare it too much with the ble_app_multilink_central example. What I really meant was that I have seen something similar with this example, when someone else tried to implement the ble_nus_c service to that example.

    I believe you if you say that you only send once from the peripheral, I believe the issue might be in the central's conn_handle. If you want to, you can send a project that can reproduce this, and I can see if I can figure out the reason for this.

    Best regards,

    Edvin

Related