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

nRF52840 - SDK16 - BLE Echo

Hi I tried modifying the ble_app_uart_pca10056_s140 example as follows (nus_data_handler method modified only) such that it echoes the data back sent from the phone:

static void nus_data_handler(ble_nus_evt_t * p_evt)
{

    if (p_evt->type == BLE_NUS_EVT_RX_DATA)
    {
        uint32_t err_code;

        if ((memcmp(p_evt->params.rx_data.p_data, "ECHO\n", p_evt->params.rx_data.length) == 0)) {
          uint8_t example[5] = "ECHO\n";
          uint16_t length_example = 0;
          uint32_t err_code;

          do {
            err_code = ble_nus_data_send(&m_nus, example, &length_example, m_conn_handle);

            if ((err_code != NRF_ERROR_INVALID_STATE) &&
                (err_code != NRF_ERROR_RESOURCES) &&
                (err_code != NRF_ERROR_NOT_FOUND)) {
              APP_ERROR_CHECK(err_code);
            }
          } while (err_code == NRF_ERROR_RESOURCES);
        }

        NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
        NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

        for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
        {
            do
            {
                err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
                if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
                {
                    NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
                    APP_ERROR_CHECK(err_code);
                }
            } while (err_code == NRF_ERROR_BUSY);
        }
        if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
        {
            while (app_uart_put('\n') == NRF_ERROR_BUSY);
        }
    }

}

I expect the nRF52840 to echo back the example array which currently contains "ECHO\n" when I send the word ECHO to the nRF52840. The word ECHO is being printed on the UART as the basic example does but the nRF52840 is not sending data back to the phone. I am using the ble_nus_data_send method used in the uart handler method.

The ble central example uses this method then ble_nus_c_string_send - what is the difference when compared to ble_nus_data_send?

Perhaps it is wrong to echo data back in the event handler?

Related