Page 1 of 1

Server versus Client Connect and Disconnect Callbacks

Posted: Fri Sep 06, 2019 12:16 am
by tony@shatalmic.com
I have an Adafruit Huzzah32 device. I am writing an app that both acts as a peripheral to allow a mobile app to connect to it and at the same time is able to connect to a device to get data from it.

I have the code all working. It includes setting up server callbacks by creating my own class derived from BLEServerCallbacks that is then passed to pServer->setCallbacks(new BTServerCallbacks());

I also have a class derived from BLEClientCallbacks that is passed to pClient->setClientCallbacks(new MyClientCallback());

I am testing just having my device connect to the sensor device to gather data.

The problem I am having is that the onConnected and onDisconnected callbacks is BOTH classes gets called when I connect to and disconnect from the sensor device.

Can anyone explain how to fix this? I do certain things based on whether I am connecting/disconnecting with the sensor versus connecting/disconnecting with a mobile app to me.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Fri Sep 06, 2019 12:49 pm
by chegewara
Recently I saw few similar questions and looks like it is bug in library. I will try to check it when i have some time.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Fri Sep 06, 2019 3:13 pm
by tony@shatalmic.com
I have a little more information for you. I added a log output to both the server and client disconnect where the event is coming in and they both get fired.

If you can point me to somewhere I might be able to look to solve this I am happy to dig into it. It is rather important and I know you are busy, so anything I can do to help please let me know.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Sat Sep 07, 2019 6:45 pm
by chegewara
You can add in your code callbacks that will be triggered on every bluedroid event unchanged. This way you can try to see if original (not Kolbans library) disconnect events are triggered for gattc and gatts and see what parameters are passed, especially gattc_if/gatts_if and param->conn_id:

https://github.com/nkolban/esp32-snippe ... .h#L60-L62

With such data we can find if and how its possible to fix it.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Mon Sep 09, 2019 11:42 pm
by tony@shatalmic.com
So I added the following code:

Code: Select all

void my_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param)
{
    Serial.print("server: esp_gatts_cb_event_t ");
    Serial.print(event);
    Serial.print(" esp_gatt_if_t ");
    Serial.print(gattc_if);
    if (param == NULL)
        Serial.println("");
    else
    {
        Serial.print(" conn_id ");
        Serial.println(param->disconnect.conn_id);
    }
}

void my_gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param)
{
    Serial.print("client: esp_gattc_cb_event_t ");
    Serial.print(event);
    Serial.print(" esp_gatt_if_t ");
    Serial.print(gattc_if);
    if (param == NULL)
        Serial.println("");
    else
    {
        Serial.print(" conn_id ");
        Serial.println(param->disconnect.conn_id);
    }
}

...

    BLEDevice::setCustomGattsHandler(my_gatts_event_handler);
    BLEDevice::setCustomGattcHandler(my_gattc_event_handler);
When I disconnect the device I am connected to here is the output:

Code: Select all

server: esp_gatts_cb_event_t 15 esp_gatt_if_t 4 conn_id 0
client: esp_gattc_cb_event_t 41 esp_gatt_if_t 5 conn_id 0
client: esp_gattc_cb_event_t 5 esp_gatt_if_t 5 conn_id 0
client: esp_gattc_cb_event_t 1 esp_gatt_if_t 5
Keep in mind that even though I have the device set up to connect to an app as a peripheral, at the point this is happening I have not connected the app. My device is only connected to my device as a central.

So disconnecting should only signal the client disconnect, not the server (as best I understand this).

Please let me know if there are other things I can test to get more information.

Also one thing that I noticed is that the

Code: Select all

gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param)
seems to have the name of the esp_gatt_if parameter named wrong. Is it supposed to be gattc_if or gatts_if?

Not a big deal, just pointing it out.

Let me know what else I can do to help figure this out.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Tue Sep 10, 2019 11:37 am
by chegewara

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Tue Sep 17, 2019 8:24 pm
by tony@shatalmic.com
I just saw your reply. Not getting emails for some reason.

Anyway, I am not sure what you want me to try. You just show 2 files that are very different. What do you mean by "try to add here" "this same code"? I don't see anything highlighted to add or move from one file to the other.

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Tue Sep 17, 2019 8:37 pm
by chegewara
tony@shatalmic.com wrote:
Tue Sep 17, 2019 8:24 pm
I just saw your reply. Not getting emails for some reason.

Anyway, I am not sure what you want me to try. You just show 2 files that are very different. What do you mean by "try to add here" "this same code"? I don't see anything highlighted to add or move from one file to the other.
Hi,
Sorry it should have been link with 2 lines, 194-195:
https://github.com/nkolban/esp32-snippe ... #L194-L195

And use it here:
https://github.com/nkolban/esp32-snippe ... r.cpp#L215

Re: Server versus Client Connect and Disconnect Callbacks

Posted: Tue Sep 17, 2019 8:52 pm
by tony@shatalmic.com
Thank you. I will give that a try.