Custom characteristics are not appearing when I attempt iterate through the attributes

the code below successfully complies and returns 3 services with 1 characteristic each, I have copied the uuid for the primary services and characteristic and that doesn't seem to have caused an issue in the nrf connect app

struct bt_gatt_attr attrs1[10]; // Adjust size as needed
struct bt_gatt_attr attrs2[10]; // Adjust size as needed
struct bt_gatt_attr attrs3[10]; // Adjust size as needed
struct bt_gatt_service services[4]; // Assuming 3 services

for (int i = 0; i < 3; i++) {
    int attr_count = 0; // Reset attr_count for each service
    struct bt_gatt_attr *attrs;

    // Assign the correct attrs array for each service
    if (i == 0) {
        attrs = attrs1;
    } else if (i == 1) {
        attrs = attrs2;
    } else {
        attrs = attrs3;
    }

    attrs[attr_count++] = (struct bt_gatt_attr) {
        .uuid = BT_UUID_GATT_PRIMARY,
        .perm = BT_GATT_PERM_READ,
        .read = bt_gatt_attr_read_service,
        .user_data = uuids_struct[i].val,
    };

    attrs[(attr_count)++] = (struct bt_gatt_attr) {
        .uuid = BT_UUID_GATT_CHRC,
        .perm = BT_GATT_PERM_READ,
        .read = bt_gatt_attr_read_chrc,
        .user_data = &(struct bt_gatt_chrc) {
            .properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
            .uuid = uuids_struct[i].val,
        },
    };

    attrs[(attr_count)++] = (struct bt_gatt_attr) {
        .uuid = uuids_struct[i].val,
        .perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
        .read = NULL,
        .write = NULL,
        .user_data = NULL,
    };

    printk("updated\n");

    services[i] = (struct bt_gatt_service) {
        .attrs = attrs,
        .attr_count = attr_count
    };

    int result = bt_gatt_service_register(&services[i]);
    if (result) {
        LOG_ERR("Failed to register service %d (err %d)", i, result);
    } else {
        LOG_INF("Service %d registered successfully", i);
    }
}

the code below successfully complies but only displays the services, I'm unsure what the issue maybe

 

struct bt_gatt_attr attrs1[10]; // Adjust size as needed
struct bt_gatt_attr attrs2[10]; // Adjust size as needed
struct bt_gatt_attr attrs3[10]; // Adjust size as needed
struct bt_gatt_service services[4]; // Assuming 3 services

for (int i = 0; i < 3; i++) {
    int attr_count = 0; // Reset attr_count for each service
    struct bt_gatt_attr *attrs;

    // Assign the correct attrs array for each service
    if (i == 0) {
        attrs = attrs1;
    } else if (i == 1) {
        attrs = attrs2;
    } else {
        attrs = attrs3;
    }

    attrs[attr_count++] = (struct bt_gatt_attr) {
        .uuid = BT_UUID_GATT_PRIMARY,
        .perm = BT_GATT_PERM_READ,
        .read = bt_gatt_attr_read_service,
        .user_data = &uuids_struct[i].val,
    };
	for (int j = 0; j < 1; j++){
		printk("entering second loop\n");
		attrs[(attr_count)++] = (struct bt_gatt_attr) {
			.uuid = BT_UUID_GATT_CHRC,
			.perm = BT_GATT_PERM_READ,
			.read = bt_gatt_attr_read_chrc,
			.user_data = &(struct bt_gatt_chrc) {
				.properties = BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
				.uuid = &uuids_struct[i].val,
			},
		};

		attrs[(attr_count)++] = (struct bt_gatt_attr) {
			.uuid = &uuids_struct[i].val,
			.perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
			.read = NULL,
			.write = NULL,
			.user_data = NULL,
		};
	}
    services[i] = (struct bt_gatt_service) {
        .attrs = attrs,
        .attr_count = attr_count
    };

    int result = bt_gatt_service_register(&services[i]);
    if (result) {
        LOG_ERR("Failed to register service %d (err %d)", i, result);
    } else {
        LOG_INF("Service %d registered successfully", i);
    }
}

Parents Reply Children
  • I do not know why you use the second loop with "J" but it does not look like it loops at all, that for loop is executed only once and you are not using the loop count for anything inside the loop.

    Why don't you just use the same code if the first is working for you. Putting these two snippets in beyond compare side by side does not show any differences apart from these three things i told you.

  • sorry for the confusion, I'm planning on adding more than one characteristic per service in the future, I simplified the code on the bottom for better readability, but the term j is going to be used to index a struct of uuid array to assign to the characteristics attribute. I was hoping to reduce the amount of lines of code by using iteration as having 10 or more services with approximately 5 attributes each hard coded isn't exactly efficient or readable. I've attempted creating a separate function for the characteristic attribute but the issue persists.

Related