Hi all
Im trying to implement on bluetooth communication between two nrf52840 DK, I have made a custom uuid service on peripherial. Now I have to do GATT discovery service on central board, because i want when i press button on central board DK to send a signal start to peripherial.
Is there a some code example how can u do GATT discovery, im able to connect boards but GATT discovery isnt working.
static uint8_t discover_func(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params)
{
int err;
printk("discover_func called, type=%d attr=%p\n", params->type, (void*)attr);
if (!attr) {
printk("Discover complete\n");
k_sem_give(&discover_sem);
return BT_GATT_ITER_STOP;
}
if (params->type == BT_GATT_DISCOVER_PRIMARY) {
const struct bt_gatt_service_val *svc = attr->user_data;
lbs_start_handle = attr->handle;
lbs_end_handle = svc->end_handle;
printk("LBS service: start 0x%04x end 0x%04x\n", lbs_start_handle, lbs_end_handle);
/* Sledeći korak: tražimo BUTTON karakteristiku unutar servisa */
static struct bt_gatt_discover_params ch_params;
memset(&ch_params, 0, sizeof(ch_params));
ch_params.uuid = BT_UUID_LBS_BUTTON; /* ← BUTTON UUID */
ch_params.func = discover_func;
ch_params.start_handle = lbs_start_handle + 1;
ch_params.end_handle = lbs_end_handle;
ch_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
err = bt_gatt_discover(conn, &ch_params);
if (err) {
printk("Discover characteristics failed (err %d)\n", err);
k_sem_give(&discover_sem);
}
return BT_GATT_ITER_STOP;
}
if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
char ustr[BT_UUID_STR_LEN];
bt_uuid_to_str(chrc->uuid, ustr, sizeof(ustr));
printk("Char: %s props=0x%02x val=0x%04x\n", ustr, chrc->properties, chrc->value_handle);
if (bt_uuid_cmp(chrc->uuid, BT_UUID_LBS_BUTTON) == 0) {
button_char_handle = chrc->value_handle; /* VAŽNO: value_handle */
printk("Found LBS BUTTON char, value handle: 0x%04x\n", button_char_handle);
/* (Opciono) možeš proveriti da li ima WRITE/WWR bit u chrc->properties
ako želiš runtime verifikaciju. */
k_sem_give(&discover_sem);
return BT_GATT_ITER_STOP;
}
}
return BT_GATT_ITER_CONTINUE;
}
static int discover_lbs_service(struct bt_conn *conn)
{
printk("usao u discover_lbs_service\n");
button_char_handle = 0;
lbs_start_handle = lbs_end_handle = 0;
static struct bt_gatt_discover_params svc_params;
memset(&svc_params, 0, sizeof(svc_params));
svc_params.uuid = BT_UUID_LBS; /* 1) PRIMARNI SERVIS */
svc_params.func = discover_func;
svc_params.start_handle = BLE_GATT_HANDLE_START;
svc_params.end_handle = BLE_GATT_HANDLE_END;
svc_params.type = BT_GATT_DISCOVER_PRIMARY;
int err = bt_gatt_discover(conn, &svc_params);
if (err) {
printk("Discover failed (err %d)\n", err);
return err;
}
k_sem_take(&discover_sem, K_FOREVER);
if (!button_char_handle) {
printk("BUTTON char not found inside LBS\n");
return -ENOENT;
}
return 0;
}
static void button_work_handler(struct k_work *work);
K_WORK_DEFINE(button_work, button_work_handler);
static void button_work_handler(struct k_work *work)
{
if (!is_connected || button_char_handle == 0) {
return;
}
uint8_t v = 0x01; /* vrednost koju periferija očekuje */
if (k_sem_take(&ble_sem, K_NO_WAIT) == 0) {
bt_gatt_write_without_response(default_conn,
button_char_handle,
&v,
sizeof(v),
false);
printk("Sent 0x%02x to Button (WWR)\n", v);
k_sem_give(&ble_sem);
}
}
Best regards.