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

nRF52832: How to add write protected characteristic by KEY

I am working on SDK-15.2, nRF52832 Dev board. My question is how to add write protected characteristic by KEY? Mean when we want to write something from peer device (smartphone) on "write" characteristic it should ask to enter KEY.

 is there any example code or documentation available?

Any suggestions will be appreciated.

Thanks

Raj

  • First you need to secure the link, this can be done using the Peer Manager. Then you can set the Security requirement for writing to the characteristic value through the field ble_add_char_params_t.write_access. For example:

    ble_add_char_params_t add_char_params;
    .
    .
    .
    add_char_params.write_access = SEC_MITM;

    If you secure the link using static passkeys, you need to enter a 6 digit key (20 bits) in order to pair/bond the devices, and access the protected characteristics. Another choice for an even more secure protection is OOB, where an 128 bit key is transferred between the devices, using e.g. NFC.

    If anything is unclear, or do you want me to go more into details about anything, please tell me.

    Best regards,

    Simon

  • Thanks Simon. Could you please give me more details and also code snippets which can ask to enter key before writing any value in characteristic?

  • I have attached a modified version of the ble app uart example, where I have restricted the write access for the RX Characteristics by demanding the passkey to be typed in. I have used the approach as described in my previous answer. 

    I also changed one line of the ble_nus.c file (which you must do yourself, since the uploded example only contains the project folder)

    uint32_t ble_nus_init(ble_nus_t * p_nus, ble_nus_init_t const * p_nus_init)
    {
        .
        .
        .
        add_char_params.read_access  = SEC_OPEN;
        // In the line below I changed the write access to JUST WORKS 
        add_char_params.write_access = SEC_JUST_WORKS;//SEC_OPEN;
    
        err_code = characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->rx_handles);
        .
        .
        

    Be aware that this file is used by other examples in the SDK, and the change will cause undesirable behaviour for those examples. 

    ble_app_uart_write_prot_char.rar

    Best regards,

    Simon

Related