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
+ .appearance = 384 ,
27
+ };
28
+
29
+ // GAP callback
30
+ static void esp_gap_cb (esp_gap_ble_cb_event_t event , esp_ble_gap_cb_param_t * param )
31
+ {
32
+ switch (event ) {
33
+
34
+ case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT :
35
+
36
+ printf ("ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT\n" );
37
+ esp_ble_gap_start_advertising (& ble_adv_params );
38
+ break ;
39
+
40
+ case ESP_GAP_BLE_ADV_START_COMPLETE_EVT :
41
+
42
+ printf ("ESP_GAP_BLE_ADV_START_COMPLETE_EVT\n" );
43
+ if (param -> adv_start_cmpl .status == ESP_BT_STATUS_SUCCESS ) {
44
+ printf ("Advertising started\n\n" );
45
+ }
46
+ else printf ("Unable to start advertising process, error code %d\n\n" , param -> scan_start_cmpl .status );
47
+ break ;
48
+
49
+ default :
50
+
51
+ printf ("Event %d unhandled\n\n" , event );
52
+ break ;
53
+ }
54
+ }
55
+
56
+
57
+ void app_main () {
58
+
59
+ printf ("BT broadcast\n\n" );
60
+
61
+ // set components to log only errors
62
+ esp_log_level_set ("*" , ESP_LOG_ERROR );
63
+
64
+ // initialize nvs
65
+ ESP_ERROR_CHECK (nvs_flash_init ());
66
+ printf ("- NVS init ok\n" );
67
+
68
+ // release memory reserved for classic BT (not used)
69
+ ESP_ERROR_CHECK (esp_bt_controller_mem_release (ESP_BT_MODE_CLASSIC_BT ));
70
+ printf ("- Memory for classic BT released\n" );
71
+
72
+ // initialize the BT controller with the default config
73
+ esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT ();
74
+ esp_bt_controller_init (& bt_cfg );
75
+ printf ("- BT controller init ok\n" );
76
+
77
+ // enable the BT controller in BLE mode
78
+ esp_bt_controller_enable (ESP_BT_MODE_BLE );
79
+ printf ("- BT controller enabled in BLE mode\n" );
80
+
81
+ // initialize Bluedroid library
82
+ esp_bluedroid_init ();
83
+ esp_bluedroid_enable ();
84
+ printf ("- Bluedroid initialized and enabled\n" );
85
+
86
+ // register GAP callback function
87
+ ESP_ERROR_CHECK (esp_ble_gap_register_callback (esp_gap_cb ));
88
+ printf ("- GAP callback registered\n\n" );
89
+
90
+ // configure the adv data
91
+ ESP_ERROR_CHECK (esp_ble_gap_set_device_name ("ESP32_BLE" ));
92
+ ESP_ERROR_CHECK (esp_ble_gap_config_adv_data (& adv_data ));
93
+ printf ("- ADV data configured\n\n" );
94
+ }
0 commit comments