1919#define BLE_APP_TAG "ble_app"
2020#define BLE_GAP_TAG "ble_gap"
2121
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-
2722esp_ble_adv_params_t adv_params = {
2823 .adv_int_min = 0x20 ,
2924 .adv_int_max = 0x40 ,
@@ -33,47 +28,11 @@ esp_ble_adv_params_t adv_params = {
3328 .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY ,
3429};
3530
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-
6331static void ble_gap_event_handler (esp_gap_ble_cb_event_t event , esp_ble_gap_cb_param_t * param )
6432{
6533 switch (event ) {
6634 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 );
7736 break ;
7837 case ESP_GAP_BLE_ADV_START_COMPLETE_EVT :
7938 // 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
9554 }
9655}
9756
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+
9878void ble_app_init (void )
9979{
10080 xEventGroupSetBits (user_event_group , BLE_GATTS_IDLE_BIT );
10181
102- ESP_ERROR_CHECK (esp_ble_gatts_register_callback (ble_gatts_event_handler ));
10382 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 ));
10484
85+ ESP_ERROR_CHECK (esp_ble_gatts_register_callback (ble_gatts_event_handler ));
10586 ESP_ERROR_CHECK (esp_ble_gatts_app_register (PROFILE_IDX_OTA ));
10687 ESP_ERROR_CHECK (esp_ble_gatts_app_register (PROFILE_IDX_VFX ));
10788
10889 ESP_ERROR_CHECK (esp_ble_gatt_set_local_mtu (ESP_GATT_MAX_MTU_SIZE ));
10990
91+ gap_config_adv_data (CONFIG_BT_NAME );
92+
11093 ESP_LOGI (BLE_APP_TAG , "started." );
11194}
0 commit comments