@@ -235,26 +235,34 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
235235 break ;
236236
237237 case ESP_SPP_SRV_OPEN_EVT:// Server connection open
238- log_i (" ESP_SPP_SRV_OPEN_EVT" );
239- if (!_spp_client){
240- _spp_client = param->open .handle ;
238+ if (param->srv_open .status == ESP_SPP_SUCCESS) {
239+ log_i (" ESP_SPP_SRV_OPEN_EVT" );
240+ if (!_spp_client){
241+ _spp_client = param->srv_open .handle ;
242+ } else {
243+ secondConnectionAttempt = true ;
244+ esp_spp_disconnect (param->srv_open .handle );
245+ }
246+ xEventGroupClearBits (_spp_event_group, SPP_DISCONNECTED);
247+ xEventGroupSetBits (_spp_event_group, SPP_CONNECTED);
241248 } else {
242- secondConnectionAttempt = true ;
243- esp_spp_disconnect (param->open .handle );
249+ log_e (" ESP_SPP_SRV_OPEN_EVT Failed!, status:%d" , param->srv_open .status );
244250 }
245- xEventGroupClearBits (_spp_event_group, SPP_DISCONNECTED);
246- xEventGroupSetBits (_spp_event_group, SPP_CONNECTED);
247251 break ;
248252
249253 case ESP_SPP_CLOSE_EVT:// Client connection closed
250- log_i (" ESP_SPP_CLOSE_EVT" );
251- if (secondConnectionAttempt) {
252- secondConnectionAttempt = false ;
254+ if ((param->close .async == false && param->close .status == ESP_SPP_SUCCESS) || param->close .async ) {
255+ log_i (" ESP_SPP_CLOSE_EVT" );
256+ if (secondConnectionAttempt) {
257+ secondConnectionAttempt = false ;
258+ } else {
259+ _spp_client = 0 ;
260+ xEventGroupSetBits (_spp_event_group, SPP_DISCONNECTED);
261+ }
262+ xEventGroupClearBits (_spp_event_group, SPP_CONNECTED);
253263 } else {
254- _spp_client = 0 ;
255- xEventGroupSetBits (_spp_event_group, SPP_DISCONNECTED);
256- }
257- xEventGroupClearBits (_spp_event_group, SPP_CONNECTED);
264+ log_e (" ESP_SPP_CLOSE_EVT failed!, status:%d" , param->close .status );
265+ }
258266 break ;
259267
260268 case ESP_SPP_CONG_EVT:// connection congestion status changed
@@ -267,11 +275,15 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
267275 break ;
268276
269277 case ESP_SPP_WRITE_EVT:// write operation completed
270- if (param->write .cong ){
271- xEventGroupClearBits (_spp_event_group, SPP_CONGESTED);
278+ if (param->write .status == ESP_SPP_SUCCESS) {
279+ if (param->write .cong ){
280+ xEventGroupClearBits (_spp_event_group, SPP_CONGESTED);
281+ }
282+ xSemaphoreGive (_spp_tx_done);// we can try to send another packet
283+ log_v (" ESP_SPP_WRITE_EVT: %u %s" , param->write .len , param->write .cong ?" CONGESTED" :" " );
284+ } else {
285+ log_e (" ESP_SPP_WRITE_EVT failed!, status:%d" , param->write .status );
272286 }
273- xSemaphoreGive (_spp_tx_done);// we can try to send another packet
274- log_v (" ESP_SPP_WRITE_EVT: %u %s" , param->write .len , param->write .cong ?" CONGESTED" :" FREE" );
275287 break ;
276288
277289 case ESP_SPP_DATA_IND_EVT:// connection received data
@@ -296,6 +308,8 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
296308 if (param->disc_comp .status == ESP_SPP_SUCCESS) {
297309 log_i (" ESP_SPP_DISCOVERY_COMP_EVT: spp connect to remote" );
298310 esp_spp_connect (ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, param->disc_comp .scn [0 ], _peer_bd_addr);
311+ } else {
312+ log_e (" ESP_SPP_DISCOVERY_COMP_EVT failed!, status:%d" , param->disc_comp .status );
299313 }
300314 break ;
301315
@@ -532,9 +546,9 @@ static bool _init_bt(const char *deviceName)
532546 return false ;
533547 }
534548
535- if (esp_bt_sleep_disable () != ESP_OK){
536- log_e (" esp_bt_sleep_disable failed" );
537- }
549+ // if (esp_bt_sleep_disable() != ESP_OK){
550+ // log_e("esp_bt_sleep_disable failed");
551+ // }
538552
539553 log_i (" device name set" );
540554 esp_bt_dev_set_device_name (deviceName);
0 commit comments