Best strategy to reconnect and connect directly at power on ESP32

968 views
Skip to first unread message

Fernando Olmeda Arcos

unread,
Feb 25, 2021, 1:39:37 PM2/25/21
to btstack-dev
After capable of sending text over bluetooth when receiving RS232 text the last thing that my client ask for is reconnect in case lost connection because the machine which will use the ESP32, cut off electric power often, every time the driver leave the crane.

I don't remember  / don't know if PDA is a STDIN device.

So my idea is when the ESP32 is being power it  automatically connect with the HID device which is near ESP32.

Another security reconnection is make inside typing_timer_handler a for bucle and when time variable arrives a 5000 for example, check app_state, if APP_NOT_CONNECTED then hid_device_connect().
If APP_CONNECTED break.

Is a good strategy? Or ther is another one much better than mine.




Matthias Ringwald

unread,
Feb 26, 2021, 4:08:32 AM2/26/21
to btsta...@googlegroups.com
Hi Fernando

For Bluetooth HID Keyboards, the common strategy is to try to connect when it's turned on/someone types. e.g. the Apple keyboard I'm tying right now connects to the device it was last used (my MacBook) when I start typing.

For that, you need to store the Bluetooth address of the remote device. You can use BTstack's TLV for that. It's used in the hog_boot_host_demo (https://github.com/bluekitchen/btstack/blob/master/example/hog_boot_host_demo.c#L309)

Cheers
Matthias
> --
> You received this message because you are subscribed to the Google Groups "btstack-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to btstack-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/67f5923d-565d-444b-b3f8-e1255cdc8cecn%40googlegroups.com.

Fernando Olmeda Arcos

unread,
Feb 26, 2021, 8:02:14 AM2/26/21
to btstack-dev
Oh thank you, I going to read this example and how to implement in hid_keyboard_demo.

Because it just seems like you need to implement the TLV part

Fernando Olmeda Arcos

unread,
Feb 27, 2021, 5:27:06 AM2/27/21
to btstack-dev
TLV is for Bluetooth Classic Devices too? Or only for BLE devices. Yesterday I used TLV to detect one Bluetooth device and  when it was paired, 00:00:00:00 address was detected and stored, then when I booted up ESP32 it wasn't connected at boot up with this 00:00:00:00 address and device automatically.

This afternoon I'm going to try with my mobile phone.
El viernes, 26 de febrero de 2021 a las 10:08:32 UTC+1, Matthias Ringwald escribió:

Matthias Ringwald

unread,
Mar 1, 2021, 3:50:38 AM3/1/21
to btsta...@googlegroups.com
Hi Fernando

BTstack's TLV is a generic persistence storage, similar to the nvs_flash of the esp-idf. While it is used by BTstack to store both Classic Link keys as well as LE device information, it can be be used by applications as well. Here's an overview/blog post about it: https://bluekitchen-gmbh.com/storing-link-keys-in-flash-memory/

Bluetooth devices usually don't have all zeroes (00:00:00:00:00:00) as their address, e.g. iPhones ignore LE devices with that address.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/0918ac04-2fb9-4e24-a1ef-c8dbb21c6843n%40googlegroups.com.

Fernando Olmeda Arcos

unread,
Mar 1, 2021, 4:10:34 AM3/1/21
to btstack-dev
Hi Matthias,

This weekend I was not capable of reconnect. I get all code about TLV of hog_boot_host_demo.c and put in my project, but I can't reconnect my mobile phone for example.

First I put this code

  app_state = APP_CONNECTED;
  hid_cid = hid_subevent_connection_opened_get_hid_cid(packet);                        
  printf("HID Connected, sending demo text...\n");
 // store device as bonded
   if (btstack_tlv_singleton_impl){
        btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, TLV_TAG_HOGD, (const uint8_t *) &remote_device, sizeof(remote_device));
   }
   // done
  hid_embedded_start_typing();
  break;

And my project works as I would like.

Then to reconnect wiht bonded device

 case BTSTACK_EVENT_STATE:
     if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
     app_state = APP_NOT_CONNECTED;
     hid_start_connect();
     break;

And of course all functions related to hid_start_connect(); as hid_connect, timeout.... I change hog for hid.

But I get a timeout and never reconnect with my mobile phone at boot up.
Timeout - abort connection
Scanning for HID devices...
[00:00:11.259] LOG -- hci.c.2192: LE Connection_complete (status=2) type 0, 00:00:00:00:00:00

Matthias Ringwald

unread,
Mar 1, 2021, 9:10:36 AM3/1/21
to btsta...@googlegroups.com
Hi Fernando

Could you post the complete file somewhere?

Two things:
- In the code to store the remote address, the address is not extracted from the HID_SUBEVENT_CONNECTION_OPENED with hid_subevent_connection_opened_get_bd_addr(..)
- In the changes below, the remote device address is not read from TLV.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/7af9d22a-f963-423b-acb9-a94edb6dd0c2n%40googlegroups.com.

Fernando Olmeda Arcos

unread,
Mar 1, 2021, 10:46:47 AM3/1/21
to btstack-dev
Hi


I think if you go to this webpage you can read my code in send_text.c

Main.c file is not modified.

main.c

#include "btstack_port_esp32.h"
#include "btstack_run_loop.h"
#include "hci_dump.h"
#include <stdio.h>

#include <stddef.h>

extern int btstack_main(int argc, const char * argv[]);

int app_main(void){

    // optional: enable packet logger
    // hci_dump_open(NULL, HCI_DUMP_STDOUT);

    // Configure BTstack for ESP32 VHCI Controller
    btstack_init();

    // Setup example
    btstack_main(0, NULL);

    // Enter run loop (forever)
    btstack_run_loop_execute();

    return 0;
}

Matthias Ringwald

unread,
Mar 2, 2021, 3:39:48 AM3/2/21
to btsta...@googlegroups.com
Hi Fernando

Quick check at your code.

- when you get HID_SUBEVENT_CONNECTION_OPENED event, you need to use the correct getter, in this case: hid_subevent_connection_opened_get_bd_addr to get the Bluetooth address
- As this is Bluetooth Classic, it will be an Classic ACL connection (not an LE Public or Random)
- To create an outgoing Classic HID connection, you can call hid_connect(...) - from src/classic/hid_device.h

For Classic connections, there will be a Page Timout, so you don't need to setup an application level timer to stop connecting.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/a5fa4403-8db2-42f3-9fd5-1b469bd213f1n%40googlegroups.com.

Fernando Olmeda Arcos

unread,
Mar 2, 2021, 6:22:08 AM3/2/21
to btstack-dev
Hi Matthias

With your indications I can reconnect with my mobile phone at boot up. But sometimes I receive  (Connection failed, status 0xb) I think is because device is yet "connected" and I try to reconnect, because If I try to reconnect after a while, all works like a charm.

If I receive this message, Can I hid_device_disconnect() before try to reconnect again? Or I need to wait until BT device disconnect after a while because ESP32 has been disconnected

Now I try to reconnect and wait for a long time to see what happens

Matthias Ringwald

unread,
Mar 3, 2021, 8:54:12 AM3/3/21
to btsta...@googlegroups.com
Hi Fernando

The error codes are defined in src/bluetooth.h. 0x11 is 'ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS'. That's a bit unexpected after power up.

Could you enable packet logging in main.c - uncomment the call to hci_dump_open(..) - make sure ENABLE_LOG_INFO is active and post the complete output for a) pairing, b) reconnect?

In general, if you get the error with the HID_SUBEVENT_CONNECTION_OPENED, you could set a timer and retry again, e.g. after 10 seconds. You don't need to call hic_device_disconnect as the connection has not been established then.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/1e4ac6b7-00fc-4b36-a911-a57e439332b2n%40googlegroups.com.

Fernando Olmeda Arcos

unread,
Mar 5, 2021, 1:39:26 AM3/5/21
to btstack-dev
Hi Matthias,

Sorry for my delay, I was working out of my office for three days.

After some tests I realized that when I switched off my ESP32 my mobile phone yet turned up as connected, so if I powered ESP32 on I received this error. But if I turned on ESP32 on after mobile phone appeared as disconnected, then I could connect perfectly.

So I made a timer like your answered me. By now all works like a charm in my test zone, now, next week I try on it in client house.

Cross my fingers.

Fernando Olmeda Arcos

unread,
Mar 5, 2021, 3:21:25 AM3/5/21
to btstack-dev
by the way, if anyone needs help about btstack or rs232 please don't hesitate to ask.

If I can, I help you.

And Of course, many many many many (thousand thanks) to Matthias for his help.

Many days thinking about what I could do and desesperated some times, but Matthias lighted me with some ideas.

Sorry for my English.

Александр Свешников

unread,
Dec 7, 2021, 5:31:07 PM12/7/21
to btstack-dev
Hello, 
I implemented "reconnect on bootup" in my project using this  discussion, so thanks a lot!
But sometimes (and often) if my device trying to reconnect, it receives connection status 0x66 (that means "L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY" ). Any suggestions?

пятница, 5 марта 2021 г. в 11:21:25 UTC+3, folmed...@gmail.com:

Matthias Ringwald

unread,
Dec 9, 2021, 3:32:23 AM12/9/21
to btsta...@googlegroups.com
Hi Александр

Could you share HCI packet logs for the reconnect? Anything special there?
Also, please try to wait a few seconds before reconnect just to know if that makes a difference.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/4d87e671-90f4-4b98-9985-4bc9b58ec6bbn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages