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

Sample code to add a Custom BLE Service on nRF82540DK using nRF Connect SDK

Hi nRF support team,

I am trying to add a new Custom Service and Characteristic an application on nRF82540 development kit using the nRF Connect SDK.

Can you please let me know the instructions on how to add a custom service and characteristic? It would be great if you can share the sample code to create a new custom BLE service.

Regards,

Kalyan

  • Hello,

    I assume you refer to the nRF52840, just to clear any misunderstandings Slight smile
    We don't have much other than the examples in the folder NCS\nrf\samples\bluetooth

    I suggest you check out the example peripheral_uart. This is an example that uses a custom BLE service (custom meaning that this service is not a standardized BLE service).



    In main.c you can see that the nus.h file is included:

    #include <bluetooth/services/nus.h>
    This file is located in NCS\nrf\include\bluetooth\services\nus.h
    The complimentary .c file is nus.c in NCS\nrf\subsys\bluetooth\services\nus.c

    The "main thread" in this example is the "led_blink_thread". There you can see the
    err = bt_enable(NULL);
    which enables the BLE stack, and:
    err = bt_gatt_nus_init(&nus_cb);
    which initializes the nus (Nordic UART Service), which is the custom BLE service in this example.

    Actually, it is not entirely true that this function intializes this service, but you can look at the nuc.c/h to see how to set up a service. It is the macros in nuc.c:
    /* UART Service Declaration */
    BT_GATT_SERVICE_DEFINE(nus_svc,
    BT_GATT_PRIMARY_SERVICE(BT_UUID_NUS_SERVICE),
    	BT_GATT_CHARACTERISTIC(BT_UUID_NUS_TX,
    			       BT_GATT_CHRC_NOTIFY,
    			       BT_GATT_PERM_READ,
    			       NULL, NULL, NULL),
    	BT_GATT_CCC(NULL, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
    	BT_GATT_CHARACTERISTIC(BT_UUID_NUS_RX,
    			       BT_GATT_CHRC_WRITE |
    			       BT_GATT_CHRC_WRITE_WITHOUT_RESP,
    			       BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
    			       NULL, on_receive, NULL),
    );
    Use this as a starting point, and let me know if you get stuck.
    Best regards,
    Edvin
  • Hi Edvin,

    Thanks for the response and instructions to add new Custom Service.

    I have implemented to added a new custom service under folder path ncs\zephyr\subsys\bluetooth\services and created custom.c file similar as bas.c and header file custom.h at ncs\zephyr\include\bluetooth\services. I am able to verify the custom gatt service in the light blue app.

    Code looks like as you mentioned above macros. Thanks for the instructions.

    Regards,

    Kalyan

  • Hello,
    I followed your tutorial for create a new service by copy your source to flask on my board, but it's not received device name when I use NRF connect. Please help me solution. 
    https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/lessons/lesson-4-bluetooth-le-data-exchange/topic/blefund-lesson-4-exercise-1/
    github.com/.../main.c

Related