# central_zephyr **Repository Path**: zephyrspace/central_zephyr ## Basic Information - **Project Name**: central_zephyr - **Description**: 验证蓝牙主机模式 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-14 - **Last Updated**: 2025-03-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ```C #include #include #include #include #include #include #include #include #include #include LOG_MODULE_REGISTER(bt_host, LOG_LEVEL_INF); struct gatt_characteristic { struct bt_uuid *uuid; uint16_t handle; uint8_t properties; sys_snode_t node; }; struct bt_device { struct bt_conn *conn; bt_addr_le_t addr; struct bt_gatt_discover_params discover_params; struct bt_gatt_subscribe_params subscribe_params; sys_slist_t gatt_chars; sys_snode_t node; }; static sys_slist_t device_list; static struct bt_device *find_device(const bt_addr_le_t *addr) { struct bt_device *dev; SYS_SLIST_FOR_EACH_CONTAINER(&device_list, dev, node) { if (!bt_addr_le_cmp(&dev->addr, addr)) { return dev; } } return NULL; } static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params) { if (!attr) { LOG_INF("Service discovery completed"); return BT_GATT_ITER_STOP; } struct bt_device *dev = find_device(bt_conn_get_dst(conn)); if (!dev) { return BT_GATT_ITER_STOP; } struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data; struct gatt_characteristic *gatt_char = k_malloc(sizeof(*gatt_char)); if (!gatt_char) { LOG_ERR("Memory allocation failed for GATT characteristic"); return BT_GATT_ITER_STOP; } gatt_char->handle = attr->handle; gatt_char->uuid = (struct bt_uuid *)attr->uuid; gatt_char->properties = chrc->properties; sys_slist_append(&dev->gatt_chars, &gatt_char->node); LOG_INF("Discovered attribute, handle: 0x%04X, properties: 0x%02X", attr->handle, chrc->properties); return BT_GATT_ITER_CONTINUE; } int bt_host_send_data(const bt_addr_le_t *addr, struct bt_uuid *uuid, const uint8_t *data, uint16_t len) { struct bt_device *dev = find_device(addr); if (!dev) { LOG_WRN("Device not found"); return -ENOTCONN; } struct gatt_characteristic *gatt_char; SYS_SLIST_FOR_EACH_CONTAINER(&dev->gatt_chars, gatt_char, node) { if (!bt_uuid_cmp(gatt_char->uuid, uuid)) { if (gatt_char->properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP) { return bt_gatt_write_without_response(dev->conn, gatt_char->handle, data, len, false); } LOG_WRN("Characteristic does not support write without response"); return -EINVAL; } } LOG_WRN("Characteristic with UUID not found"); return -EINVAL; } static void connected(struct bt_conn *conn, uint8_t err) { if (err) { LOG_ERR("Connection failed (err 0x%02x)", err); return; } struct bt_device *dev = k_malloc(sizeof(*dev)); if (!dev) { LOG_ERR("Memory allocation failed for new device"); return; } dev->conn = bt_conn_ref(conn); bt_conn_get_info(conn, &dev->addr); sys_slist_init(&dev->gatt_chars); sys_slist_append(&device_list, &dev->node); LOG_INF("Connected to device"); dev->discover_params.uuid = NULL; dev->discover_params.func = discover_func; dev->discover_params.start_handle = 0x0001; dev->discover_params.end_handle = 0xffff; dev->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; bt_gatt_discover(dev->conn, &dev->discover_params); } static void disconnected(struct bt_conn *conn, uint8_t reason) { LOG_INF("Disconnected (reason 0x%02x)", reason); struct bt_device *dev = find_device(bt_conn_get_dst(conn)); if (dev) { struct gatt_characteristic *gatt_char, *tmp; SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&dev->gatt_chars, gatt_char, tmp, node) { sys_slist_find_and_remove(&dev->gatt_chars, &gatt_char->node); k_free(gatt_char); } sys_slist_find_and_remove(&device_list, &dev->node); bt_conn_unref(dev->conn); k_free(dev); } } ```