Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Converting BLE_peripheral UART example to support 4 peripheral connections

Hi All,

I modified ble_peripheral UART example to support 4 peripheral links instead of one by referring the bel_app_multiperipheral example, I am able to connect four mobiles(central devices) to it and sending (from central devices ) some strings successfully to peripheral.

now question is how to replay from peripheral to all central devices? actually I tried with below function to send the strings from peripheral to central devices

void vfnSendSensorResponsOnBle(uint8_t *ptr, uint32_t len)
{
         uint8_t uIndex = 0;
         uint32_t err_code = 0;
    

         for (uint32_t i = 0; i < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT; i++)
         {
              if (m_qwr[i].conn_handle != BLE_CONN_HANDLE_INVALID)
              {
                     NRF_LOG_DEBUG("Ready to send data over BLE NUS");

                    do
                   {
                        if(len>20)
                           len=19;


                       err_code = ble_nus_data_send(&m_nus, ptr, (uint16_t *)&len, m_qwr[i].conn_handle/*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);
              }
         }
}

with the above function able to send strings from peripheral to central when one central device is connected. when I tried the same with more than once central connections, peripheral device getting restarted.

Here I am not getting what causing for it, I think  need to modify above function. 

Please let me know what are the things need to be change or any example for reference other than  bel_app_multiperipheral.

Thanks in Advance

Regards,

Sudheer 

  • Hi there, I am taking over from Susheel as he is not available. I checked your information and assume that you are working with a nRF5 SDK version where ble_nus.c supports this function: 

    ble_nus_data_send(ble_nus_t * p_nus, uint8_t * p_data, uint16_t * p_length, uint16_t conn_handle)

    That is because that with such versions, the ble_nus module should support multiple connections out of the box (unlike earlier versions where you do need to modify it).

    And your code seems to be correct. I have also successfully sent text to two central devices using a very similar implementation I will post below.

    While implementing that from a fresh example, I also notice that, depends on implementation, UART lib, NUS and multiple link support can eat up a lot of memory. Your description of "peripheral device getting restarted" is a symptom that the device might have run out of RAM. Or it could be that some pointers are not being handled correctly. Please also check if you are building with "Release" configuration. In default "Release" build config, the device would just reset at any failed APP_ERROR_CHECK.

    Could you please configure log level to DEBUG in sdk_config.h and debug some more with RTT logs? You might want to try both Debug and Release build configurations to see how it changes things.

    If you need support with that, please produce a clean log of the peripheral application, in Debug build configuration, failing and restarting and reply here.

    Finally, this is my current implementation which successfully sends to two centrals:

    void dummy_nus_send(void)
    {
        uint32_t       err_code;
        static uint8_t data_array[] = "dummy\r\n";
        for (uint32_t i = 0; i < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT; i++)
        {
            if (m_qwr[i].conn_handle != BLE_CONN_HANDLE_INVALID)
            {
                NRF_LOG_DEBUG("Ready to send data over BLE NUS Conn %d of %d, handle %x", i, NRF_SDH_BLE_PERIPHERAL_LINK_COUNT, m_qwr[i].conn_handle);
                do
                {
                    uint16_t length = (uint16_t) sizeof(data_array);
                    err_code = ble_nus_data_send(&m_nus, data_array, &length, m_qwr[i].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);
                
            }
        }
    }

  • Hi,

    Thanks for reply!

    I am able to share strings up to four central devices , but I am facing challenge with string size. 

    Unable to send more than 20 characters because of NRF_SDH_BLE_GATT_MAX_MTU_SIZE 23, is there any other option to send up to 128 or 256 characters long Over BLE  

  • That's good to hear. If you don't mind, please share what the problem is and verify it as the answer, so that future developers can refer from it.

    Sure, you can increase MTU size by updating that exact config. You can refer to the UART/Serial Port Emulation over BLE (or ble_app_uart) example for a working configuration. You could also discover DevZone for more information on how-to, common issues, and more details.

    If you need further help with that, please open a new ticket, as that would be a different topic from what we are discussing here.

Related