Esp32-snippets: Save/remember BLE pairing in NVS

Created on 24 Feb 2018  路  11Comments  路  Source: nkolban/esp32-snippets

I'm using code based on the following example:
BLE_server_passkey.ino

I can successfully pair and read/write to a Characteristic with "setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED)", but after removing power from the ESP32 I need to repeat the pairing process.

Is there a way to save the pairing information to non volatile storage?

Most helpful comment

Hi,
there is no need to save anything in NVS, it should be done by esp-idf. You just need to change this line https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/Arduino/security/BLE_server/BLE_server_passkey/BLE_server_passkey.ino#L65

and use this in it:
ESP_LE_AUTH_REQ_SC_BOND
or this:
ESP_LE_AUTH_BOND

Then your device will be bonded and stored and no more authentication will be required unless esp32 or device will delete bonding info.
This info may be helpful:
https://github.com/espressif/esp-idf/blob/e676676541ff31b72818246d267b0ccc529cb0d7/examples/bluetooth/gatt_security_server/tutorial/Gatt_Security_Server_Example_Walkthrough.md

BTW Im glad it works for you

All 11 comments

Hi,
there is no need to save anything in NVS, it should be done by esp-idf. You just need to change this line https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/Arduino/security/BLE_server/BLE_server_passkey/BLE_server_passkey.ino#L65

and use this in it:
ESP_LE_AUTH_REQ_SC_BOND
or this:
ESP_LE_AUTH_BOND

Then your device will be bonded and stored and no more authentication will be required unless esp32 or device will delete bonding info.
This info may be helpful:
https://github.com/espressif/esp-idf/blob/e676676541ff31b72818246d267b0ccc529cb0d7/examples/bluetooth/gatt_security_server/tutorial/Gatt_Security_Server_Example_Walkthrough.md

BTW Im glad it works for you

Thanks, it's working!

Kinda unrelated, but I had to implement method bool onConfirmPIN(uint32_t pin) in class MySecurity to get the example to compile.

Example seems to be a little outdated, because bool onConfirmPIN(uint32_t pin) has been added later on, sorry for that.

How do I get the example to compile? I've been looking into the library, but I can't find a function definition for bool on ConfirmPIN(unsigned int) that there's defined reference to, only bool onConfirmPIN (uint32_t pin).

You have to use callback with this value as parameter:
https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/BLESecurity.h#L67

Here is only example, but you have to implement it to return true or false, depend on input value:
https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLETests/security/SampleClient_authentication_passkey.cpp#L39-L43
You can have for example 2 buttons where one is to return true and one to return false.

I've used the example, thank. I've implemented onConfirmPIN to my understanding that if the input value matches the key it return true, otherwise false. I still have the compiler error saying undefined reference to MySecurity::onConfirmPIN(unsigned int)' no matter what I do. It doesn't make much sense to me.

If you have somewhere in your code (BLESecurity or example) onConfirmPIN(unsigned int) then you need to change it to:
onConfirmPIN(uint32_t pin)
This must be veeery old code and sorry for that.

No problem, I've checked the libraries and there's no instance of onConfirmPIN(unsigned int) anywhere, not even in my code, which is exactly why this compiler error doesn't make any sense to me.

Archiving built core (caching) in: C:\Users\xxx~1\AppData\Local\Temp\arduino_cache_984968\core\core_espressif_esp32_featheresp32_FlashFreq_80,UploadSpeed_921600,DebugLevel_verbose_2bcf637f728dfd555f24612e1a1b530d.a sketch\BLE.cpp.o:(.rodata._ZTV10MySecurity[vtable for MySecurity]+0x20): undefined reference to MySecurity::onConfirmPIN(unsigned int)'

That file is the compiler file I believe, but it's not human readable.

The thing is i am not using unsigned int because i am trying to follow esp-idf convention.
Just try to add onConfirmPIN(unsigned int) in your callback and see what happen, and if you can could you post all logs with error you are getting.

//bool onConfirmPIN(uint32_t pass_key);
bool onConfirmPIN(usigned int);
and the code compiles with no errors...

Good for you, now you have to remember this to fix when it will complain next time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mahdikan picture mahdikan  路  4Comments

glatteis picture glatteis  路  10Comments

hansmbakker picture hansmbakker  路  6Comments

JasXSL picture JasXSL  路  3Comments

frankipl picture frankipl  路  8Comments