ESP32-C6-WROOM1 : impossible to bond
Posted: 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 :
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 :
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
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 :
- #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);
- }
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 :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
- #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);
- }
Anselme