turgu1 / bt-keyboard

Other
71 stars 8 forks source link

Can't reconnect to BLE keyboard after ESP reset #4

Closed Hamberthm closed 1 year ago

Hamberthm commented 1 year ago

Hi and thanks for this amazing code!

I succesfully implemented it on my own project, a BT to PS/2 adapter.

The ESP can discover the keyboard, a Redragon Draconic, while it's in pairing mode and connect to it succesfully. However, if I cycle the power on wichever device, they wouldn't reconnect. I have to enter pairing mode again on the keyboard for it to work.

In main's code I have:

// To test the Pairing code entry, uncomment the following line as pairing info is 
// kept in the nvs. Pairing will then be required on every boot.
// ESP_ERROR_CHECK(nvs_flash_erase());

As you can see the line for the NVS flash erase is commented out,. However that doesn't seem to apply to BLE devices, as I have to do pairing each time.

Is it possible that BLE devices require the host (the ESP) to initiate a connection at all times? If that is the case, how would be the right way to implement it?

Thanks!

Hamberthm commented 1 year ago

Í'm further investigating this issue.

First of all, my keyboard won't advertise an UUID, not even an appearence flag if not in pairing mode. As a result, the scan routine totally ignores it.

The solution to this is to manually recover the bonded devices addresses from the NVS and try to connect to them as a first step. If anyone can guide me on how to do this, I would be DEEPLY appreciated as I'm a noob on this and browsing/understanding the API takes me too much valuable time ;(

Thanks!!

EDIT: I've managed to reconnect using the esp_ble_get_bond_device_num() and esp_ble_get_bond_device_list() functions to recover the last bonded device from flash!! I'll be posting a pull request soon.

Hamberthm commented 1 year ago

Well, I've successfully added the missing features. Check my fork and this commit