BLE disconnect event??

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

BLE disconnect event??

Postby Trialblazer47 » Mon Jul 31, 2017 2:09 pm

Hi,

I want to know is there any Event called in BLE when peer device disconnects or disconnects on power off or some things?
Particulary I want to know this for gattc or gatt client. Currently I am getting ESP_GATTC_CLOSE_EVENT which is called after a long time of disconnection(after a min or so).

Code: Select all

.[0;32mI (7372) GATTC_DEMO: REG FOR NOTIFY: status 0.[0m
.[0;32mI (7372) GATTC_DEMO: REG FOR_NOTIFY: srvc_id = 941e, char_id = 941e.[0m
.[0;32mI (7382) GATTC_DEMO: EVT 35, gattc if 3.[0m
.[0;31mE (7392) BT: No pending command.[0m
.[0;31mE (41982) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0008.[0m
.[0;31mE (41982) BT: btm_sec_disconnected clearing pending flag handle:0 reason:8
.[0m
.[0;32mI (41982) GATTC_DEMO: EVT 5, gattc if 3.[0m
.[0;32mI (41992) GATTC_DEMO: ESP_GATTC_CLOSE_EVENT.[0m
I want to restart scanning just after the device disconnects. So I want to know immediately that device has disconnect of lost connect.
Thanks.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE disconnect event??

Postby kolban » Mon Jul 31, 2017 3:27 pm

If a peer explicitly disconnects, I have seen that CLOSE event arrive immediately. I am guessing that if the peer doesn't disconnect but just gets lost or disappears ... then there will be a timeout before the disconnection as ping missed would be sent and not responded to.

It is likely that the time for a disconnection detection can be configured but if made too short, then you would end up with false disconnects when the response is just slow or sporadic.

If we think about TCP/IP, if a peer goes off network, it can also take a while before the connection is considered abandoned. This is the nature of connectionless protocols. (me thinks).
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Mon Jul 31, 2017 6:31 pm

kolban wrote:If a peer explicitly disconnects, I have seen that CLOSE event arrive immediately. I am guessing that if the peer doesn't disconnect but just gets lost or disappears ... then there will be a timeout before the disconnection as ping missed would be sent and not responded to.

It is likely that the time for a disconnection detection can be configured but if made too short, then you would end up with false disconnects when the response is just slow or sporadic.

If we think about TCP/IP, if a peer goes off network, it can also take a while before the connection is considered abandoned. This is the nature of connectionless protocols. (me thinks).
Thats exactly what I think well I am developing code between NRF51 and ESP32. I think I will test GATT client on NRF51 and check how fast it responds. Thanks but I would like to see some EVENT which handles disconnection.
Thanks.

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Tue Aug 01, 2017 6:50 pm

Yes it takes time for even the peer device to disconnect if I power of ESP32 while it is connected.

Another issue arised. Regarding Connection.

what I want is to start scaning and once connected stop scaning(this is done with in demo) but then After I am done with my device I want to disconnect and start scanning again. which I am able to do anyway. But once the scan duration is complete I don't get any event regarding it and so I am not able to start scanning. :( I want to keep scanning for devices until I dont get any. this should always be scanning, or stop only when connected and then restart scaning after disconnected.

here is the serial output from my code after it disconnects its starts scan but after its scan period is finished I don't get any event of it. what should be the event after finish of scan ?

Code: Select all

.[0m
.[0;32mI (48272) GATTC_DEMO: EVT 10, gattc if 3.[0m
.[0;32mI (48272) GATTC_DEMO: NOTIFY: len 3, value 00646e65.[0m
.[0;32mI (48272) BLE_CMD: Commander Here. 
.[0m
.[0;32mI (48282) BLE_CMD: data: 101   e .[0m								<---my device sends this to indicate end of sending
.[0;32mI (48282) BLE_CMD: data: 110   n .[0m
.[0;32mI (48282) BLE_CMD: data: 100   d .[0m
.[0;32mI (48292) BLE_CMD: Finished Data Tansfer.[0m						<-- now here I disconnect with in ESP32
.[0;31mE (48652) BT: gatt_disc_cmpl_cback() - Register for service changed indication failure.[0m
.[0;31mE (48652) BT: bta_gattc_conn_cback() - cif=3 connected=0 conn_id=3 reason=0x0016.[0m
.[0;32mI (48652) GATTC_DEMO: EVT 5, gattc if 3.[0m
.[0;32mI (48662) GATTC_DEMO: ESP_GATTC_CLOSE_EVENT.[0m	
.[0;32mI (48662) DEV: Disconnected.[0m
.[0;32mI (48852) GATTC_DEMO: GAP CB EVT:  7.[0m							<-- this is SCAN_START_COMPLETE_EVT
.[0;31mE (50272) BT: btm_sec_disconnected clearing pending flag handle:0 reason:22
.[0m
.[0;32mI (50272) GATTC_DEMO: GAP CB EVT:  8.[0m							<--ESP_GAP_BLE_AUTH_CMPL_EVT (NRF51 has just works auth)
.[0;32mI (50272) GATTC_DEMO: GAP CB EVT:  8.[0m
.[0;31mE (78852) BT: btc_search_callback  BLE observe complete. Num Resp 1		<-- I think its end of Scan duration and it stops
.[0m																
.[0;32mI (78852) GATTC_DEMO: GAP CB EVT:  3.[0m
Thanks.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE disconnect event??

Postby kolban » Tue Aug 01, 2017 7:35 pm

It seems to me that if we start a scan at time T for d seconds then the scan will be completed at T+d and we don't need an event to tell us that. If you need an asynchronous indication, then you could create a task that delays "d" and then indicates.

If you explicitly request that scanning stop, then you do receive an event (ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT).

I believe a major part of the philosophy of BLE is to minimize energy consumption. This would seem to say that we would want to avoid continuously scanning for new BLE devices. Are you 100% sure that you want to scan 100% of the time when not connected? If you are, could you not set the scan interval to be MAXINT and then explicitly stop scanning when you find what you want?

For my curiosity, would you be able to describe your project so that I may better appreciate the idea of continually scanning?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Wed Aug 02, 2017 9:51 am

Yes I understand that, but I am making something like a hub for BLE devices and it will be powered. those devices might advertise DATA at any time, Although those devices have the capabilities to store data if it fails to connect to hub. What do you think should the approach be ?
A sync time when devices come to relay data ? that may cause problem as(no Real time, all devices might advertise togather and only one will be served)
I will keep a task that flags after certain time and restart scan.
But I believe there should be an even when scanning is finished.
Thanks.

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Wed Aug 23, 2017 12:31 pm

is there anyway we can know scanning is done? I tried with a task that starts scanning after scan interval. but adding that task breaks the app.

Code: Select all

void ticker(void *params)
{

  vTaskDelay( (Scanning_duration+5)*1000 / portTICK_RATE_MS);
  ESP_LOGI(GATTC_TAG,"Ticker");
//  dev.scanning=false;
}
void app_main()
{

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    esp_bt_controller_init(&bt_cfg);
    esp_bt_controller_enable(ESP_BT_MODE_BTDM);

    dev_init();//Bytelens init dev struct.



    xTaskCreate(&task_gpio_handler,"Task_IO",1024,NULL,1,NULL);

    gattc_client_test();
    vTaskDelay( 200 / portTICK_RATE_MS );
    //  dev.scanning=true;
    //  esp_ble_gap_start_scanning(Scanning_duration);
    xTaskCreate(&bytelens_task,"Task_BLE",2048,NULL,2,NULL);
    xTaskCreate(&ticker,"Task_ticker",1024,NULL,3,NULL);
    gpioController.LED_Control = color_white;
}

and this is the error

Code: Select all

[0;32mI (30502) GATTC_DEMO: GAP CB EVT:  3.[0m
.[0;32mI (30502) GATTC_DEMO: Searched Device Name Len 0.[0m
.[0;31mE (30562) BT: btc_search_callback  BLE observe complete. Num Resp 3
.[0m
.[0;32mI (30562) GATTC_DEMO: GAP CB EVT:  3.[0m
.[0;32mI (35742) GATTC_DEMO: Ticker.[0m
Guru Meditation Error of type IllegalInstruction occurred on core  0. Exception was unhandled.
Register dump:
PC      : 0x400dde54  PS      : 0x00060830  A0      : 0x00000000  A1      : 0x3ffd58a0  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00000000  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x800dde54  A9      : 0x3ffd5850  
A10     : 0x400d6460  A11     : 0x3ffc3248  A12     : 0x3f402f10  A13     : 0x0000001f  
A14     : 0x00000001  A15     : 0x00000005  SAR     : 0x00000004  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffd  

Backtrace: 0x400dde54:0x3ffd58a0 0x00000000:0x3ffd58c0

Rebooting...
Thanks.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: BLE disconnect event??

Postby kolban » Wed Aug 23, 2017 12:50 pm

Howdy my friend,
My guess is that your puzzle isn't related to BLE but rather to task management. When you create a FreeRTOS task, it is essential that when the task ends, it deletes itself. In your code you have:

Code: Select all

void ticker(void *params) {
   // do something
}
This is (I believe) an error because when the ticker() function ends, you haven't deleted the task. Can you try the following:

Code: Select all

void ticker(void *params) {
   // do something
   vTaskDelete(NULL);
}
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Wed Aug 23, 2017 1:04 pm

thanks for quick reply. I don't know How I missed that ?

anyways I want to keep that task to periodically check flags(that device is connected to other BLE device, or scanning is on ) if not than start scanning again.

Code: Select all

void ticker(void *params)
{
while(1){
    vTaskDelay( (Scanning_duration+5)*1000 / portTICK_RATE_MS); 
    ESP_LOGI(GATTC_TAG,"Ticker");
    if(!dev.Connected && !dev.notify && !dev.scanning) 
      {
        dev.scanning=true;
        esp_ble_gap_start_scanning(Scanning_duration);
      }
  }
}
however I am not able to reset the flag for scanning when it stops because its does not give any callback event as I mentioned before .. is there any function to check scanning state???
Thanks.

Trialblazer47
Posts: 60
Joined: Mon Jun 26, 2017 5:36 am

Re: BLE disconnect event??

Postby Trialblazer47 » Wed Aug 23, 2017 1:22 pm

later the issue was this

Code: Select all

[0;31mE (30562) BT: btc_search_callback  BLE observe complete. Num Resp 5
.[0m
.[0;32mI (30562) GATTC_DEMO: GAP CB EVT:  3.[0m
.[0;32mI (35742) GATTC_DEMO: Ticker.[0m
***ERROR*** A stack overflow in task Task_ticker has been detected.
abort() was called at PCGuru Meditation Error of type LoadProhibited occurred on core  1. Exception was unhandled.
Register dump:
PC      : 0x400856ad  PS      : 0x00060033  A0      : 0x40086a7b  A1      : 0x3ffcc250  
A2      : 0x00050023  A3      : 0x00000001  A4      : 0x3ffc300c  A5      : 0x3ffc300c  
A6      : 0x00000001  A7      : 0x00060420  A8      : 0x00000002  A9      : 0x00000002  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3ffc303c  A13     : 0x00060220  
A14     : 0x00000000  A15     : 0x00000001  SAR     : 0x0000001c  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000048  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  


now increasing stack size and issue is gone. But this isn't good I am not sure what to size would be optimal.. next I am going to put MQTT Client and I would require more RAM, this way I will lose lot of ram..


Also on completion of scanning ESP_GAP_SEARCH_INQ_CMPL_EVT event is generated which resolved my other problem of knowing scanning is complete.
thanks. you Rock. Have a great day.
Thanks.

Who is online

Users browsing this forum: pcouderc, tahseenabbas52 and 109 guests