Skip to content

Commit

Permalink
Merge branch 'docs/optimized_ble_adv_data_type' into 'master'
Browse files Browse the repository at this point in the history
docs(ble/bluedroid): Optimize BLE example for setting advertising data

Closes BLERP-919

See merge request espressif/esp-idf!32735
  • Loading branch information
Isl2017 committed Aug 14, 2024
2 parents fbab3bc + f9e45f9 commit cd11782
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,23 @@ static prepare_type_env_t prepare_write_env;
//#define CONFIG_SET_RAW_ADV_DATA
#ifdef CONFIG_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = {
/* flags */
0x02, 0x01, 0x06,
/* tx power*/
0x02, 0x0a, 0xeb,
/* service uuid */
0x03, 0x03, 0xFF, 0x00,
/* device name */
0x0E, 0x09, 'B', 'L', 'E', '_', 'C', 'O','M', 'P', '_', 'T','E', 'S', 'T'
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* TX Power */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xeb,
/* Service UUID */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00,
/* Device Name */
0x0E, ESP_BLE_AD_TYPE_NAME_CMPL, 'B', 'L', 'E', '_', 'C', 'O', 'M', 'P', '_', 'T', 'E', 'S', 'T'
};

static uint8_t raw_scan_rsp_data[] = {
/* flags */
0x02, 0x01, 0x06,
/* tx power */
0x02, 0x0a, 0xeb,
/* service uuid */
0x03, 0x03, 0xFF,0x00
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* TX Power */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xeb,
/* Service UUID */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00
};

#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ static const uint16_t spp_service_uuid = 0xABF0;

static const uint8_t spp_adv_data[23] = {
/* Flags */
0x02,0x01,0x06,
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* Complete List of 16-bit Service Class UUIDs */
0x03,0x03,0xF0,0xAB,
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xF0, 0xAB,
/* Complete Local Name in advertising */
0x0F,0x09, 'E', 'S', 'P', '_', 'S', 'P', 'P', '_', 'S', 'E', 'R','V', 'E', 'R'
0x0F, ESP_BLE_AD_TYPE_NAME_CMPL, 'E', 'S', 'P', '_', 'S', 'P', 'P', '_', 'S', 'E', 'R', 'V', 'E', 'R'
};

static uint16_t spp_mtu_size = 23;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -98,12 +98,17 @@ static uint8_t adv_config_done = 0;

#ifdef CONFIG_EXAMPLE_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = {
0x02, 0x01, 0x06,
0x02, 0x0a, 0xeb, 0x03, 0x03, 0xab, 0xcd
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* TX Power Level */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xEB,
/* Service UUID */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xAB, 0xCD
};

static uint8_t raw_scan_rsp_data[] = {
0x0f, 0x09, 0x45, 0x53, 0x50, 0x5f, 0x47, 0x41, 0x54, 0x54, 0x53, 0x5f, 0x44,
0x45, 0x4d, 0x4f
/* Complete Local Name */
0x0F, ESP_BLE_AD_TYPE_NAME_CMPL, 'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D', 'E', 'M', 'O'
};
#else

Expand Down
16 changes: 10 additions & 6 deletions examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ static uint8_t adv_config_done = 0;

#ifdef CONFIG_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = {
0x02, 0x01, 0x06, // Length 2, Data Type 1 (Flags), Data 1 (LE General Discoverable Mode, BR/EDR Not Supported)
0x02, 0x0a, 0xeb, // Length 2, Data Type 10 (TX power level), Data 2 (-21)
0x03, 0x03, 0xab, 0xcd, // Length 3, Data Type 3 (Complete 16-bit Service UUIDs), Data 3 (UUID)
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06, // Length 2, Data Type ESP_BLE_AD_TYPE_FLAG, Data 1 (LE General Discoverable Mode, BR/EDR Not Supported)
/* TX Power Level */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xEB, // Length 2, Data Type ESP_BLE_AD_TYPE_TX_PWR, Data 2 (-21)
/* Complete 16-bit Service UUIDs */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xAB, 0xCD // Length 3, Data Type ESP_BLE_AD_TYPE_16SRV_CMPL, Data 3 (UUID)
};
static uint8_t raw_scan_rsp_data[] = { // Length 15, Data Type 9 (Complete Local Name), Data 1 (ESP_GATTS_DEMO)
0x0f, 0x09, 0x45, 0x53, 0x50, 0x5f, 0x47, 0x41, 0x54, 0x54, 0x53, 0x5f, 0x44,
0x45, 0x4d, 0x4f

static uint8_t raw_scan_rsp_data[] = {
/* Complete Local Name */
0x0F, ESP_BLE_AD_TYPE_NAME_CMPL, 'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D', 'E', 'M', 'O' // Length 15, Data Type ESP_BLE_AD_TYPE_NAME_CMPL, Data (ESP_GATTS_DEMO)
};
#else

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,24 @@ static prepare_type_env_t prepare_write_env;
#define CONFIG_SET_RAW_ADV_DATA
#ifdef CONFIG_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = {
/* flags */
0x02, 0x01, 0x06,
/* tx power*/
0x02, 0x0a, 0xeb,
/* service uuid */
0x03, 0x03, 0xFF, 0x00,
/* device name */
0x0f, 0x09, 'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D','E', 'M', 'O'
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* TX Power Level */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xEB,
/* Complete 16-bit Service UUIDs */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00,
/* Complete Local Name */
0x0F, ESP_BLE_AD_TYPE_NAME_CMPL,
'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D', 'E', 'M', 'O'
};

static uint8_t raw_scan_rsp_data[] = {
/* flags */
0x02, 0x01, 0x06,
/* tx power */
0x02, 0x0a, 0xeb,
/* service uuid */
0x03, 0x03, 0xFF,0x00
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06,
/* TX Power Level */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xEB,
/* Complete 16-bit Service UUIDs */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xFF, 0x00
};

#else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
menu "Example 'GATTC_GATTS_COEX' Config"

config SET_RAW_ADV_DATA
bool "Use raw data for advertising packets and scan response data"
help
If this config item is set, raw binary data will be used to generate advertising & scan response data.
This option uses the esp_ble_gap_config_adv_data_raw() and esp_ble_gap_config_scan_rsp_data_raw()
functions.

If this config item is unset, advertising & scan response data is provided via a higher-level
esp_ble_adv_data_t structure. The lower layer will generate the BLE packets. This option has higher
overhead at runtime.

endmenu
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ esp_attr_value_t gatts_demo_char1_val = {
.attr_value = char1_str,
};

#ifdef CONFIG_SET_RAW_ADV_DATA
static uint8_t raw_adv_data[] = {
/* Flags */
0x02, ESP_BLE_AD_TYPE_FLAG, 0x06, // Length 2, Data Type ESP_BLE_AD_TYPE_FLAG, Data 1 (LE General Discoverable Mode, BR/EDR Not Supported)
/* TX Power Level */
0x02, ESP_BLE_AD_TYPE_TX_PWR, 0xEB, // Length 2, Data Type ESP_BLE_AD_TYPE_TX_PWR, Data 2 (-21)
/* Complete 16-bit Service UUIDs */
0x03, ESP_BLE_AD_TYPE_16SRV_CMPL, 0xAB, 0xCD // Length 3, Data Type ESP_BLE_AD_TYPE_16SRV_CMPL, Data 3 (UUID)
};

static uint8_t raw_scan_rsp_data[] = {
/* Complete Local Name */
0x0F, ESP_BLE_AD_TYPE_NAME_CMPL, 'E', 'S', 'P', '_', 'G', 'A', 'T', 'T', 'S', '_', 'D', 'E', 'M', 'O' // Length 15, Data Type ESP_BLE_AD_TYPE_NAME_CMPL, Data (ESP_GATTS_DEMO)
};

#else
static uint8_t service_uuid128[32] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
Expand Down Expand Up @@ -157,6 +173,7 @@ static esp_ble_adv_data_t scan_rsp_data = {
.p_service_uuid = NULL,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
#endif /* CONFIG_SET_RAW_ADV_DATA */

static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
Expand Down

0 comments on commit cd11782

Please sign in to comment.