Skip to content
Browse files

Bluetooth: gatt: Reuse existing handles from a previously added service

When removing a service using bt_gatt_service_unregister, its attributes
handles will keep their value which can cause a problem when adding the
service again to the GATT database.
When re-adding, the gatt_register routine is taking the last handle of
the GATT database to compare it with the handles of the service to be
If a service has handles with a lower value than the last handle of
the database an error will occur.
If we add/remove/add the last service, no error will occur as its
handles are always greater than the last one of the database.

Instead of resetting the handles when unregistering a service and
reassign new ones we make sure that the handles of the services are
not in use in the GATT database in order to place the service back
where it used to be.

Signed-off-by: Arthur Crepin-Leblond <>
  • Loading branch information...
arthur-proglove authored and nashif committed May 15, 2019
1 parent 6cf806a commit 1d3673479999965ef38646d76f836b0a71822878
Showing with 20 additions and 1 deletion.
  1. +20 −1 subsys/bluetooth/host/gatt.c
@@ -515,6 +515,25 @@ BT_GATT_SERVICE_DEFINE(_1_gatt_svc,


static u8_t found_attr(const struct bt_gatt_attr *attr, void *user_data)
const struct bt_gatt_attr **found = user_data;

*found = attr;


static const struct bt_gatt_attr *find_attr(uint16_t handle)
const struct bt_gatt_attr *attr = NULL;

bt_gatt_foreach_attr(handle, handle, found_attr, &attr);

return attr;

static int gatt_register(struct bt_gatt_service *svc)
struct bt_gatt_service *last;
@@ -539,7 +558,7 @@ static int gatt_register(struct bt_gatt_service *svc)
} else if (attrs->handle > handle) {
/* Use existing handle if valid */
handle = attrs->handle;
} else {
} else if (find_attr(attrs->handle)) {
/* Service has conflicting handles */
BT_ERR("Unable to register handle 0x%04x",

0 comments on commit 1d36734

Please sign in to comment.
You can’t perform that action at this time.