This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

NCS BLE NUS uninitialize function

Hello everyone,

I am working on an application where I am using the BLE Nordic UART service (NUS) with a changed service UUID. I need to be able to uninitialize the service after a while so it should only be available after startup for a certain time.

My thought was to enable CONFIG_BT_GATT_DYNAMIC_DB so the bt_gatt_service_register/unregister functions can be used.
Therefore a service object with type struct bt_gatt_service is needed (instead of struct bt_gatt_service_static), so I replaced this service declaration:

/* 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(nus_ccc_cfg_changed, 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),
);

with this code:

#define BT_NUS_SERVICE_DEFINITION { \
	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(nus_ccc_cfg_changed, 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), \
	}

static struct bt_gatt_attr attr_nus_svc[] = BT_NUS_SERVICE_DEFINITION;
static struct bt_gatt_service nus_svc = {
	.attrs = attr_nus_svc,
	.attr_count = ARRAY_SIZE(attr_nus_svc),
};

Is this the right way to this, or am I missing something?

Thanks in advance!

Parents
  • Hi David, 
    It looks good. Have you tried to call bt_gatt_service_register() and bt_gatt_service_unregister() to enable and disable the service ?


    I would suggest to have a look at the \zephyr\subsys\bluetooth\mesh\pb_gatt_srv.c (for pb-gatt in mesh) as an example. In the example we enable and disable the PB-GATT service dynamically before and after the node is provisioned. 

Reply
  • Hi David, 
    It looks good. Have you tried to call bt_gatt_service_register() and bt_gatt_service_unregister() to enable and disable the service ?


    I would suggest to have a look at the \zephyr\subsys\bluetooth\mesh\pb_gatt_srv.c (for pb-gatt in mesh) as an example. In the example we enable and disable the PB-GATT service dynamically before and after the node is provisioned. 

Children
  • Hello Hung Bui,

    thanks for your fast reply.

    Have you tried to call bt_gatt_service_register() and bt_gatt_service_unregister() to enable and disable the service ?

    So far the bt_gatt_service_register() and bt_gatt_service_unregister() functions do not throw errors with my service structure.

    I would suggest to have a look at the \zephyr\subsys\bluetooth\mesh\pb_gatt_srv.c (for pb-gatt in mesh) as an example. In the example we enable and disable the PB-GATT service dynamically before and after the node is provisioned. 

    Great, this example looks like what I was searching for.

    Best regards,
    David

Related