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

Multiperipheral NUS conn_handle issue

Dear Sir/Madam

I am using multiperipheral example. I can connect to multiple phones. I added NUS example in it. 

I am using package nRF5_SDK_15.2.0_9412b96.

I do not know how to assign connection handle to NUS when a device connects. I initialised NUS in services_Init().

static void services_init(void)

{

    uint32_t           err_code;

    ble_nus_init_t     nus_init;
    //ble_nus_init_t     nus_init[LINK_TOTAL];
    static uint8_t     m_ble_nus_c_count;  

    nrf_ble_qwr_init_t qwr_init = {0};


    // Initialize Queued Write Module.
    qwr_init.error_handler = nrf_qwr_error_handler;

    //for muliuple link
     for (uint32_t i = 0; i < LINK_TOTAL; i++)
    {
        err_code = nrf_ble_qwr_init(&m_qwr[i], &qwr_init);
        APP_ERROR_CHECK(err_code);
    }


    // Initialize NUS.

    memset(&nus_init, 0, sizeof(nus_init));
    nus_init.data_handler = nus_data_handler;

        err_code = ble_nus_init(&m_nus, &nus_init);
        APP_ERROR_CHECK(err_code);

}

When I get a data from phone, I can receive it in the code and display in terminal but I do not which connection handle is sending data as I do not know how to assign the handles to NUS and where should I do it. 

Connection handles are created when ble_evt_handler is triggered. 

                   printf("Connection with link 0x%x established.", p_ble_evt->evt.gap_evt.conn_handle);

            // Assign connection handle to available instance of QWR module.

            for (uint32_t i = 0; i < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT; i++)
            {
                
               // ble_nus_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;

                if (m_qwr[i].conn_handle == BLE_CONN_HANDLE_INVALID)
                {
                    err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr[i], p_ble_evt->evt.gap_evt.conn_handle);
                     APP_ERROR_CHECK(err_code);
                   
                    // err_code = ble_nus_c_handles_assign(p_ble_evt->evt.gap_evt.conn_handle);   //I thought I should do this, but it creates error
                    // APP_ERROR_CHECK(err_code);    
               
                    break;
                }
            }

When I receive data, I try to enquire its conn_handle but it was always empty.

Parents
  • Hi,

    I'm guessing that your nRF acts as the peripheral while the phones acts as centrals? I'm a bit unsure on this as the code from the qwr module that you shared is from the central code. If the nRF is going to be the peripheral then you should add the code from the peripheral ble_app_uart example not the central and vice versa. 

    Anyways:

    For printing out the connection handle if the nRF is a peripheral:

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
    
            NRF_LOG_INFO("Connection handle: %u",(unsigned int)p_evt->conn_handle);
    
    
    
        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++)
            {
                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);
            }
        }
    
    }

    note that the ble_app_uart uses the RTT backend for the logger module as the UART is already in use. 

    regards

    Jared 

Reply
  • Hi,

    I'm guessing that your nRF acts as the peripheral while the phones acts as centrals? I'm a bit unsure on this as the code from the qwr module that you shared is from the central code. If the nRF is going to be the peripheral then you should add the code from the peripheral ble_app_uart example not the central and vice versa. 

    Anyways:

    For printing out the connection handle if the nRF is a peripheral:

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {
    
            NRF_LOG_INFO("Connection handle: %u",(unsigned int)p_evt->conn_handle);
    
    
    
        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++)
            {
                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);
            }
        }
    
    }

    note that the ble_app_uart uses the RTT backend for the logger module as the UART is already in use. 

    regards

    Jared 

Children
No Data
Related