#include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <gatt/gap.h> #include <gatt/hrs.h> #include <gatt/dis.h> #include <gatt/bas.h> #define DEVICE_NAME "Zephyr Heartrate Sensor" #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define HEART_RATE_APPEARANCE 0x0341 struct bt_conn *default_conn; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18), }; static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn, uint8_t err) { if (err) { printk("Connection failed (err %u)\n", err); } else { default_conn = bt_conn_ref(conn); printk("Connected\n"); }
{ printk("Disconnected (reason %u)\n", reason); if (default_conn) { bt_conn_unref(default_conn); default_conn = NULL; } } static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, }; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), /* Custom power service UUID 9C10C448-3082-44CD-853D-08266C070BE5 */ BT_DATA_BYTES(BT_DATA_UUID128_ALL, 0xE5, 0x0B, 0x07, 0x6C, 0x26, 0x08, 0x3D, 0x85, 0xCD, 0x44, 0x82, 0x30, 0x48, 0xC4, 0x10, 0x9C) }; static struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN) }; /* Power Service variables */ /* Service UUID 9C10C448-3082-44CD-853D-08266C070BE5 */ static struct bt_uuid_128 power_uuid = BT_UUID_INIT_128( 0xE5, 0x0B, 0x07, 0x6C, 0x26, 0x08, 0x3D, 0x85, 0xCD, 0x44, 0x82, 0x30, 0x48, 0xC4, 0x10, 0x9C);
read_vnd, write_vnd, vnd_value), BT_GATT_CHARACTERISTIC(&vnd_long_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_EXT_PROP), BT_GATT_DESCRIPTOR(&vnd_long_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_long_vnd, write_long_vnd, &vnd_long_value), BT_GATT_CEP(&vnd_long_cep), BT_GATT_CHARACTERISTIC(&vnd_signed_uuid.uuid, BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_AUTH), BT_GATT_DESCRIPTOR(&vnd_signed_uuid.uuid, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, read_signed, write_signed, &signed_value), }; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18), BT_DATA_BYTES(BT_DATA_UUID128_ALL, 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12), }; static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn, uint8_t err) { if (err) { printk("Connection failed (err %u)\n", err);
printk("Not connected\n"); return 0; } err = bt_gatt_exchange_mtu(default_conn, exchange_rsp); if (err) { printk("Exchange failed (err %d)\n", err); } else { printk("Exchange pending\n"); } return 0; } static const struct bt_data ad_discov[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), }; static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static int cmd_advertise(int argc, char *argv[]) { struct bt_le_adv_param param; const struct bt_data *ad, *scan_rsp; size_t ad_len, scan_rsp_len; if (argc < 2) { goto fail; }
#include <bluetooth/hci.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <gatt/gap.h> #include <gatt/dis.h> #include <gatt/bas.h> #include <gatt/hog.h> #define DEVICE_NAME CONFIG_BLUETOOTH_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define HOG_APPEARANCE 0x03c2 static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x12, 0x18, /* HID Service */ 0x0f, 0x18), /* Battery Service */ }; static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void connected(struct bt_conn *conn, u8_t err) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
static void advertise_start(enum ble_adv_reason reason) { size_t adv_len; struct bt_le_adv_param adv_param; uint16_t interval; uint16_t timeout; struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE, BLE_APP_APPEARANCE & 0xFF, BLE_APP_APPEARANCE >> 8), BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA, BLE_APP_MANUFACTURER & 0xFF, BLE_APP_MANUFACTURER >> 8), BT_DATA(BT_DATA_NAME_COMPLETE, _ble_app_cb.device_name, strlen((char *)_ble_app_cb.device_name)) }; struct bt_data *ad_name = &ad[3]; int status; pr_info(LOG_MODULE_MAIN, "advertise_start: reason:0x%x", reason); switch (reason) { case BLE_ADV_USER1: case BLE_ADV_USER2: case BLE_ADV_DISCONNECT: timeout = CONFIG_BLE_APP_DEFAULT_ADV_TIMEOUT; interval = CONFIG_BLE_APP_DEFAULT_ADV_INTERVAL; break; case BLE_ADV_TIMEOUT: interval = CONFIG_BLE_ADV_SLOW_INTERVAL; timeout = CONFIG_BLE_ADV_SLOW_TIMEOUT; break; case BLE_ADV_STARTUP: interval = CONFIG_BLE_ULTRA_FAST_ADV_INTERVAL; if (get_boot_target() == TARGET_RECOVERY) timeout = 0; else timeout = CONFIG_BLE_APP_DEFAULT_ADV_TIMEOUT; break; default: interval = CONFIG_BLE_APP_DEFAULT_ADV_INTERVAL; timeout = 0; break; } adv_param.type = BT_LE_ADV_IND; adv_param.interval_max = interval; adv_param.interval_min = interval; adv_param.addr_type = _ble_app_cb.my_bd_addr.type; adv_len = adv_data_len(ad, ARRAY_SIZE(ad)) + 2 * ARRAY_SIZE(ad); if (adv_len > 31) { ad_name->data_len -= adv_len - 31; ad_name->type = BT_DATA_NAME_SHORTENED; } status = bt_le_adv_start(&adv_param, ad, ARRAY_SIZE(ad), NULL, 0); if (!status && timeout) ble_app_adv_timer_start(timeout, adv_timeout_cb); if (status) pr_error(LOG_MODULE_MAIN, "start_adv err %d", status); }