Is BLE client pairing / bonding possible?

Hi, I have a few BLE window sensors that I was hoping to use with a Sonoff DUALR3 (as it has an ESP32) to inhibit a roller shutter from closing if the window was open.

The device requires you to press on a reset button to pair the device, when I do this I can see in the logs that it is being discovered but after the 30s pairing cycle finishes it disconnects. I was hoping that enabling a BLE Client Sensor component with the notify option would pair the device but it does not. Does anyone have any advice on this?

The ESPhome docs for the BLE client say This component does not (yet) support devices that require security settings (eg connecting with a PIN). but this device doesn’t have a pin but looks like it just requires some pairing/bonding to happen.

Here is the log for when the device connects if useful?

[23:08:33][I][ble_client:083]: Attempting BLE connection to xx:xx:xx:xx:xx:xx
[23:08:34][I][ble_sensor:033]: [Notifiy] Connected successfully!
[23:08:35][I][ble_client:159]: Service UUID: 0x1800
[23:08:35][I][ble_client:160]:   start_handle: 0x1  end_handle: 0x7
[23:08:35][I][ble_client:339]:  characteristic 0x2A00, handle 0x3, properties 0x12
[23:08:35][I][ble_client:339]:  characteristic 0x2A01, handle 0x5, properties 0x2
[23:08:35][I][ble_client:339]:  characteristic 0x2A04, handle 0x7, properties 0x2
[23:08:35][I][ble_client:159]: Service UUID: 0x180A
[23:08:35][I][ble_client:160]:   start_handle: 0x8  end_handle: 0xc
[23:08:35][I][ble_client:339]:  characteristic 0x2A50, handle 0xa, properties 0x2
[23:08:35][I][ble_client:339]:  characteristic 0x2A26, handle 0xc, properties 0x2
[23:08:35][I][ble_client:159]: Service UUID: 00010203040506-0708-090A-0B0C-0D1912
[23:08:35][I][ble_client:160]:   start_handle: 0xd  end_handle: 0x10
[23:08:35][I][ble_client:339]:  characteristic 00010203040506-0708-090A-0B0C-0D2B12, handle 0xf, properties 0x6
[23:08:35][I][ble_client:159]: Service UUID: 0x1827
[23:08:35][I][ble_client:160]:   start_handle: 0x11  end_handle: 0x19
[23:08:35][I][ble_client:339]:  characteristic 0x2ADC, handle 0x13, properties 0x10
[23:08:35][I][ble_client:339]:  characteristic 0x2ADB, handle 0x17, properties 0x4
[23:08:35][I][ble_client:159]: Service UUID: 0x7FDD
[23:08:35][I][ble_client:160]:   start_handle: 0x1a  end_handle: 0x22
[23:08:35][I][ble_client:339]:  characteristic 0x2ADE, handle 0x1c, properties 0x10
[23:08:35][I][ble_client:339]:  characteristic 0x2ADD, handle 0x20, properties 0x4
[23:08:35][I][ble_client:159]: Service UUID: 00010203040506-0708-090A-0B0C-0D7FDE
[23:08:35][I][ble_client:160]:   start_handle: 0x23  end_handle: 0x26
[23:08:35][I][ble_client:339]:  characteristic 00010203040506-0708-090A-0B0C-0D7FDF, handle 0x25, properties 0x36
[23:08:35][I][ble_client:159]: Service UUID: 0x1801
[23:08:35][I][ble_client:160]:   start_handle: 0x27  end_handle: 0x2b
[23:08:35][I][ble_client:339]:  characteristic 0x2A05, handle 0x29, properties 0x20
[23:08:35][I][ble_client:159]: Service UUID: 0x1910
[23:08:35][I][ble_client:160]:   start_handle: 0x2c  end_handle: 0x34
[23:08:35][I][ble_client:339]:  characteristic 0x2B10, handle 0x2e, properties 0x10
[23:08:35][I][ble_client:339]:  characteristic 0x2B11, handle 0x32, properties 0x4

I think Service 0x1801 is the contact, but don’t think I can check until it pairs correctly.

After some further reading it would look like even tho there is no pin, pairing authentication / key exchange would need to happen. So until the component supports SMP (Security Manager Protocol) i don’t think i will be able to pair them.

FYI the contacts are from Tuya, and contain a BT5S module

Did you find another solution for this? I also have a device that needs to bond in order to send commands to it.

Unfortunately not, they are still sitting in my draw. I did come across a issue in the esphome github that if resolved might have helped with this. I will try and find it again and see if there has been any progress.

1 Like

I do not know if this thread is dead or not. But I spend a lot of time to find solution for bonding ESP32 with another device. In my case I found the solution:

void YourClass::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) {
  ... if (event == ESP_GATTC_CONNECT_EVT) {
  esp_ble_set_encryption(param->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
  } ...
}
1 Like

@dentra Thanks for sharing. Are you able to share more detail on how you implemented this and bonded a device?

@roscoe I posted the solution that was working for me: call esp_ble_set_encryption on ESP_GATTC_CONNECT_EVT event in gattc_event_handler

Thanks @dentra, but I don’t think i’m at your level to achieve this. I’m assuming you modified the source code, under esp32_ble_tracker maybe?.

It is ble client node related and must be implemented at a level of concrete component that need to enter that mode.

ESPHome 2022.4.0 supports BLE security: feat: support ble_client that use security w/o pin codes by ammmze · Pull Request #3320 · esphome/esphome · GitHub

4 Likes

@roscoe Have you tried this? I stumbled back to this topic just now and will have to try it for my use case!

Yes, tried. It works for my case especially for Tion 3s Breezer component, for 4s and Lite only esp_ble_set_encryption needed to work.

1 Like

hi, i have not had the time to try, but will give it a go and let you know how i get on. Sorry for late reply!

1 Like

Still having the same issue using the latest version of ESPhome. @dentra, i still don’t understand where your using esp_ble_set_encryption? But i assume with ESPHome 2022.4.0 supports BLE security encryption is now part of it?

Just cant seem to pair the sensor, only whilst its in pairing mode can espome connect, but then it drops out when the device timeouts of being in pairing mode.

Yes, BLE security encryption now works out of box. To be paired you need to enter your device to pairing mode then robot esp or configure a switch to disable/enable BLE and turn it off and on when pairing mode active.

Using esp_ble_set_encryption is only possible in custom component and not in yaml configuration.

1 Like

Hi dentra according to official ESPHOME documentation security encryption is not supported yet.
It says:
“This component does not (yet) support devices that require security settings (eg connecting with a PIN).”
I have a Daikin Madoka device which supports BLE.
I am able to find it as ble_client but how to pair it?

Thanks.
Ignazio

I do not know your device but as I said early, devices with simple security scheme (not using pin) are connected and paired automatically. Just enter your device in pair mode and then enable ble_client and esphome should do all the magic stuff

Hi dentra, yes this is the case.
My BLE devices show a pin when in pairing mode.
So, definitively, it’s a no go right?

Ignazio

According to your own post :point_down:

you are right :clap:

Ok, let’s wait then.
Sooner or later it will come for sure.