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