Bulfi: Check if ble is connected before sending a message via ble. Fix the wrong wifi storage mode from WIFI_STORAGE_RAM to default WIFI_STORAGE_FLASH.

This commit is contained in:
gengyuchao 2020-02-20 18:11:55 +08:00
parent ca1685f7a2
commit 1aee0a0357

View file

@ -86,6 +86,7 @@ const int CONNECTED_BIT = BIT0;
/* store the station info for send back to phone */ /* store the station info for send back to phone */
static bool gl_sta_connected = false; static bool gl_sta_connected = false;
static bool ble_is_connected = false;
static uint8_t gl_sta_bssid[6]; static uint8_t gl_sta_bssid[6];
static uint8_t gl_sta_ssid[32]; static uint8_t gl_sta_ssid[32];
static int gl_sta_ssid_len; static int gl_sta_ssid_len;
@ -112,7 +113,11 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event)
info.sta_bssid_set = true; info.sta_bssid_set = true;
info.sta_ssid = gl_sta_ssid; info.sta_ssid = gl_sta_ssid;
info.sta_ssid_len = gl_sta_ssid_len; info.sta_ssid_len = gl_sta_ssid_len;
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info); if (ble_is_connected == true) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info);
} else {
BLUFI_INFO("BLUFI BLE is not connected yet\n");
}
break; break;
} }
case SYSTEM_EVENT_STA_CONNECTED: case SYSTEM_EVENT_STA_CONNECTED:
@ -135,10 +140,14 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event)
esp_wifi_get_mode(&mode); esp_wifi_get_mode(&mode);
/* TODO: get config or information of softap, then set to report extra_info */ /* TODO: get config or information of softap, then set to report extra_info */
if (gl_sta_connected) { if (ble_is_connected == true) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL); if (gl_sta_connected) {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, NULL);
} else {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL);
}
} else { } else {
esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL); BLUFI_INFO("BLUFI BLE is not connected yet\n");
} }
break; break;
case SYSTEM_EVENT_SCAN_DONE: { case SYSTEM_EVENT_SCAN_DONE: {
@ -167,7 +176,13 @@ static esp_err_t example_net_event_handler(void *ctx, system_event_t *event)
blufi_ap_list[i].rssi = ap_list[i].rssi; blufi_ap_list[i].rssi = ap_list[i].rssi;
memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid)); memcpy(blufi_ap_list[i].ssid, ap_list[i].ssid, sizeof(ap_list[i].ssid));
} }
esp_blufi_send_wifi_list(apCount, blufi_ap_list);
if (ble_is_connected == true) {
esp_blufi_send_wifi_list(apCount, blufi_ap_list);
} else {
BLUFI_INFO("BLUFI BLE is not connected yet\n");
}
esp_wifi_scan_stop(); esp_wifi_scan_stop();
free(ap_list); free(ap_list);
free(blufi_ap_list); free(blufi_ap_list);
@ -186,7 +201,6 @@ static void initialise_wifi(void)
ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) ); ESP_ERROR_CHECK( esp_event_loop_init(example_net_event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() ); ESP_ERROR_CHECK( esp_wifi_start() );
} }
@ -215,6 +229,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
break; break;
case ESP_BLUFI_EVENT_BLE_CONNECT: case ESP_BLUFI_EVENT_BLE_CONNECT:
BLUFI_INFO("BLUFI ble connect\n"); BLUFI_INFO("BLUFI ble connect\n");
ble_is_connected = true;
server_if = param->connect.server_if; server_if = param->connect.server_if;
conn_id = param->connect.conn_id; conn_id = param->connect.conn_id;
esp_ble_gap_stop_advertising(); esp_ble_gap_stop_advertising();
@ -222,6 +237,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
break; break;
case ESP_BLUFI_EVENT_BLE_DISCONNECT: case ESP_BLUFI_EVENT_BLE_DISCONNECT:
BLUFI_INFO("BLUFI ble disconnect\n"); BLUFI_INFO("BLUFI ble disconnect\n");
ble_is_connected = false;
blufi_security_deinit(); blufi_security_deinit();
esp_ble_gap_start_advertising(&example_adv_params); esp_ble_gap_start_advertising(&example_adv_params);
break; break;
@ -251,7 +267,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
esp_wifi_get_mode(&mode); esp_wifi_get_mode(&mode);
if (gl_sta_connected ) { if (gl_sta_connected) {
memset(&info, 0, sizeof(esp_blufi_extra_info_t)); memset(&info, 0, sizeof(esp_blufi_extra_info_t));
memcpy(info.sta_bssid, gl_sta_bssid, 6); memcpy(info.sta_bssid, gl_sta_bssid, 6);
info.sta_bssid_set = true; info.sta_bssid_set = true;