Page 1 of 1

Pairing mode on ESP32

Posted: Tue May 11, 2021 2:27 pm
by danpf1
Hello,

What is the correct way to enter pairing mode on an ESP32 peripheral device with only a single button?

I want the device to allow pairing and bonding after a button press. Then, after the device reboots only a previous paired/bonded device will be allowed to connect.

I have seen this question asked before and here are the things I tried (unsuccessfully).

1. Whitelisting
  1. In "pairing mode" change the

    Code: Select all

    adv_filter_policy--> ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY.
  2. After a successful pair and bonding, add the device to the whitelist
  3. Set the

    Code: Select all

    adv_filter_policy--> ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST
    (so only whitelisted devices can connect).


Result: After step (c) the whitelisted device still cannot connect. It seems like the device wasn't added to the whitelist because it cannot connect. However, I have checked the count of whitelisted devices (using function: esp_ble_gap_get_whitelist_size ) and it says the count went up by one. Similarly, GAP event ESP_GAP_BLE_UPDATE_WHITELIST_COMPLETE_EVT also shows a successful "add" operation. (Side note: param->update_whitelist_cmpl.wl_opration. "wl_opration" is spelled wrong).

Question: Is there a way to print out the whitelist device addresses? Or debug it in some way?

2. Second approach: Toggle IO_CAP after pairing/bonding.
  1. Enter pairing mode by setting

    Code: Select all

    esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
  2. Pair and bond with the device
  3. Stop allowing "bonding" by setting:

    Code: Select all

    esp_ble_io_cap_t iocap = ESP_IO_CAP_IN;
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
Result: Using this approach the Android phone pairs and bonds in step (b) correctly. However, after we change the IOCAP_MODE in step (c) and try to reconnect, the Android phone attempts to pair/bond again which fails. I expected since it was already bonded that it would be able to reconnect without pairing again.

Question: Is there a way to change the IO capability and have previously paired / bonded devices skip the pairing process after the change?


Notes:
- I'm using IDF esp-idf-v4.2.1/
- I'm using an Android smartphone to connect to the ESP32 WROOM board
- This is all using BLE with ESP_BLE_SM_AUTHEN_REQ_MODE --> ESP_LE_AUTH_REQ_SC_BOND

Thanks for your help!

Re: Pairing mode on ESP32

Posted: Tue May 11, 2021 6:58 pm
by chegewara
You have few options how to do it. One option is to return true in ESP_GAP_BLE_SEC_REQ_EVT event if you want to bond with peer device. This can be controlled with button.

You have to use any security request params with bonding enabled. Then device will connect next time without need to enter pin code:
https://github.com/espressif/esp-idf/bl ... parameters

Re: Pairing mode on ESP32

Posted: Wed May 12, 2021 2:26 pm
by danpf1
Thanks for your reply.

I never get this event "ESP_GAP_BLE_SEC_REQ_EVT". I tried with both ESP_IO_CAP_OUT and ESP_IO_CAP_NONE. The event never fired when I paired and bonded.

I'm using auth_req = ESP_LE_AUTH_REQ_SC_BOND

Is there something else to try? Or is there something else that needs to be set to have that event fire?

Is there any example with the whitelist working? If it works that would seem to be a nice approach.

Thanks for your help!

Re: Pairing mode on ESP32

Posted: Wed May 12, 2021 3:57 pm
by chegewara

Re: Pairing mode on ESP32

Posted: Wed May 12, 2021 4:37 pm
by danpf1
Thanks. That was the code I started with.

It doesn't do whitelisting. When I use my version of it, the event ESP_GAP_BLE_SEC_REQ_EVT isn't called.

Do you think that event is fired when running the code?

Thanks!

Re: Pairing mode on ESP32

Posted: Wed May 12, 2021 5:11 pm
by chegewara
All depends what security settings you have. I know it works with Kolban's C++ library/arduino BLE library, which is just wrapper around esp-idf.

Re: Pairing mode on ESP32

Posted: Tue Jun 08, 2021 10:33 am
by mmm.elife
Hi there @danpf1, I am trying the same thing.

About your question of the ESP_GAP_BLE_SEC_REQ_EVT, it seems it is triggered by the client requesting a change on the security settings. See more info in the "Security Requests" chapter, of the link below.

https://github.com/espressif/esp-idf/bl ... y-requests

Re: Pairing mode on ESP32

Posted: Tue Jun 08, 2021 2:50 pm
by danpf1
Thanks! I'll check it out.