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

How to set static passkey for nrf51822 BLE central(s120 ) ?

As we known we can set static passkey in peripheral side (s110) with the API sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &ble_opt) as below:

inline void setPassKey(void) { uint8_t passkey[] = "123456"; ble_opt_t ble_opt; ble_opt.gap.passkey.p_passkey = &passkey[0]; (void) sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &ble_opt); }

But the same API can't be used to set passkey in nrf51822 BLE central(s120 ) side. So how to set static passkey in nrf51822 BLE central(s120 )?

  • FormerMember
    0 FormerMember

    Peripheral display:

    Static passkey for S120 can be impmlemented by following the message sequence chart for "GAP Central Bonding: Passkey Entry (Peripheral display) or OOB MSC" . The passkey could then be hard coded in the application.

    The passkey should be passed as a parameter when the function sd_ble_gap_auth_key_reply(..) is being called. It can for example be done the following way in main.c:

    #define STATIC_PASSKEY				"123456" 				            		/**< Static pin. */
    uint8_t                             passkey[] = STATIC_PASSKEY;
    
    static void on_ble_evt(ble_evt_t * p_ble_evt)
    {
        uint32_t                err_code;
        switch (p_ble_evt->header.evt_id)
        {
            ...
            ...
            case BLE_GAP_EVT_AUTH_KEY_REQUEST:
                err_code = sd_ble_gap_auth_key_reply(m_connection_handle,BLE_GAP_AUTH_KEY_TYPE_PASSKEY, passkey);
                APP_ERROR_CHECK(err_code);
                break;
            ...
            ...
            default:
                break;
        }
    }
    

    Central display:

    If you want to implement static passkey for S120 when the S120 device has the display,you should follow the message sequence chart "GAP Central Bonding: Passkey Entry, Central displays". In addition to what is in the message sequence chart, the static passkey has to be added. It can for example be done the following way: Set the option for static passkey in the softdevice, it can for example be done right after the initialization of the softdevice in main.c:

        #define STATIC_PASSKEY				"123456" 				         /**< Static pin. */
        uint8_t                             passkey[] = STATIC_PASSKEY;
        int main(void)
        {       
            uint32_t err_code;
            ble_opt_t static_pin_option;
            ...
            ble_stack_init();
            static_pin_option.gap_opt.passkey.p_passkey = passkey;
            err_code =  sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &static_pin_option);
            ...
        }
    

    On the peripheral side (S110), the message sequence chart "GAP Bonding: Passkey Entry (Central display) or OOB MSC" should be followed. The passkey can then be added the same way as for the central in the "peripheral display" case.

  • I want to set static passkey on Central side and then display it. So the message sequence chart should be "GAP Central Bonding: Passkey Entry, Central displays". But according to the sequence, I still don't know how to set a static passkey on Central side. Looks like we should set static passkey to softdevice. Could you please tell more details about how to implement it? Thanks!

  • Hi Kristin, Maybe I didn't describe my question clearly. For your answer, I know this case. This case is that set the static passkey on peripheral side, and then display it and send out AUTH_KEY_REQUEST message on peripheral side. Then on Central side, can use the function sd_ble_gap_auth_key_reply() to reply the required passkey. But my question is not in this case. My case is a opposite direction of this case. I have a display device on Central side. Then the passkey can be displayed on it. But by default the passkey is random. I want to set a static passkey "123456", and display it on the display of central side, then the peripheral side can use function sd_ble_gap_auth_key_reply() to reply the passkey. So I'm asking how to set static passkey on Central side. If I don't use some API to set passkey to "123456", then after peripheral reply the passkey "123456", the auth will be failed.

  • Hi Kristin, This is just my question. Can function sd_ble_opt_set() in s120 really be used to set passkey? I see in the code and SDK document this function sd_ble_opt_set() in s120 can just be used to set channel map, but can't be used to set passkey. The API sd_ble_opt_set() in s120 is not the same as s110. Could you please help to have a look again? Thanks!

  • FormerMember
    0 FormerMember in reply to FormerMember

    Yes, with the latest version of S120, S120 v.2.0.0, sd_ble_opt_set(..) can be used to set the static passkey option.

    In S120 version 1.0.0 or 1.0.1, sd_ble_opt(..) cannot be used to set the static passkey option. For static key implementation, you will then have to use the "peripheral display" option.

Related