Cannot reconfigure BLE peripheral UART example to send data I have stored in buffer please help

Hello I have seen ALOT of posts referencing to first build and run ble peripheral uart example and then from there that it will be easy to remove the uart from the functionality if so desired but I cannot seem to get this to work.

I have tried many different approaches over the past few weeks and no matter what I do I cannot get an app up and running that allows me to use my PCB as peripheral that automatically sends a buffer of data (a bunch of SSIDs concatenated) on connection of central device (NRF Connect app). Is there please a snippet of code you can provide me that shows how to go from using the uart to having my own custom service that just sends a buffered message over ble?

Parents
  • Hi there,

    Are you using nRF Connect SDK or the nRF5SDK and which version are you using?

    regards

    Jared 

  • HI Jared our project is based on using the UART BLE peripheral example in NRF5SDK SDK version 17.1. Thank you for your quick response.

  • I actually just managed to get ble_nus_data_send() workign yesterday afternoon, kind of, by employing:

    void uart_event_handle(app_uart_evt_t * p_event)
    {
        static uint8_t index = 0;
        uint32_t       err_code;

        switch (p_event->evt_type)
        {
            case APP_UART_DATA_READY:
                UNUSED_VARIABLE(app_uart_get(&txBuff[index]));
                index++;

                   if ((txBuff[index - 1] == '\n') ||
                        (txBuff[index - 1] == '\r'))
                    {
                        if (index > 1)
                        {
                            NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                            NRF_LOG_HEXDUMP_DEBUG(txBuff, index);

                            do
                            {
                                uint16_t length = (uint16_t)index;
                                err_code = ble_nus_data_send(&m_nus, txBuff, &length, 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);
                        }

                    }

                break;

    and

    static void idle_state_handle(void)
    {
        app_sched_execute();

        cli_process();

        (void)NRF_LOG_PROCESS();

       if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
       {
          uart_event_handle(APP_UART_DATA_READY);
       }
    }

    but now I am running into a new problem, the data I need to send is around 256 bytes, in length and anytime I increase my MTU to more than the default:

    /** @brief Default ATT MTU, in bytes. */
    #define BLE_GATT_ATT_MTU_DEFAULT          23

    I get an asserition failure.

    Is there a way to repeatedly call data in a loop ble nus data send () and step trhough my 256 byte string in 20 byte snippets?

    Thank you

  • mjconnor57 said:
    Is there a way to repeatedly call data in a loop ble nus data send () and step trhough my 256 byte string in 20 byte snippets?

    Yes, just call ble_nus_data_send() in a for loop where you increment the array parameter 20 byte for each call until you've sent the data. 

  • I know this is more of a C question than  Nordic, I apologize for that but is there a chance you can give me pseudo code example of how this might be done? I am having a hard time reconciling stepping through the index for parsing the message and moving my message queue window.

    Thank you for your help 

  • Hmm,

    Actually on a second thought, looking at how ble_nus_data_send() is implemented, I think it would better to divide the buffer that you're going to send into 20 byte arrays first, and then after that pass the 20 byte array to ble_nus_data_send(). So you fill the data buffer with 20 bytes, when it has reached 20 bytes you call ble_nus_data_send() and then restart the process.

    regards

    Jared 

  • Ok I will give that a try and see if I can implement it, thank you for the follow up

Reply Children
  • I am able to split the messages into 20 byte chunks with a small function but oddly, my code execution stays at the first:

                  err_code = ble_nus_data_send(&m_nus, data_array1, &length, m_conn_handle); call

    Despite the data_array1 appearing at NRFConnect on my Android... should I be looking for confirmation

  • I have stripped ble_nus_data_send() completely down to this for sending 8 20 byte packets:

        case APP_UART_DATA_READY:

                NRF_LOG_DEBUG("Ready to send data over BLE NUS");

                  ble_nus_data_send(&m_nus, data_array1, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array2, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array3, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array4, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array5, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array6, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array7, &packetSize, m_conn_handle);
                  ble_nus_data_send(&m_nus, data_array8, &packetSize, m_conn_handle);
                  break;

    But oddly, execution just keeps cycling through the sends and does not break after the 8th, does that make sense?

  • HI Matt,

    You mean that the program "spins" in  the APP_UART_DATA_READY switch case? Are you sending a lot off data? Maybe the APP_UAR_DATA_READY event is just generated multiple times because you're trying to send much data at a time.

    regards

    Jared 

  • Yes that is what it is doing, but I am only sending 20 bytes per nus_data_send() per your earlier instruction, perhaps it is my invocation?

    static void idle_state_handle(void)
    {
        static int index = 1;
        app_sched_execute();

        cli_process();

        (void)NRF_LOG_PROCESS();

       if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
       {
          uart_event_handle(APP_UART_DATA_READY);
       }

    }

  • Hi,

    Can you share your main.c file?

    regards

    Jared

Related