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

Sending byte array over ble on nrf52840

Hello,

I am in the process trying to send multiple packets over ble but cannot seems to figure out this UART example.

I have read many posts that use `ble_nus_string_send` to send string data, however, I can't seem to find this function located anywhere.

I currently can only send one byte at a time.

I started on the ble_blinky example as I needed some buttons so I don't know exactly what I would be missing.

This is a little test hello world on button click for button 0 in main.c:

static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    ret_code_t err_code;

    uint8_t data_value[] = {"HelloWorld!"};
    uint16_t len = sizeof(data_value);

    SEGGER_RTT_printf(0, "Length = %d\n", len);

    switch (pin_no)
    {
        case BUTTON0_BUTTON:
            SEGGER_RTT_WriteString(0, "Send button state change.\n");
            err_code = ble_btn_on_button0_change(m_conn_handle, &m_btn, data_value, len);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
        case BUTTON1_BUTTON:
            SEGGER_RTT_WriteString(0, "Send button state change.\n");
            err_code = ble_btn_on_button1_change(m_conn_handle, &m_btn, button_action);
            if (err_code != NRF_SUCCESS &&
                err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                err_code != NRF_ERROR_INVALID_STATE &&
                err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
            {
                APP_ERROR_CHECK(err_code);
            }
            break;
        default:
            APP_ERROR_HANDLER(pin_no);
            break;
    }
}

button_service.c:

uint32_t ble_btn_on_button0_change(uint16_t conn_handle, ble_btn_t * p_btn, uint8_t * button_state, uint16_t len)
{
    ble_gatts_hvx_params_t params;

    memset(&params, 0, sizeof(params));
    params.type   = BLE_GATT_HVX_NOTIFICATION;
    params.handle = p_btn->button0_char_handles.value_handle;
    params.p_data = button_state;
    params.p_len  = len;

    return sd_ble_gatts_hvx(conn_handle, &params);
}

I don't know if there is some external setting that I need to change to send more than one byte. I know this to be the case in some areas as it took me longer than I would like to admit to figure out that `app_button_enable` is required to enable buttons.

If I am totally off base please point me in the right direction.

Parents
  • You're so close.  If you'd have searched for ble_nus_c_string_send() you'd have found it:  There's a ble_nus_c_string_send() in the ble_app_uart_c client example.   But note that you might be better off inspecting the ble_app_uart server example and dropping the NUS server into your button server project. The equivalent server project uses ble_nus_data_send() and can send about 247 bytes at a time.  By dropping into I mean settomg BLE_NUS_ENABLED to 1 in sdk_config.h, adding ble_nus.c service to the project along with other source files that you'll find in the ble_app_uart project that aren't in blinky along with the corresponding init calls..

  • There's a ble_nus_c_string_send() in the ble_app_uart_c client example

    IIRC, the name is misleading:

    In 'C', a string is taken to mean an array of characters terminated by NUL.

    The ble_nus_c_string_send() function it is not a string send - it just sends a buffer of entirely arbitrary bytes, and takes a second parameter to specify the length.

Reply Children
No Data
Related