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

Purpose of on_connect function in ble_nus.c?

Hello, I am studying the peripheral/ble_app_uart and peripheral/ble_app/template examples to understand the proper way to build a peripheral with our own set of services and characteristics.  I'm also studying this tutorial:

https://devzone.nordicsemi.com/nordic/short-range-guides/b/bluetooth-low-energy/posts/ble-services-a-beginners-tutorial

One thing I don't understand is the purpose of the on_connect function in ble_nus.  It says that it is checking 'the hosts CCCD value to inform of readiness to send data using the RX characteristic' and then it is performing a sd_ble_gatts_value_get.  I don't really understand the purpose of these two things, and it's not explained in the tutorial.  Do I need to do something like this when setting up my own characteristics?  

Thanks!

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the SoftDevice.
*
* @param[in] p_nus Nordic UART Service structure.
* @param[in] p_ble_evt Pointer to the event received from BLE stack.
*/
static void on_connect(ble_nus_t * p_nus, ble_evt_t const * p_ble_evt)
{
ret_code_t err_code;
ble_nus_evt_t evt;
ble_gatts_value_t gatts_val;
uint8_t cccd_value[2];
ble_nus_client_context_t * p_client = NULL;
err_code = blcm_link_ctx_get(p_nus->p_link_ctx_storage,
p_ble_evt->evt.gap_evt.conn_handle,
(void *) &p_client);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
p_ble_evt->evt.gap_evt.conn_handle);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Parents
  • Hi

    The on_connect function is there to handle the BLE_GAP_EVT_CONNECTED event from the SoftDevice when the ble_nus_on_ble_evt() function is called. When using other services than NUS, this function is called from ble_conn_params.c, so it should be handled by the SDK when creating your own characteristics as well.

    You can check out the ble_custom_service example over at our GitHub for a more complete tutorial on how to set up custom services and characteristics here.

    Best regards,

    Simon

Reply
  • Hi

    The on_connect function is there to handle the BLE_GAP_EVT_CONNECTED event from the SoftDevice when the ble_nus_on_ble_evt() function is called. When using other services than NUS, this function is called from ble_conn_params.c, so it should be handled by the SDK when creating your own characteristics as well.

    You can check out the ble_custom_service example over at our GitHub for a more complete tutorial on how to set up custom services and characteristics here.

    Best regards,

    Simon

Children