This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
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

How to do a GATT read for multiple characterisitics in nRF Connect SDK

Hello all,

I'm working on a BLE central application based on nRF Connect SDK V1.6.1. The application connects to certain BLE peripheral devices and does a service discovery for the device information service. If discovered I would like the application to read out multiple characteristics of the DIS but when I do so I get error 6 in my read attribute callback.

I adopted the ble_discovery module from the nrf_desktop application sample to my needs.

How can I do a GATT read for multiple characteristics? I couldn't find any example that does this but according to the zephyr documentation it should be possible.

Below are the relevant code parts. The error is catched in line 5.

static uint8_t read_attr(struct bt_conn *conn, uint8_t err,
		      struct bt_gatt_read_params *params,
		      const void *data, uint16_t length)
{
	if (err) {
		LOG_ERR("Problem reading GATT (err:%" PRIu8 ")", err);
		peer_disconnect(conn);
		return BT_GATT_ITER_STOP;
	}

    __ASSERT_NO_MSG(data != NULL);
	const uint8_t *data_ptr = data;

	switch (state) {
	case DISCOVERY_STATE_DIS_FW_VER:
		read_dis(data_ptr, length);
		break;

	default:
		__ASSERT_NO_MSG(false);
	}

	k_work_submit(&next_discovery_step);

	return BT_GATT_ITER_STOP;
}

static uint16_t handles[2];

static void discovery_completed(struct bt_gatt_dm *dm, void *context)
{
	__ASSERT_NO_MSG(dm != NULL);
	__ASSERT_NO_MSG(discovering_peer_conn == bt_gatt_dm_conn_get(dm));
	const struct bt_uuid *model_num_uuid = dis_uuids[0];
    const struct bt_uuid *fw_uuid = dis_uuids[1];
	const struct bt_gatt_dm_attr *attr;
	int err;

	attr = bt_gatt_dm_char_by_uuid(dm, model_num_uuid);

	if (!attr) {
		LOG_ERR("Characteristic not found - disconnecting");
		err = bt_gatt_dm_data_release(dm);
		if (err) {
			goto error;
		}
		peer_disconnect(bt_gatt_dm_conn_get(dm));
		return;
	}

    handles[0] = attr->handle + 1;

    attr = bt_gatt_dm_char_by_uuid(dm, fw_uuid);

	if (!attr) {
		LOG_ERR("Characteristic not found - disconnecting");
		err = bt_gatt_dm_data_release(dm);
		if (err) {
			goto error;
		}
		peer_disconnect(bt_gatt_dm_conn_get(dm));
		return;
	}

    handles[1] = attr->handle + 1;

    static struct bt_gatt_read_params rp =  {
		.func = read_attr,
		.handle_count = 2,
	};

    rp.handles = handles;

	err = bt_gatt_read(bt_gatt_dm_conn_get(dm), &rp);
	if (err) {
		LOG_ERR("GATT read problem (err:%d)", err);
		peer_disconnect(bt_gatt_dm_conn_get(dm));
	}

	err = bt_gatt_dm_data_release(dm);
	if (err) {
		goto error;
	}

	return;
error:
	LOG_ERR("Discovery data release failed (err:%d)", err);
	module_set_state(MODULE_STATE_ERROR);
}

Thanks in advance!

Parents Reply Children
No Data
Related