Pairing or bonding from ESP32 Central using NimBLE stack

nirmal.lad
Posts: 7
Joined: Mon Apr 20, 2020 5:33 am

Pairing or bonding from ESP32 Central using NimBLE stack

Postby nirmal.lad » Mon Apr 20, 2020 10:32 am

Hello,

I am new to ESP32 BLE and also new to this forum.
I am developing an application using BLE of ESP WROOM32 module.
Here is my current development setup information:
IDF version: ESP-IDFv4.0
IDE: Eclipse IDE
OS: Ubuntu 16.04LTE

I am using ESP-IDF v4.0 for development using free-rtos.
I am currently using NimBLE stack for development as my application only requires BLE support.
I require the ESP WROOM32 module to be configured as BLE central and able to connect to dedicated peripheral and communicate over secure link.
I am able to communicate with BLE peripheral without pairing or bonding.

I am unable to configure pairing process from central BLE.
I saw the examples of NimBLE Central but that doesn't show anything for pairing or bonding process.
I am currently having few queries regarding pairing and bonding process.
1. what are the steps required for initiating pairing or bonding with peripheral from central?
2. what are the api's required?
3. what events would trigger?
4. And how to handle it?

Please provide any example code or guideline(i.e. any document) for pairing and bonding process in ESP32 using NimBLE stack.
Looking forward to your response.

nirmal.lad
Posts: 7
Joined: Mon Apr 20, 2020 5:33 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby nirmal.lad » Mon Apr 27, 2020 6:47 am

Hello,

while looking for the solution to the same pairing requirement. I found that this is already same as provided in the niumble_peripheral example.
so i copied few code line:

Code: Select all

   /******************************************************************/
    ble_hs_cfg.sm_io_cap =  BLE_SM_IO_CAP_NO_IO;
    ble_hs_cfg.sm_bonding = 1;
    ble_hs_cfg.sm_mitm = 1;
    ble_hs_cfg.sm_sc = 1;
    ble_hs_cfg.sm_our_key_dist = 1;
    ble_hs_cfg.sm_their_key_dist = 1;
    /*****************************************************************/
  
This helped me for generating the pairing request with null pair key, when tried with no IO capabilities.
I also tried with IO capability of display only, this helped pairing/bonding with passkey.

Here I am testing with Android phone using nRF_Connect App, configuring it as peripheral.
But when the bonding process is done once with it, Android app stores the bond information.
Which doesn't generate pairing request once again, and ESP32 doesn't receive the encryption change event.
And communication gives error 0x105 ==>BLE_ATT_ERR_INSUFFICIENT_AUTHEN.

Can Anybody tell how to resolve this issue?
I still want to know about the bonding process and storing its key for long term.

ESP_Prasad
Posts: 18
Joined: Tue Nov 12, 2019 7:46 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby ESP_Prasad » Mon Apr 27, 2020 10:13 am

@nirmal.lad I could not reproduce the issue, Can you provide Debug logs(`make menuconfig--> component config --> Log output--> Default log verbosity` to `Debug` ) for the issue ? Is it possible for you to provide demo application to reproduce the issue? I believe you are calling `ble_gap_security_initiate()` to start security procedure after successful connection, Can you please confirm ? Can you also call `ble_store_config_init();` in your application as done in `bleprph` and let me know if you still face the issue?

nirmal.lad
Posts: 7
Joined: Mon Apr 20, 2020 5:33 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby nirmal.lad » Mon Apr 27, 2020 11:58 am

Hello,

Please find the attached document which contains sdkconfig and application code.
This application code needs improvement which is yet under progress.

I am able to pair now. but unable to store those information like that happens in mobile phone.
On next connection, mobile phone doesn't generate pair request, as well unable to communicate with characteristics too.
I have tried using nRF Connect Application in android.

Please check and suggest me the changes required.
Attachments
app_ble_central.zip
SDKconfig and source files.
(24.81 KiB) Downloaded 937 times


ESP_Prasad
Posts: 18
Joined: Tue Nov 12, 2019 7:46 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby ESP_Prasad » Tue Apr 28, 2020 8:15 am

Hi @nirmal.lad,

It would be really helpful if you can provide console logs with DEBUG prints on as mentioned earlier :) I have left a few comment, please let me know if you find them helpful.
On next connection, mobile phone doesn't generate pair request
If the bond is established successfully then it is expected that new pairing request is not generated . As per as 'unable to communicate with characteristics' part, it would be better if you can provide console logs with DEBUG prints on.

In your logs, if you are able to observe below print, this means encryption is successful.

Code: Select all

Encrypt change: status=0 handle=0 state=1
You can also observe in console logs the status of 'LE Start Encryption Command' sent from central,

Code: Select all

looking up peer sec; peer_addr_type=1 peer_addr=0x41 0x61 0x2b 0xe0 0xbe 0x5b  
ble_hs_hci_cmd_send: ogf=0x08 ocf=0x0019 len=28
0x19 0x20 0x1c 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xff 0x7c 0x92 0x40 0xd7 0x16 0x2b 0x74 0x6d 0xbf 0x9d 0x1b 0x19 0x64 0xf2 0xf3 
Command Status: status=0 cmd_pkts=5 ocf=0x19 ogf=0x8
but unable to store those information like that happens in mobile phone.
If you are able to see below prints in your console logs (DEBUG on), then the bond is getting stored actually. Can you please confirm if you are not able to see below prints ?

Code: Select all

persisting peer sec; ediv=0 rand=0 authenticated=1

nirmal.lad
Posts: 7
Joined: Mon Apr 20, 2020 5:33 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby nirmal.lad » Tue Apr 28, 2020 12:19 pm

Hello @ESP_Prasad,

As per your suggestion, I have generated the debug logs.
But I was unable to find the term "LE Start Encryption Command".

Please check the attached log file.
I have generated the log with no IO capability configuration.
Here is steps performed:
1. BLE Started:==> Paired with the BLE Peripheral with null pair key.
2. Communicated with peripheral and then disconnected.
3. Resetting the device after disconnection:=> Unable to communicate.

Please suggest me if any changes required.
Looking forward to your response.
Attachments
ble_logs_28042020.txt
(72.59 KiB) Downloaded 757 times

ESP_Prasad
Posts: 18
Joined: Tue Nov 12, 2019 7:46 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby ESP_Prasad » Tue Apr 28, 2020 2:38 pm

Thank you for the logs @nirmal.lad

From your logs

Code: Select all

error persisting our sec; too many entries (3)
Although this should have been handled by `ble_hs_cfg.store_status_cb` callback by un-pairing oldest peer, somehow the space was not freed up, will update about this point. Can you please issue 'erase_flash' (`make erase_flash` / `idf.py erase_flash`) and redo the experiment again ?

nirmal.lad
Posts: 7
Joined: Mon Apr 20, 2020 5:33 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby nirmal.lad » Wed Apr 29, 2020 11:16 am

Hello @ESP_Prasad,

I have tried erasing flash and re programming the device.
But the issue remains same.

Looking forward to your response.

ESP_Prasad
Posts: 18
Joined: Tue Nov 12, 2019 7:46 am

Re: Pairing or bonding from ESP32 Central using NimBLE stack

Postby ESP_Prasad » Thu Apr 30, 2020 11:21 am

Hi @nirmal.lad,

Could you please confirm that you are not enabling `BLE_SM_PAIR_KEY_DIST_ID` for key distribution ?
Though I can see from your code that

Code: Select all

ble_hs_cfg.sm_our_key_dist = 1;
However from the logs I can see

Code: Select all

 rxed sm command: id addr info; conn=0 addr_type=0 addr=40:9c:28:d2:67:93
which is kind of possible only when `BLE_SM_PAIR_KEY_DIST_ID` is enabled in key distribution. If you have dependency on using Id keys then it worth to give a try to RPA feature, reference: https://esp32.com/viewtopic.php?f=13&t=14966#p58984 .

Who is online

Users browsing this forum: No registered users and 17 guests