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

Implement Static Passkey with SDK12.2.0

Hi, I want to use iOS to connect nRF82832 with passkey when connecting. I use pca10040_s132 with SDK 12.2.0.

Following is my procedure:

  1. define security parameters:

    #define SEC_PARAM_BOND 0 #define SEC_PARAM_MITM 1 #define SEC_PARAM_LESC 0 #define SEC_PARAM_KEYPRESS 0 #define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY #define SEC_PARAM_OOB 0

  2. define passkey and set it:

// this is global variables
  #define STATIC_PASSKEY    "123456" 
  static ble_opt_t    m_static_pin_option;
  uint8_t passkey[] = STATIC_PASSKEY; 

// set passkey in gap_params_init()
    m_static_pin_option.gap_opt.passkey.p_passkey = &passkey[0]; 
    err_code =  sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option); 
   APP_ERROR_CHECK(err_code);
  1. send pairing request and implemet pairing procedure
 // add those cases in on_ble_evt()
      
    case BLE_GAP_EVT_CONNECTED:
        ble_gap_sec_params_t params;
        params.bond = 0;
        params.mitm = 1;
        sd_ble_gap_authenticate(m_conn_handle,&params);
       break;

   case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
       // printf("receive pair request\n");
       // resp_pair_request();
       ble_gap_sec_params_t sec_params;
       uint32_t  err_code;

      sec_param.bond           = SEC_PARAM_BOND;
      sec_param.mitm           = SEC_PARAM_MITM;
      sec_param.io_caps        = SEC_PARAM_IO_CAPABILITIES;
      sec_param.oob            = SEC_PARAM_OOB;
      sec_param.min_key_size   = SEC_PARAM_MIN_KEY_SIZE;
      sec_param.max_key_size   = SEC_PARAM_MAX_KEY_SIZE;
      sec_param.kdist_own.enc  = 1;
      sec_param.kdist_own.id   = 1;
      sec_param.kdist_peer.enc = 1;
      sec_param.kdist_peer.id  = 1;

     err_code=sd_ble_gap_sec_params_reply(m_conn_handle,BLE_GAP_SEC_STATUS_SUCCESS,&sec_param,NULL);
     APP_ERROR_CHECK(err_code);
    break;

   case BLE_GAP_EVT_AUTH_STATUS:
          if(p_ble_evt->evt.gap_evt.params.auth_status.auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
          {
            //printf("pair success\r\n");
               //  int s=0;
                // s++;
          }
          else
          {
            sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
          }
          
        break;

Conclusion: When I use iOS and start connect, the pairing message shows sucessfully and I then enter the passkey "123456". But it fails after senconds with alert:"Timeout interrogating the peripheral". Actually I find "printf("pair success\r\n");" arrives with debugging. So It should be connected. I donnot know why it comes this error. Is there something wrong with my configuration? Is peer_manager_init() important? I comment this function as I donnot need bond. Thanks a lot!

  • Hi Hung Bui, I add break point in im_ble_evt_handler() in id_manager.c, the result is that: focus on code below:

           case BLE_GAP_ADDR_TYPE_RANDOM_STATIC:
            {
                while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data))
                {
                    if (addr_compare(&gap_evt.params.connected.peer_addr,
                                     &peer_data.p_bonding_data->peer_ble_id.id_addr_info))
                    {
                        bonded_matching_peer_id = peer_id;
                        break;
                    }
                }
            }
            break;
    

    I add break point in "while(..)" and "if(add_compare(..))", "while(..)" can be excuted but "if(add_compare())" has never been excuted. Because logic in "while(..)" is false.

    I don not know why it happens. Thanks a lot!

    Best Regards

  • That means there is no bonded id in the database, I don't know how with that you would get "Connected to a previously bonded device." ?

  • .You can use another BLE module(eg.RN4020) to do a test if possible. The expeiment can be described like this:

    1. Configure RN4020 without MITM. Then try to connect and bond it. You will be rejected as no MITM protection.
    2. Configure RN4020 with MIMT. Then you try to connect it again. And you may find "Connected to a previously bonded device" in UART log.

    Best Regards

  • Hi longycy,

    I tried here with nRFMasterControl panel but don't see the same issue. When I turned off MITM ( turn off keyboard&display security option) to bond and then turn it on again after that, I simply have the passkey asking pop up and can continue normally. Here is the screenshot:

    image description

    Please make sure you deleted bond information on the RN4020 after the first trial.

    Do you have any Nordic board that you can use to test with nRFMaster Control Panel ?

    If possible, please record a sniffer trace when you test with RN4020. It can reveal why we have different result when testing with nRFMasterControlPanel vs RN4020.

    Also, please try to test with SDK v13.

Related