Select one of the symbols to view example projects that use it.
 
Outline
CONN_TAG
device_name
adv_params
adv_raw_data
app_main()
esp_gap_cb(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t *)
gatts_event_handler(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t *)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gatt_common_api.h"
#define APP_ID_PLACEHOLDER
Files
loading...
SourceVuESP-IDF Framework and ExamplesBluedroid_Connection samplemain/main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <inttypes.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_log.h" #include "nvs_flash.h" #include "esp_bt.h" #include "esp_gap_ble_api.h" #include "esp_gatts_api.h" #include "esp_bt_defs.h" #include "esp_bt_main.h" #include "esp_bt_device.h" #include "esp_gatt_common_api.h"17 includes #define APP_ID_PLACEHOLDER 0 static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); static const char *CONN_TAG = "CONN_DEMO"; static const char device_name[] = "Bluedroid_Conn"; static esp_ble_adv_params_t adv_params = { .adv_int_min = 0x20, // 20ms .adv_int_max = 0x20, // 20ms .adv_type = ADV_TYPE_IND, .own_addr_type = BLE_ADDR_TYPE_PUBLIC, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, }{...}; static uint8_t adv_raw_data[] = { 0x02, ESP_BLE_AD_TYPE_FLAG, 0x06, 0x0F, ESP_BLE_AD_TYPE_NAME_CMPL, 'B', 'l', 'u', 'e', 'd', 'r', 'o', 'i', 'd', '_', 'C', 'o', 'n', 'n', 0x02, ESP_BLE_AD_TYPE_TX_PWR, 0x09, }{...}; void app_main(void) { esp_err_t ret; //initialize NVS ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); }{...} ESP_ERROR_CHECK( ret ); ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg); if (ret) { ESP_LOGE(CONN_TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret) { ESP_LOGE(CONN_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_init(); if (ret) { ESP_LOGE(CONN_TAG, "%s init bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_bluedroid_enable(); if (ret) { ESP_LOGE(CONN_TAG, "%s enable bluetooth failed: %s", __func__, esp_err_to_name(ret)); return; }{...} ret = esp_ble_gap_register_callback(esp_gap_cb); if (ret) { ESP_LOGE(CONN_TAG, "%s gap register failed, error code = %x", __func__, ret); return; }{...} ret = esp_ble_gatts_register_callback(gatts_event_handler); if (ret) { ESP_LOGE(CONN_TAG, "%s gatts register failed, error code = %x", __func__, ret); return; }{...} ret = esp_ble_gatts_app_register(APP_ID_PLACEHOLDER); if (ret) { ESP_LOGE(CONN_TAG, "%s gatts app register failed, error code = %x", __func__, ret); return; }{...} ret = esp_ble_gatt_set_local_mtu(500); if (ret) { ESP_LOGE(CONN_TAG, "set local MTU failed, error code = %x", ret); return; }{...} ret = esp_ble_gap_set_device_name(device_name); if (ret) { ESP_LOGE(CONN_TAG, "set device name failed, error code = %x", ret); return; }{...} ret = esp_ble_gap_config_adv_data_raw(adv_raw_data, sizeof(adv_raw_data)); if (ret) { ESP_LOGE(CONN_TAG, "config adv data failed, error code = %x", ret); }{...} }{...} static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { switch (event) { case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: ESP_LOGI(CONN_TAG, "Advertising data set, status %d", param->adv_data_raw_cmpl.status); esp_ble_gap_start_advertising(&adv_params); break;... case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(CONN_TAG, "Advertising start failed, status %d", param->adv_start_cmpl.status); break; }{...} ESP_LOGI(CONN_TAG, "Advertising start successfully"); break;... case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(CONN_TAG, "Advertising stop failed, status %d", param->adv_stop_cmpl.status); }{...} ESP_LOGI(CONN_TAG, "Advertising stop successfully"); break;... case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: ESP_LOGI(CONN_TAG, "Connection params update, status %d, conn_int %d, latency %d, timeout %d", param->update_conn_params.status, param->update_conn_params.conn_int, param->update_conn_params.latency, param->update_conn_params.timeout); break;... default: break;... }{...} }{...} //because we only have one profile table in this demo, there is only one set of gatts evemt handler static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { switch (event) { case ESP_GATTS_REG_EVT: ESP_LOGI(CONN_TAG, "GATT server register, status %d, app_id %d", param->reg.status, param->reg.app_id); break;... case ESP_GATTS_CONNECT_EVT: esp_ble_conn_update_params_t conn_params = {0}; memcpy(conn_params.bda, param->connect.remote_bda, sizeof(esp_bd_addr_t)); conn_params.latency = 0; conn_params.max_int = 0x20; conn_params.min_int = 0x10; conn_params.timeout = 400; ESP_LOGI(CONN_TAG, "Connected, conn_id %u, remote "ESP_BD_ADDR_STR"", param->connect.conn_id, ESP_BD_ADDR_HEX(param->connect.remote_bda)); esp_ble_gap_update_conn_params(&conn_params); break;... case ESP_GATTS_DISCONNECT_EVT: ESP_LOGI(CONN_TAG, "Disconnected, remote "ESP_BD_ADDR_STR", reason 0x%02x", ESP_BD_ADDR_HEX(param->disconnect.remote_bda), param->disconnect.reason); esp_ble_gap_start_advertising(&adv_params); break;... default: break;... }{...} }{...}
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.