ESP32 Bluetooth Pairing Mode Change

schkillll
Posts: 16
Joined: Tue Jul 23, 2019 1:46 pm

ESP32 Bluetooth Pairing Mode Change

Postby schkillll » Wed Aug 07, 2019 6:42 am

Hi,
With the below example, I can pair my phone to an ESP32. But the passkey is shown to the user at phone's end and the user just has to click OK for pairing.

But I need the user to enter the passkey for pairing, which I can see in IDF monitor. So, only those who know the key can pair to the ESP32. Which API to use for this?
Also in the example, the passkey is randomly generated. Can I use a hardcoded passkey, which I can set in the code?

Link for example - https://github.com/espressif/esp-idf/bl ... tor_demo.c

Link for API's available - https://docs.espressif.com/projects/esp ... ap_bt.html

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

Re: ESP32 Bluetooth Pairing Mode Change

Postby phantomBlurrr » Wed Aug 07, 2019 12:53 pm

Hello,
You are very close to the solution you want; The code you used to make the passkey be shown on the android device is correct, you just have to change some of the parameters. Try changing these like so:

esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;
uint8_t key_size = 16; //the key size should be 7~16 bytes
uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
uint32_t passkey = 123456; //Number that has to be input when the user tries to pair their phone
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
uint8_t oob_support = ESP_BLE_OOB_DISABLE;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));

I hope you recognize these parameters, they are at the end of the app_main() loop after you have initialized the GATT server and all that. I don't remember which one makes it so the password is required, I THINK it's setting iocap = ESP_IO_CAP_OUT. Try that first.
Good luck!

Also, the example you linked is BT Classic, is this intentional or are you trying to use BLE? Make sure you're at the right place (the above code is for BLE GATT, idk if it works with BT Classic).

Who is online

Users browsing this forum: No registered users and 22 guests