ESP32-C6-WROOM1 : impossible to bond

AnselmeC
Posts: 2
Joined: Wed Nov 13, 2024 5:08 am

ESP32-C6-WROOM1 : impossible to bond

Postby AnselmeC » Wed Nov 13, 2024 5:29 am

Hello,

I am trying to bond my ESP32-C6-WROOM1 to my smartphone.
I am using ESP_LE_AUTH_REQ_SC_BOND and ESP_IO_CAP_NONE, with a 16 bits key size. Here is my code :
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLESecurity.h>
  5.  
  6. BLESecurity *pSecurity = new BLESecurity();
  7.  
  8. void setup() {
  9.   BLEDevice::init("ESP32 JustWorks");
  10.   BLEServer *pServer = BLEDevice::createServer();
  11.   BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  12.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  13.     "beb5483e-36e1-4688-b7f5-ea07361b26a8",
  14.     BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
  15.   );
  16.   pCharacteristic->setValue("Hello World");
  17.   pService->start();
  18.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  19.   pAdvertising->addServiceUUID(pService->getUUID());
  20.   pAdvertising->start();
  21.  
  22.   pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
  23.   pSecurity->setCapability(ESP_IO_CAP_NONE);
  24.   pSecurity->setKeySize(16);
  25. }
  26.  
  27. void loop() {
  28.   delay(1000);
  29. }
  30.  
I can connect successfully (either directly in my phone settings or using NRFConnect Mobile App), but when trying to bond, I get a bondng request on my phone (which I accept), and then I instantly get an error on my ESP32 :
E (9904) BT_SMP: smp_calculate_link_key_from_long_term_key failed to update link_key. Sec Mode = 2, sm4 = 0x00
E (9904) BT_SMP: smp_derive_link_key_from_long_term_key failed

E (9910) BT_BTM: btm_proc_smp_cback received for unknown device
E (12962) BT_BTM: Device not found
What's strange is that I don't have this problem anymore when changing settings for a passkey, however I don't want my user to enter a passkey for this to work. The below code is working for the first bonding, but when shutting down the ESP32 and trying to connect again, it completely looses the bond, disconnect and doesn't want to connect anymore. Here is the code :
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLESecurity.h>
  5.  
  6. BLESecurity *pSecurity = new BLESecurity();
  7.  
  8. class MySecurity : public BLESecurityCallbacks {
  9.   uint32_t onPassKeyRequest() override {
  10.     Serial.println("Passkey requested");
  11.     return 123456;  // Set your passkey here
  12.   }
  13.  
  14.   void onPassKeyNotify(uint32_t passkey) override {
  15.     Serial.print("Passkey: ");
  16.     Serial.println(passkey);
  17.   }
  18.  
  19.   bool onConfirmPIN(uint32_t passkey) override {
  20.     Serial.print("Confirm Passkey: ");
  21.     Serial.println(passkey);
  22.     return true;  // Return true to confirm the PIN
  23.   }
  24.  
  25.   bool onSecurityRequest() override {
  26.     return true;  // Accept security requests from the peer device
  27.   }
  28.  
  29.   void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl) override {
  30.     if (cmpl.success) {
  31.       Serial.println("Authentication Success");
  32.     } else {
  33.       Serial.println("Authentication Failed");
  34.     }
  35.   }
  36. };
  37.  
  38. void setup() {
  39.   Serial.begin(115200);
  40.  
  41.   BLEDevice::init("ESP32 Passkey");
  42.   BLEServer *pServer = BLEDevice::createServer();
  43.   BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  44.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  45.     "beb5483e-36e1-4688-b7f5-ea07361b26a8",
  46.     BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
  47.   );
  48.   pCharacteristic->setValue("Hello World");
  49.   pService->start();
  50.  
  51.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  52.   pAdvertising->addServiceUUID(pService->getUUID());
  53.   pAdvertising->start();
  54.  
  55.   pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
  56.   pSecurity->setCapability(ESP_IO_CAP_IO);
  57.   pSecurity->setKeySize(16);
  58.   pSecurity->setStaticPIN(123456);  // Set a 6-digit passkey here
  59.  
  60.   BLEDevice::setSecurityCallbacks(new MySecurity());
  61. }
  62.  
  63. void loop() {
  64.   delay(1000);
  65. }
I'm struggling to make it work. I'm trying to think ESP32 Arduino BLE libraries are not reliable and that I will have to use something else for ESP32. Thank you for your help.

Anselme

AnselmeC
Posts: 2
Joined: Wed Nov 13, 2024 5:08 am

Re: ESP32-C6-WROOM1 : impossible to bond BLE

Postby AnselmeC » Thu Nov 14, 2024 4:55 am

Hello, I solved my problem.
It looks like adding these lines of code solved the issue :
  1. pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
  2. pAdvertising->setScanResponse(true);
  3. pAdvertising->setMinPreferred(0x06);  // helps with iPhone discovery
  4. pAdvertising->setMinPreferred(0x12);
I don't really know why but I hope this will help someone one day.

Who is online

Users browsing this forum: No registered users and 61 guests