Automatically Reconnect w/Paired Device? esp-idf, BLE (solved)

phantomBlurrr
Posts: 19
Joined: Mon Jun 10, 2019 4:01 pm

Automatically Reconnect w/Paired Device? esp-idf, BLE (solved)

Postby phantomBlurrr » Tue Jul 16, 2019 2:45 pm

Hello,
I am wondering if it is possible to set up the ESP to automatically reconnect with a paired BLE device if the device comes back into range? I thought I saw a setting to do this but I forgot where I saw it or maybe my memory is playing tricks on me?
I am using ESP-IDF, NO Kolban, NO Arduino
Last edited by phantomBlurrr on Mon Jul 29, 2019 8:32 pm, edited 1 time in total.

phantomBlurrr
Posts: 19
Joined: Mon Jun 10, 2019 4:01 pm

Re: Automatically Reconnect w/Paired Device? esp-idf, BLE (solved?)

Postby phantomBlurrr » Mon Jul 29, 2019 8:32 pm

Hello,
It seems this is not done in the ESP firmware, it is done in the phone application code instead. I'm not sure if this is the correct way to do it or if there are more optimal ways, but here's what I did. In your Android phone application, in the line of code that starts the GATT connection from the Android side (Android Studio or whatever you're using) set it to auto-connect. The line of code looks like this:

private BluetoothGatt mGatt;

public void gattConnect(BluetoothDevice device)
{
mGatt = device.connectGatt(this, true, mGattCallback); //This triggers the callback: onConnectionStateChange
}

Where mGatt is a variable containing the GATT that is being used by the application. The second argument of connectGatt() has to be set to true and this will cause the phone application to passively keep an eye out for your ESP to broadcast the BT signal again. So if the ESP gets out of range, the phone app will lose connection, but if the ESP comes back in range, the phone app will automatically reconnect. I was able to send a disconnect command from my phone, the ESP disconnected, then the phone automatically reconnected. I was then able to send other commands without ever having to press "connect" on my phone, since the connection had already been reestablished. However, there is one bug, if I press "disconnect" in the phone application, it doesn't do anything, and I have to press it one more time before the application reacts and disconnects from the ESP. Probably a bug on my application rather than my ESP FW (I hope). Ok, BYE!

Who is online

Users browsing this forum: Bing [Bot], XHYZN_Marshall and 27 guests