Skip to content

Commit 43c5d62

Browse files
committed
BLE scan response example added
1 parent 608e725 commit 43c5d62

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

28_ble_scan_response/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := ble_scan_response
7+
8+
include $(IDF_PATH)/make/project.mk
9+

28_ble_scan_response/main/component.mk

Whitespace-only changes.

28_ble_scan_response/main/main.c

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <freertos/FreeRTOS.h>
2+
#include <freertos/task.h>
3+
4+
#include "nvs_flash.h"
5+
#include "esp_err.h"
6+
#include "esp_log.h"
7+
8+
#include "esp_bt.h"
9+
#include "esp_bt_main.h"
10+
#include "esp_gap_ble_api.h"
11+
12+
static esp_ble_adv_params_t ble_adv_params = {
13+
14+
.adv_int_min = 0x20,
15+
.adv_int_max = 0x40,
16+
.adv_type = ADV_TYPE_NONCONN_IND,
17+
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
18+
.channel_map = ADV_CHNL_ALL,
19+
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
20+
};
21+
22+
static esp_ble_adv_data_t adv_data = {
23+
24+
.include_name = true,
25+
.flag = ESP_BLE_ADV_FLAG_LIMIT_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT,
26+
};
27+
28+
static uint8_t manufacturer_data[6] = {0xE5,0x02,0x01,0x01,0x01,0x01};
29+
static esp_ble_adv_data_t scan_rsp_data = {
30+
31+
.set_scan_rsp = true,
32+
.manufacturer_len = 6,
33+
.p_manufacturer_data = manufacturer_data,
34+
};
35+
36+
static uint8_t adv_raw_data[10] = {0x09,0x09,0x4c,0x75,0x6b,0x45,0x53,0x50,0x33,0x32};
37+
static uint8_t scan_rsp_raw_data[8] = {0x07,0xFF,0xE5,0x02,0x01,0x01,0x01,0x01};
38+
39+
bool adv_data_set = false;
40+
bool scan_rsp_data_set = false;
41+
42+
// GAP callback
43+
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
44+
{
45+
switch (event) {
46+
47+
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
48+
49+
printf("ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT\n");
50+
adv_data_set = true;
51+
if(scan_rsp_data_set) esp_ble_gap_start_advertising(&ble_adv_params);
52+
break;
53+
54+
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
55+
56+
printf("ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT\n");
57+
adv_data_set = true;
58+
if(scan_rsp_data_set) esp_ble_gap_start_advertising(&ble_adv_params);
59+
break;
60+
61+
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
62+
63+
printf("ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT\n");
64+
scan_rsp_data_set = true;
65+
if(adv_data_set) esp_ble_gap_start_advertising(&ble_adv_params);
66+
break;
67+
68+
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
69+
70+
printf("ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT\n");
71+
scan_rsp_data_set = true;
72+
if(adv_data_set) esp_ble_gap_start_advertising(&ble_adv_params);
73+
break;
74+
75+
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
76+
77+
printf("ESP_GAP_BLE_ADV_START_COMPLETE_EVT\n");
78+
if(param->adv_start_cmpl.status == ESP_BT_STATUS_SUCCESS) {
79+
printf("Advertising started\n\n");
80+
}
81+
else printf("Unable to start advertising process, error code %d\n\n", param->scan_start_cmpl.status);
82+
break;
83+
84+
default:
85+
86+
printf("Event %d unhandled\n\n", event);
87+
break;
88+
}
89+
}
90+
91+
92+
void app_main() {
93+
94+
printf("BT broadcast\n\n");
95+
96+
// set components to log only errors
97+
esp_log_level_set("*", ESP_LOG_ERROR);
98+
99+
// initialize nvs
100+
ESP_ERROR_CHECK(nvs_flash_init());
101+
printf("- NVS init ok\n");
102+
103+
// release memory reserved for classic BT (not used)
104+
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
105+
printf("- Memory for classic BT released\n");
106+
107+
// initialize the BT controller with the default config
108+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
109+
esp_bt_controller_init(&bt_cfg);
110+
printf("- BT controller init ok\n");
111+
112+
// enable the BT controller in BLE mode
113+
esp_bt_controller_enable(ESP_BT_MODE_BLE);
114+
printf("- BT controller enabled in BLE mode\n");
115+
116+
// initialize Bluedroid library
117+
esp_bluedroid_init();
118+
esp_bluedroid_enable();
119+
printf("- Bluedroid initialized and enabled\n");
120+
121+
// register GAP callback function
122+
ESP_ERROR_CHECK(esp_ble_gap_register_callback(esp_gap_cb));
123+
printf("- GAP callback registered\n");
124+
125+
// configure the adv data
126+
ESP_ERROR_CHECK(esp_ble_gap_set_device_name("ESP32_ScanRsp"));
127+
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&adv_data));
128+
//ESP_ERROR_CHECK(esp_ble_gap_config_adv_data_raw(adv_raw_data, 10));
129+
printf("- ADV data configured\n");
130+
131+
// configure the scan response data
132+
ESP_ERROR_CHECK(esp_ble_gap_config_adv_data(&scan_rsp_data));
133+
//ESP_ERROR_CHECK(esp_ble_gap_config_scan_rsp_data_raw(scan_rsp_raw_data, 8));
134+
printf("- Scan response data configured\n\n");
135+
}

0 commit comments

Comments
 (0)