19
19
#define BLE_APP_TAG "ble_app"
20
20
#define BLE_GAP_TAG "ble_gap"
21
21
22
- static uint8_t adv_config_done = 0 ;
23
-
24
- #define adv_config_flag (1 << 0)
25
- #define scan_rsp_config_flag (1 << 1)
26
-
27
22
esp_ble_adv_params_t adv_params = {
28
23
.adv_int_min = 0x20 ,
29
24
.adv_int_max = 0x40 ,
@@ -33,47 +28,11 @@ esp_ble_adv_params_t adv_params = {
33
28
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY ,
34
29
};
35
30
36
- void ble_gap_init_adv_data (const char * name )
37
- {
38
- int len = strlen (name );
39
- uint8_t raw_adv_data [len + 5 ];
40
- // flag
41
- raw_adv_data [0 ] = 2 ;
42
- raw_adv_data [1 ] = ESP_BT_EIR_TYPE_FLAGS ;
43
- raw_adv_data [2 ] = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT );
44
- // adv name
45
- raw_adv_data [3 ] = len + 1 ;
46
- raw_adv_data [4 ] = ESP_BLE_AD_TYPE_NAME_CMPL ;
47
- for (int i = 0 ; i < len ; i ++ ) {
48
- raw_adv_data [i + 5 ] = * (name ++ );
49
- }
50
- // the length of adv data must be less than 31 bytes
51
- esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw (raw_adv_data , sizeof (raw_adv_data ));
52
- if (raw_adv_ret ) {
53
- ESP_LOGE (BLE_GAP_TAG , "config raw adv data failed, error code = 0x%x " , raw_adv_ret );
54
- }
55
- adv_config_done |= adv_config_flag ;
56
- esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw (raw_adv_data , sizeof (raw_adv_data ));
57
- if (raw_scan_ret ) {
58
- ESP_LOGE (BLE_GAP_TAG , "config raw scan rsp data failed, error code = 0x%x" , raw_scan_ret );
59
- }
60
- adv_config_done |= scan_rsp_config_flag ;
61
- }
62
-
63
31
static void ble_gap_event_handler (esp_gap_ble_cb_event_t event , esp_ble_gap_cb_param_t * param )
64
32
{
65
33
switch (event ) {
66
34
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT :
67
- adv_config_done &= (~adv_config_flag );
68
- if (adv_config_done == 0 ) {
69
- esp_ble_gap_start_advertising (& adv_params );
70
- }
71
- break ;
72
- case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT :
73
- adv_config_done &= (~scan_rsp_config_flag );
74
- if (adv_config_done == 0 ) {
75
- esp_ble_gap_start_advertising (& adv_params );
76
- }
35
+ esp_ble_gap_start_advertising (& adv_params );
77
36
break ;
78
37
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT :
79
38
// advertising start complete event to indicate advertising start successfully or failed
@@ -95,17 +54,41 @@ static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p
95
54
}
96
55
}
97
56
57
+ static void gap_config_adv_data (const char * name )
58
+ {
59
+ size_t len = strlen (name );
60
+ uint8_t raw_adv_data [len + 5 ];
61
+
62
+ // flag
63
+ raw_adv_data [0 ] = 2 ;
64
+ raw_adv_data [1 ] = ESP_BT_EIR_TYPE_FLAGS ;
65
+ raw_adv_data [2 ] = ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT ;
66
+
67
+ // adv name
68
+ raw_adv_data [3 ] = len + 1 ;
69
+ raw_adv_data [4 ] = ESP_BLE_AD_TYPE_NAME_CMPL ;
70
+ memcpy (raw_adv_data + 5 , name , len );
71
+
72
+ esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw (raw_adv_data , sizeof (raw_adv_data ));
73
+ if (raw_adv_ret ) {
74
+ ESP_LOGE (BLE_GAP_TAG , "config raw adv data failed, error code = 0x%x " , raw_adv_ret );
75
+ }
76
+ }
77
+
98
78
void ble_app_init (void )
99
79
{
100
80
xEventGroupSetBits (user_event_group , BLE_GATTS_IDLE_BIT );
101
81
102
- ESP_ERROR_CHECK (esp_ble_gatts_register_callback (ble_gatts_event_handler ));
103
82
ESP_ERROR_CHECK (esp_ble_gap_register_callback (ble_gap_event_handler ));
83
+ ESP_ERROR_CHECK (esp_ble_gap_set_device_name (CONFIG_BT_NAME ));
104
84
85
+ ESP_ERROR_CHECK (esp_ble_gatts_register_callback (ble_gatts_event_handler ));
105
86
ESP_ERROR_CHECK (esp_ble_gatts_app_register (PROFILE_IDX_OTA ));
106
87
ESP_ERROR_CHECK (esp_ble_gatts_app_register (PROFILE_IDX_VFX ));
107
88
108
89
ESP_ERROR_CHECK (esp_ble_gatt_set_local_mtu (ESP_GATT_MAX_MTU_SIZE ));
109
90
91
+ gap_config_adv_data (CONFIG_BT_NAME );
92
+
110
93
ESP_LOGI (BLE_APP_TAG , "started." );
111
94
}
0 commit comments