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

Using BLE NUS SERVICE without UART

Hello

I would like to send my sensor data to a mobile application using BLE SDK 15. Someone suggested me to go through the BLE NUS SERVICE example which was a good one. I understood a lot of things but now my sensor values are coming on analog pins and not on UART pins. How do i send those values? Would i have to create a whole new service for doing this or could i somehow modify the same example to work according to my needs. Thanks!  

Parents
  • err_code = 0x05 is NRF_ERROR_NOT_FOUND.

    This means that the ble_nus_data_send() receives an invalid conn_handle.

     

    What conn_handle do you insert?

    in ble_nus_data_send()?

    from the ble_app_uart example:

    err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);

     

    Do you get this return value before or after you connect with the phone?

     

    Best regards,

    Edvin

  • Hello Edvin,

    That sounds about right. Actually, i do not understand the concept of connection handle, so i passed the same "m_conn_handle" in my ble_nus_data_send() function. The error comes before connection itself. Can you tell me a little about the connection handle ? or a link where i can learn bout it. Thanks,

    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        uart_init();
        log_init();
        timers_init();
        buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        printf("\r\nUART started.\r\n");
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            uint8_t str[4];
    
          sprintf((char*)str, "%d", (int)adc_sample);
    
          ret_code_t err_code =  ble_nus_data_send(&m_nus, str, strlen((char*)str), m_conn_handle);
          APP_ERROR_CHECK(err_code);
            //idle_state_handle();
        }
    }

  • Hello,

    The explanation is using the unmodified ble_app_uart example:

    The connection handle is, during the startup of the ble_app_uart example, set to BLE_CONN_HANDLE_INVALID (= 0xFFFF).

    In the BLE_GAP_EVT_CONNECTED event in ble_evt_handler in main.c, the conn_handle, m_conn_handle is set to the conn_handle of the connection event, and it remains unchanged until the disconnected-event.

     

    If you look in the same example, in the uart_event_handle() in main.c, which is the only place using the ble_nus_data_send() function, you can see that it ignores returns from ble_nus_data_send if the err_code = NRF_ERROR_INVALID_STATE, NRF_ERROR_BUSY or NRF_ERROR_NOT_FOUND.

     

    It only means that it will not try to send anything if there is no device to send to.

    So you can ignore this error:

    err_code = ble_nus_data_send(...)
    if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))
    {
        APP_ERROR_CHECK(err_code);
    }

     

    However, you can use these return values to tell your application that the message is not sent, if you need it. The fact that ble_nus_data_send() returns any of these values will not break anything, other than telling you that the message was not sent.

     

    Alternatively, you can check the conn_handle before you try to send:

    if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        err_code = ble_nus_data_send(...);
        if (err_code != NRF_ERROR_INVALID_STATE && err_code != NRF_ERROR_BUSY)
        {
            APP_ERROR_CHECK(err_code);
        }
    }

     

    Best regards,

    Edvin

     

Reply
  • Hello,

    The explanation is using the unmodified ble_app_uart example:

    The connection handle is, during the startup of the ble_app_uart example, set to BLE_CONN_HANDLE_INVALID (= 0xFFFF).

    In the BLE_GAP_EVT_CONNECTED event in ble_evt_handler in main.c, the conn_handle, m_conn_handle is set to the conn_handle of the connection event, and it remains unchanged until the disconnected-event.

     

    If you look in the same example, in the uart_event_handle() in main.c, which is the only place using the ble_nus_data_send() function, you can see that it ignores returns from ble_nus_data_send if the err_code = NRF_ERROR_INVALID_STATE, NRF_ERROR_BUSY or NRF_ERROR_NOT_FOUND.

     

    It only means that it will not try to send anything if there is no device to send to.

    So you can ignore this error:

    err_code = ble_nus_data_send(...)
    if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) && (err_code != NRF_ERROR_NOT_FOUND))
    {
        APP_ERROR_CHECK(err_code);
    }

     

    However, you can use these return values to tell your application that the message is not sent, if you need it. The fact that ble_nus_data_send() returns any of these values will not break anything, other than telling you that the message was not sent.

     

    Alternatively, you can check the conn_handle before you try to send:

    if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        err_code = ble_nus_data_send(...);
        if (err_code != NRF_ERROR_INVALID_STATE && err_code != NRF_ERROR_BUSY)
        {
            APP_ERROR_CHECK(err_code);
        }
    }

     

    Best regards,

    Edvin

     

Children
Related