Initializing Services


Bluetooth data handler

/**@brief Function for handling the data from the Nordic UART Service.
 *
 * @details This function will process the data received from the Nordic UART BLE Service and send
 *          it to the UART module.
 *
 * @param[in] p_nus    Nordic UART Service structure.
 * @param[in] p_data   Data to be send to UART module.
 * @param[in] length   Length of the data.
 */
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
    for (uint32_t i = 0; i < length; i++)
    {
        while (app_uart_put(p_data[i]) != NRF_SUCCESS); // Place received data to the UART module one character at a time
    }
    while (app_uart_put('\r') != NRF_SUCCESS);
    while (app_uart_put('\n') != NRF_SUCCESS);
}


The following function declares static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) function shown above as the bluetooth data handler. This function will be called to print received data to a serial terminal ( while (app_uart_put(p_data[i]) != NRF_SUCCESS);)

/**@snippet [Handling the data received over BLE] */
      		    		      							 		     					
/**@brief Function for initializing services that will be used by the application.
 */
static void services_init(void)
{
    uint32_t       err_code;
    ble_nus_init_t nus_init;			//Nordic UART Service initialization structure

    memset(&nus_init, 0, sizeof(nus_init));	//Clear nus_init 

    nus_init.data_handler = nus_data_handler;	//Event handler to be called for handling received data from BLE link

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