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); } }