|
| 1 | +/* |
| 2 | + * BLEAdvertising.cpp |
| 3 | + * |
| 4 | + * Created on: Jun 21, 2017 |
| 5 | + * Author: kolban |
| 6 | + */ |
| 7 | + |
| 8 | +#include "BLEAdvertising.h" |
| 9 | +#include <esp_log.h> |
| 10 | +#include <esp_err.h> |
| 11 | + |
| 12 | +static char TAG[] = "BLEAdvertising"; |
| 13 | + |
| 14 | +extern "C" { |
| 15 | + char *espToString(esp_err_t value); |
| 16 | +} |
| 17 | + |
| 18 | +BLEAdvertising::BLEAdvertising() { |
| 19 | + // TODO Auto-generated constructor stub |
| 20 | + m_advData.set_scan_rsp = false; |
| 21 | + m_advData.include_name = true; |
| 22 | + m_advData.include_txpower = true; |
| 23 | + m_advData.min_interval = 0x20; |
| 24 | + m_advData.max_interval = 0x40; |
| 25 | + m_advData.appearance = 0x00; |
| 26 | + m_advData.manufacturer_len = 0; |
| 27 | + m_advData.p_manufacturer_data = nullptr; |
| 28 | + m_advData.service_data_len = 0; |
| 29 | + m_advData.p_service_data = nullptr; |
| 30 | + m_advData.service_uuid_len = 0; |
| 31 | + m_advData.p_service_uuid = nullptr; |
| 32 | + m_advData.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT); |
| 33 | + |
| 34 | + m_advParams.adv_int_min = 0x20; |
| 35 | + m_advParams.adv_int_max = 0x40; |
| 36 | + m_advParams.adv_type = ADV_TYPE_IND; |
| 37 | + m_advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC; |
| 38 | + m_advParams.channel_map = ADV_CHNL_ALL; |
| 39 | + m_advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY; |
| 40 | +} // BLEAdvertising |
| 41 | + |
| 42 | +BLEAdvertising::~BLEAdvertising() { |
| 43 | + // TODO Auto-generated destructor stub |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * @brief Start advertising. |
| 49 | + * Start advertising. |
| 50 | + * @return N/A. |
| 51 | + */ |
| 52 | +void BLEAdvertising::start() { |
| 53 | + // Set the configuration for advertising. |
| 54 | + esp_err_t errRc = ::esp_ble_gap_config_adv_data(&m_advData); |
| 55 | + if (errRc != ESP_OK) { |
| 56 | + ESP_LOGE(TAG, "esp_ble_gap_config_adv_data: rc=%d %s", errRc, espToString(errRc)); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Start advertising. |
| 61 | + errRc = ::esp_ble_gap_start_advertising(&m_advParams); |
| 62 | + if (errRc != ESP_OK) { |
| 63 | + ESP_LOGE(TAG, "esp_ble_gap_start_advertising: rc=%d %s", errRc, espToString(errRc)); |
| 64 | + return; |
| 65 | + } |
| 66 | +} // advertising |
| 67 | + |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief Stop advertising. |
| 71 | + * Stop advertising. |
| 72 | + * @return N/A. |
| 73 | + */ |
| 74 | +void BLEAdvertising::stop() { |
| 75 | + esp_err_t errRc = ::esp_ble_gap_stop_advertising(); |
| 76 | + if (errRc != ESP_OK) { |
| 77 | + ESP_LOGE(TAG, "esp_ble_gap_stop_advertising: rc=%d %s", errRc, espToString(errRc)); |
| 78 | + return; |
| 79 | + } |
| 80 | +} // stop |
| 81 | + |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief Set the device appearance in the advertising data. |
| 85 | + * The apperance attribute is of type 0x19. The codes for distinct appearances can be found here: |
| 86 | + * https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.gap.appearance.xml. |
| 87 | + * @param [in] appearance The appearance of the device in the advertising data. |
| 88 | + * @return N/A. |
| 89 | + */ |
| 90 | +void BLEAdvertising::setAppearance(uint16_t appearance) { |
| 91 | + m_advData.appearance = appearance; |
| 92 | +} // setAppearance |
0 commit comments