This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Merging LE Secure Connections Multirole Example and Ble App UART Example

Hi,

I am working on a project where I need to send and receive ASCII strings from my Central Device to the Peripheral Device. Before the communication starts there should be a bonding (paring) between the devices. The nRF52840 DK is used as a peripheral device and nRF Toolbox is used as a central device.  After the bond is established the text should be encrypted using the AES algorithm. Finally, the text sent should be displayed using UART.

the bonding and paring are done in example LE Secure Connections Multirole and the UART is done in the example Ble App UART so I wanted to merge both the files. Can anyone help me with this?

The tools and software used are listed here:

NRF52840 DK

SDK 15.2.0

Segger embedded studio for coding

nRF Toolbox version 2.0

Parents
  • You don't need to merge the two examples completely, I would instead recommend you to add the security related functionality to the BLE App UART example. In addition I don't think the nRF Toolbox supports bonding with the BLE UART example. You should use nRF Connect (for phone) instead.

    I would recommend you to take a look at the Peer Manager, which is a library that can be used to manage BLE Security. If you take a look at the different Message Sequence Charts for LESC Pairing, you can see that pairing consist of a set of events and replies. The Peer Manager will handle most of this for you.  

    In order to initialize the Peer Manager you have to execute the three steps explained in this link. Then, depending on the pairing method, you have to set the Security Parameters accordingly. 

    Choose pairing method and set security parameters

    The LE Secure Connections Multirole examples uses the pairing method Numeric Comparison, but you have other choices, such as Numeric comparison, Out of Band, Passkey Entry and Just works. Check out this link on how to set the security parameters for the different pairing methods.

    Initialize the Peer manager

    In order to initialize the Peer Manager you have to run the three steps, as explained here:

    Include functionality depending on pairing method

    Depending on the pairing method, different functionality must be implemented. E.g. if you want to use Numeric Comparison, you need a way of displaying the comparison numbers when receiving BLE_GAP_EVT_PASSKEY_DISPLAY, and also functionality to press a button and respond with sd_ble_gap_auth_key_reply(). This will make more sense of you look at the Message sequence chart for Numeric Comparison. The function nrf_ble_lesc_request_handler() should run regularly in the main loop (Take a look at the The LESC Multirole example on where to put it).

    If you want me to go through any of the other pairing methods, please ask. 

    Remove BLE event replies from the BLE App UART examples

    The ble app uart example includes a ble_evt_handler(), which will receive and reply to BLE events, including some that will interfere with the Peer Manager and the pairing process. You should comment out the replies to these events: BLE_GAP_EVT_SEC_PARAMS_REQUEST and BLE_GATTS_EVT_SYS_ATTR_MISSING.

    I may have forgotten something, but this should be a good start. Please ask if anything is unclear.

    Best regards,

    Simon

  • Thank You for explaining the initial steps.

    I have included the peer manager function in my UART example with numeric comparison security features.
    When I try to connect my nRF board with the new UART example( including security features) it shows connected initially but while bonding and pairing it throws an error stating:

    ERROR: (0x16): GATT CONN TERMINATED LOCAL HOST.

    The screenshort of the nRF Toolbox app UART logs is attached below:



    As you told me previously that nRF toolbox doesn't support BONDING and PAIRING so I tried to connect using the nRF Connect App but still I am getting the same Error.

    Am I missing anything If so please help me!

  • I was able to make it work. I didn't do any changes to your main file, I only modified sdk_config.h and added some .c and .h files to the project. Here is the result:

    nRF Toolbox

    <info> app: LE Secure Connections example started.
    <info> app: Advertising
    <info> app: PERIPHERAL: Connected, handle 1.
    <info> app: PERIPHERAL: BLE_GAP_EVT_SEC_PARAMS_REQUEST
    <info> app: PERIPHERAL: BLE_GAP_EVT_PASSKEY_DISPLAY: passkey=982962 match_req=0
    <info> peer_manager_handler: Connection secured: role: Peripheral, conn_handle: 1, procedure: Bonding
    <info> app: PERIPHERAL: BLE_GAP_EVT_AUTH_STATUS: status=0x0 bond=0x1 lv4: 0 kdist_own:0x3 kdist_peer:0x3

    nRF Connect

    <info> app: LE Secure Connections example started.
    <info> app: Advertising
    <info> app: PERIPHERAL: Connected, handle 1.
    <info> peer_manager_handler: Connection security failed: role: Peripheral, conn_handle: 0x1, procedure: Encryption, error: 4102
    <warning> peer_manager_handler: Disconnecting conn_handle 1.
    <info> app: PERIPHERAL: Disconnected, handle 1, reason 0x16.
    <info> app: PERIPHERAL: Connected, handle 1.
    <info> app: PERIPHERAL: BLE_GAP_EVT_SEC_PARAMS_REQUEST
    <info> app: PERIPHERAL: BLE_GAP_EVT_PASSKEY_DISPLAY: passkey=757912 match_req=0
    <info> peer_manager_handler: Connection secured: role: Peripheral, conn_handle: 1, procedure: Bonding
    <info> app: PERIPHERAL: BLE_GAP_EVT_AUTH_STATUS: status=0x0 bond=0x1 lv4: 0 kdist_own:0x3 kdist_peer:0x3
    

    When I am using nRF Connect, I have to connect two times before it works, I am not sure why but can look into it.

    I am not sure why your application fails, but could you test out the exact same project as me, and see if you get the same results? I have attached the project in zipped format.

    ble_app_multirole_lesc_uart.zip

    Best regards,

    Simon

  • Thank You. I will try to run the file and let you know.

  • So, nRF Toolbox also supports bonding and pairing?

  • Yes it seems like it, I was wrong.

    Did my project work?

  • Yes, nRF Toolbox supports bonding. I verified now.
    Actually, there was a problem with the central device which was getting disconnected.
    So I changed the central device and now the communication is established with bonding.
    I tried your code also and it's working.

    Thank you for your help.  

Reply Children
Related