ESP32-C6-WROOM1 : impossible to bond

AnselmeC
Posts: 3
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 :

Code: Untitled.cpp Select all


#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLESecurity.h>

BLESecurity *pSecurity = new BLESecurity();

void setup() {
BLEDevice::init("ESP32 JustWorks");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
"beb5483e-36e1-4688-b7f5-ea07361b26a8",
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID());
pAdvertising->start();

pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
pSecurity->setCapability(ESP_IO_CAP_NONE);
pSecurity->setKeySize(16);
}

void loop() {
delay(1000);
}
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 :

Code: Untitled.cpp Select all


#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLESecurity.h>

BLESecurity *pSecurity = new BLESecurity();

class MySecurity : public BLESecurityCallbacks {
uint32_t onPassKeyRequest() override {
Serial.println("Passkey requested");
return 123456; // Set your passkey here
}

void onPassKeyNotify(uint32_t passkey) override {
Serial.print("Passkey: ");
Serial.println(passkey);
}

bool onConfirmPIN(uint32_t passkey) override {
Serial.print("Confirm Passkey: ");
Serial.println(passkey);
return true; // Return true to confirm the PIN
}

bool onSecurityRequest() override {
return true; // Accept security requests from the peer device
}

void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl) override {
if (cmpl.success) {
Serial.println("Authentication Success");
} else {
Serial.println("Authentication Failed");
}
}
};

void setup() {
Serial.begin(115200);

BLEDevice::init("ESP32 Passkey");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
"beb5483e-36e1-4688-b7f5-ea07361b26a8",
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World");
pService->start();

BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID());
pAdvertising->start();

pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
pSecurity->setCapability(ESP_IO_CAP_IO);
pSecurity->setKeySize(16);
pSecurity->setStaticPIN(123456); // Set a 6-digit passkey here

BLEDevice::setSecurityCallbacks(new MySecurity());
}

void loop() {
delay(1000);
}
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: 3
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 :

Code: Untitled.cpp Select all


pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // helps with iPhone discovery
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 1 guest