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

Difference in adding characteristics from example codes

Dear community,

I am trying to implement a custom service, however I am struggling because many example codes are for PCA10040, Softdevice S132 and/or for older SDK's. I've basically tried to implement it based on:

  • the custom BLE service example (PCA10040, S132) from GitHub
  • the predefined services like BAS and LLS from the proximity example (PCA10056, S140), on which my code is based on
  • the ble_app_template example from SDK 17.0.2.

So I've tried to work out the differences between those three examples and now I am wondering what difference is between those three code snippets (from the (...)_char_add functions of those three example codes).

ble_add_char_params_t char_md;

memset(&char_md, 0, sizeof(char_md));

char_md.char_props.read   = 1;
char_md.char_props.write  = 1;
char_md.char_props.notify = 1; 
char_md.p_char_user_desc  = NULL;
char_md.p_char_pf         = NULL;
char_md.p_user_desc_md    = NULL;
char_md.p_cccd_md         = &cccd_md; 
char_md.p_sccd_md         = NULL;

ble_gatts_attr_md_t attr_md;

memset(&attr_md, 0, sizeof(attr_md));

attr_md.read_perm  = p_sds_init->custom_value_char_attr_md.read_perm;
attr_md.write_perm = p_sds_init->custom_value_char_attr_md.write_perm;
attr_md.vloc       = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth    = 0;
attr_md.wr_auth    = 0;
attr_md.vlen       = 0;

ble_gatts_attr_t    attr_char_value;

memset(&attr_char_value, 0, sizeof(attr_char_value));

attr_char_value.p_uuid    = &ble_uuid;
attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len  = sizeof(uint8_t);
attr_char_value.init_offs = 0;
attr_char_value.max_len   = sizeof(uint8_t);

Also, the characteristics and attributes are often added with different functions, such as:

err_code = sd_ble_gatts_characteristic_add(p_sds->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_sds->custom_value_handles);

or in case of the predefined services BAS and LLS:

return characteristic_add(p_lls->service_handle,
                              &add_char_params,
                              &p_lls->alert_level_handles);

I basically want to add three services with multiple characteristics that are all read/write. What I already achieved is to read and write the LLS characteristic with a smartphone app, so it would be most convenient for me to use more or less the kind of characteristics that are added there.

Can someone explain to me what the difference is between those code snippets? And maybe a recommendation on which of that I should use? Or rather how I can implement these read/write characteristics?

Kind regards.

  • Hi

    In general I would recommend using either the Nordic UART service (NUS) or the Led Button service (LBS) from SDK v17.0.2 as a starting point if you want to make your own proprietary service(s). 

    Both of these services are quite similar in that they set up one characteristic for sending data from the client to the server (write), and one characteristic for sending data from the server to the client (notify). The only difference is that LBS is set up to send a single byte back and forth, while NUS is set up to send strings of varying lengths, making it more suitable if you want to send a large amount of data. 

    To modify one of these services to your own requirements you can simply copy and rename the existing files, replace the UUID with one of your own, and change the implementation as needed to add or remove characteristics or change the characteristic properties. 

    As for the differences between the examples, the various service SDK implementations vary a bit in how they configure and set up the characteristics, but in the end it all boils down to a series of calls to the sd_ble_gatts_service_add(..) and sd_ble_gatts_characteristic_add(..) functions. 

    Best regards
    Torbjørn

  • Hi Torbjørn,

    I successfully implemented my own GATT profile based on the Nordic UART service as you suggested. It was fast and easy.

    Thank you for your great support!

  • Excellent news Alexander, good to hear you were able to get it running Slight smile

    I will consider the case closed then. 

Related