Implement the discovery_completed_cb() method to discover all the peripheral services and initialize them to be able to use their writing, reading, notification, and initialization methods in the application (main)

Hello everyone, I'm trying to create a sample code for a central unit on the NRF52840 to work with two services simultaneously, from a single connection with the peripheral.

My tests manage to connect between the central and peripheral devices, but I can't discover all the services on the central unit side and be able to use all the services provided by the peripheral server.

I'll describe my code here, I hope you can help me, thank you in advance.

# On the Peripheral side

I start the BAS, HTS and LBS services.

# On the Central side

In the discovery part, I implement the callbacks, discovery_completed_cb(), discovery_service_not_found_cb((), void discovery_error_found_cb(), and gatt_discover() to use in application,

and call the method bt_gatt_dm_start(conn, NULL, &discovery_cb, NULL); with NULL in the second field to discover all the services.

in the discovery_completed_cb() callback, I don't know how I should manipulate the attributes, see the attributes of the three services (BAS, HTS and LBS) and make their service methods available in the application.

static void discovery_completed_cb(struct bt_gatt_dm *dm, void *context)
{

const struct bt_gatt_dm_attr *attr = bt_gatt_dm_service_get(dm); // Obtém o primeiro atributo de serviço
const struct bt_gatt_service_val *service_val;
//const struct bt_gatt_dm_attr *desc;
//char uuid_str[BT_UUID_STR_LEN];

printf("Descoberta de serviços, características e descritores concluída:\n");

// Itera sobre os serviços e características usando bt_gatt_dm_attr_next
while (attr) {

printk("Handle: %d\n",attr->handle);
//printk("Permi: %d\n",attr->perm);

// Buffer para converter o UUID para string
char uuid_str[BT_UUID_STR_LEN];
bt_uuid_to_str(attr->uuid, uuid_str, sizeof(uuid_str));
printk("UUID: %s\n\n",uuid_str);

if (attr->uuid == 2800){
service_val = bt_gatt_dm_attr_service_val(attr);
}

attr = bt_gatt_dm_attr_next(dm, attr);
}


bt_gatt_dm_data_release(dm);

}

static void discovery_service_not_found_cb(struct bt_conn *conn, void *context)
{
printk("The service could not be found during the discovery\n");
}

static void discovery_error_found_cb(struct bt_conn *conn, int err, void *context)
{
printk("The discovery procedure failed with %d\n", err);
}

static struct bt_gatt_dm_cb discovery_cb = {
.completed = discovery_completed_cb,
.service_not_found = discovery_service_not_found_cb,
.error_found = discovery_error_found_cb,
};

static void gatt_discover(struct bt_conn *conn)
{
int err;

if (conn != default_conn) {
return;
}

err = bt_gatt_dm_start(conn, NULL, &discovery_cb, NULL);
if (err) {
printk("Could not start the discovery procedure, error code: %d\n", err);
}

}

Related