/** * Enables advertising with the following parameters: * o General discoverable mode. * o Undirected connectable mode. */ static void sensor_oic_advertise(void) { struct ble_gap_adv_params adv_params; struct ble_hs_adv_fields fields; const char *name; int rc; /** * Set the advertisement data included in our advertisements: * o Flags (indicates advertisement type and other general info). * o Advertising tx power. * o Device name. * o 16-bit service UUIDs (alert notifications). */ memset(&fields, 0, sizeof fields); /* Advertise two flags: * o Discoverability in forthcoming advertisement (general) * o BLE-only (BR/EDR unsupported). */ fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; /* Indicate that the TX power level field should be included; have the * stack fill this value automatically. This is done by assiging the * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. */ fields.tx_pwr_lvl_is_present = 1; fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; name = ble_svc_gap_device_name(); fields.name = (uint8_t *)name; fields.name_len = strlen(name); fields.name_is_complete = 1; fields.uuids128 = (ble_uuid128_t []) { BLE_UUID128_INIT(OC_GATT_UNSEC_SVC_UUID) }; fields.num_uuids128 = 1; fields.uuids128_is_complete = 1; rc = ble_gap_adv_set_fields(&fields); if (rc != 0) { BLEPRPH_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); return; } /* Begin advertising. */ memset(&adv_params, 0, sizeof adv_params); adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, sensor_oic_gap_event, NULL); if (rc != 0) { BLEPRPH_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); return; } }
/** * Enables advertising with the following parameters: * o General discoverable mode. * o Undirected connectable mode. */ static void bleuart_advertise(void) { struct ble_gap_adv_params adv_params; struct ble_hs_adv_fields fields; int rc; /* * Set the advertisement data included in our advertisements: * o Flags (indicates advertisement type and other general info). * o Advertising tx power. * o 128 bit UUID */ memset(&fields, 0, sizeof fields); /* Advertise two flags: * o Discoverability in forthcoming advertisement (general) * o BLE-only (BR/EDR unsupported). */ fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; /* Indicate that the TX power level field should be included; have the * stack fill this value automatically. This is done by assiging the * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. */ fields.tx_pwr_lvl_is_present = 1; fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; fields.uuids128 = BLE_UUID128(&gatt_svr_svc_uart_uuid.u); fields.num_uuids128 = 1; fields.uuids128_is_complete = 1; rc = ble_gap_adv_set_fields(&fields); if (rc != 0) { return; } memset(&fields, 0, sizeof fields); fields.name = (uint8_t *)ble_svc_gap_device_name(); fields.name_len = strlen((char *)fields.name); fields.name_is_complete = 1; rc = ble_gap_adv_rsp_set_fields(&fields); if (rc != 0) { return; } /* Begin advertising. */ memset(&adv_params, 0, sizeof adv_params); adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, bleuart_gap_event, NULL); if (rc != 0) { return; } }
/** * Enables advertising with the following parameters: * o General discoverable mode. * o Undirected connectable mode. */ static void bleprph_advertise(void) { struct ble_hs_adv_fields fields; int rc; /** * Set the advertisement data included in our advertisements: * o Advertising tx power. * o Device name. * o 16-bit service UUIDs (alert notifications). */ memset(&fields, 0, sizeof fields); fields.tx_pwr_lvl_is_present = 1; fields.name = (uint8_t *)bleprph_device_name; fields.name_len = strlen(bleprph_device_name); fields.name_is_complete = 1; fields.uuids16 = (uint16_t[]){ GATT_SVR_SVC_ALERT_UUID }; fields.num_uuids16 = 1; fields.uuids16_is_complete = 1; rc = ble_gap_adv_set_fields(&fields); if (rc != 0) { BLEPRPH_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); return; } /* Begin advertising. */ rc = ble_gap_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_UND, NULL, 0, NULL, bleprph_gap_event, NULL); if (rc != 0) { BLEPRPH_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); return; } }
/** * Configures the device to advertise iBeacons. * * @param uuid The 128-bit UUID to advertise. * @param major The major version number to include in * iBeacons. * @param minor The minor version number to include in * iBeacons. * * @return 0 on success; * BLE_HS_EBUSY if advertising is in progress; * Other nonzero on failure. */ int ble_ibeacon_set_adv_data(void *uuid128, uint16_t major, uint16_t minor) { struct ble_hs_adv_fields fields; uint8_t buf[BLE_IBEACON_MFG_DATA_SIZE]; int8_t tx_pwr; int rc; /** Company identifier (Apple). */ buf[0] = 0x4c; buf[1] = 0x00; /** iBeacon indicator. */ buf[2] = 0x02; buf[3] = 0x15; /** UUID. */ memcpy(buf + 4, uuid128, 16); /** Version number. */ put_be16(buf + 20, major); put_be16(buf + 22, minor); /** Last byte (tx power level) filled in after HCI exchange. */ rc = ble_hs_hci_util_read_adv_tx_pwr(&tx_pwr); if (rc != 0) { return rc; } buf[24] = tx_pwr; memset(&fields, 0, sizeof fields); fields.mfg_data = buf; fields.mfg_data_len = sizeof buf; rc = ble_gap_adv_set_fields(&fields); return rc; }
/** * Populates the supplied advertisement fields struct to represent an eddystone * advertisement. Prior to calling this function, you must write the service * data header and payload using the ble_eddystone_set_svc_data_base() * function. * * @param adv_fields The base advertisement fields to transform into * an eddystone beacon. All configured fields * are preserved; you probably want to clear * this struct before calling this function. * @param svc_data_len The amount of data written to the global * service data buffer. * * @return 0 on success; BLE_HS_E... on failure. */ static int ble_eddystone_set_adv_data_gen(struct ble_hs_adv_fields *adv_fields, uint8_t svc_data_len) { int rc; if (adv_fields->num_uuids16 > BLE_EDDYSTONE_MAX_UUIDS16) { return BLE_HS_EINVAL; } if (svc_data_len > BLE_EDDYSTONE_MAX_SVC_DATA_LEN) { return BLE_HS_EINVAL; } if (adv_fields->num_uuids16 > 0 && !adv_fields->uuids16_is_complete) { return BLE_HS_EINVAL; } if (adv_fields->svc_data_uuid16_len != 0) { return BLE_HS_EINVAL; } ble_eddystone_uuids16[0] = BLE_EDDYSTONE_SERVICE_UUID; memcpy(ble_eddystone_uuids16 + 1, adv_fields->uuids16, adv_fields->num_uuids16 * 2); adv_fields->uuids16 = ble_eddystone_uuids16; adv_fields->num_uuids16++; adv_fields->uuids16_is_complete = 1; adv_fields->svc_data_uuid16 = ble_eddystone_svc_data; adv_fields->svc_data_uuid16_len = svc_data_len + BLE_EDDYSTONE_SVC_DATA_BASE_SZ; rc = ble_gap_adv_set_fields(adv_fields); if (rc != 0) { return rc; } return 0; }
/** * Enables advertising with the following parameters: * o General discoverable mode. * o Undirected connectable mode. */ static void blesplit_advertise(void) { uint8_t own_addr_type; struct ble_gap_adv_params adv_params; struct ble_hs_adv_fields fields; const char *name; int rc; /* Figure out address to use while advertising (no privacy for now) */ rc = ble_hs_id_infer_auto(0, &own_addr_type); if (rc != 0) { BLESPLIT_LOG(ERROR, "error determining address type; rc=%d\n", rc); return; } /** * Set the advertisement data included in our advertisements: * o Flags (indicates advertisement type and other general info). * o Advertising tx power. * o Device name. * o 16-bit service UUIDs (alert notifications). */ memset(&fields, 0, sizeof fields); /* Advertise two flags: * o Discoverability in forthcoming advertisement (general) * o BLE-only (BR/EDR unsupported). */ fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; /* Indicate that the TX power level field should be included; have the * stack fill this value automatically. This is done by assiging the * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. */ fields.tx_pwr_lvl_is_present = 1; fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; name = ble_svc_gap_device_name(); fields.name = (uint8_t *)name; fields.name_len = strlen(name); fields.name_is_complete = 1; fields.uuids16 = (ble_uuid16_t[]) { BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID), }; fields.num_uuids16 = 1; fields.uuids16_is_complete = 1; rc = ble_gap_adv_set_fields(&fields); if (rc != 0) { BLESPLIT_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); return; } /* Begin advertising. */ memset(&adv_params, 0, sizeof adv_params); adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, blesplit_gap_event, NULL); if (rc != 0) { BLESPLIT_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); return; } }