Want to send data to phone over BLE UART every 500ms. ble_nus_data_send() is giving me problems.

Hi,

I am writing a program that receives a command from a phone using the BLE UART example. The program can count up or down depending on the command. On every count, I want to send the number to the phone, then wait 500 ms before sending the next one. I am using the function ble_nus_data_send() to send the data to the phone. I can send text like "counting up", but if I try to send a number, my phone displays a lot of strange, unreadable text. Also, when I use the app nRF Toolbox on my phone, I can only receive 4 messages from the dev board, then it just stops. My code is shown below.

static void nus_data_handler(ble_nus_evt_t * p_evt)
{
    NRF_LOG_DEBUG("NUS data handler.");

    uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    uint8_t b[BLE_NUS_MAX_DATA_LEN];

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

        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++)
        {
            //b[i] = app_uart_get(&data_array[i]);
            b[i] = app_uart_get(&data_array[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);
        }
        if((memcmp(p_evt->params.rx_data.p_data,"0x01", p_evt->params.rx_data.length) == 0))
        {
            NRF_LOG_DEBUG("Sending response.");
            char a[BLE_NUS_MAX_DATA_LEN] = "Counting up.";
            char c[BLE_NUS_MAX_DATA_LEN];
            int length = BLE_NUS_MAX_DATA_LEN;

            ble_nus_data_send(&m_nus, a, &length, m_conn_handle);
            
            for(int i = UP_LOW_LIM; i <= UP_HIGH_LIM; ++i)
            {
                //delay(DELAY_TIME);
                nrf_delay_ms(DELAY_TIME);
                c[0] = i;
                ble_nus_data_send(&m_nus, c, &length, m_conn_handle);
            }

        }else if((memcmp(p_evt->params.rx_data.p_data,"0x02", p_evt->params.rx_data.length) == 0))
        {
            NRF_LOG_DEBUG("Sending response.");
            char a[BLE_NUS_MAX_DATA_LEN] = "Counting down.";
            int length = BLE_NUS_MAX_DATA_LEN;

            ble_nus_data_send(&m_nus, a, &length, m_conn_handle);
            
            for(uint8_t i = DN_HIGH_LIM; i <= DN_LOW_LIM; --i)
            {
                //delay(DELAY_TIME);
                nrf_delay_ms(DELAY_TIME);
                ble_nus_data_send(&m_nus, (char *) i, &length, m_conn_handle);
            }

        }

    }

}

Related