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 Longycy,

    Could you capture a sniffer trace ?

    If you simply test with the ble_app_gls example would you manage to bond and access the service ? (it's not static passkey, but we should start there)

    Also please try to use nRFConnect app to test instead of Light Blue.

  • Hi Hung Bui,

    Thank you for your example "ble_app_gls". I have tried this example and it works when using nRFConnect. But when I tried another BLE module, I found a question: In the code if peer did not use MITM, the function pm_peer_id_get(...) will be called to get the bonded peer as BLE Stack has its bond information. Then bond information will be erased when disconnected. Actually, in my test pm_peer_id_get(...) get NULL, because "NRF_LOG_DEBUG("Collector's bond deleted\r\n");" is not printed in UART Tool. So "m_peer_to_be_deleted == PM_PEER_ID_INVALID". Why it cannot get peer id by using "pm_peer_id_get(m_conn_handle, &m_peer_to_be_deleted)" ? Could you please help me to confirm this? Thank you very much!

    Best Regards

  • Which was the another BLE module you tested ?

    If you were testing with gls, you can find that if the connection was not paired with MITM we will disconnect and set m_peer_to_be_deleted to the current peer in PM_EVT_CONN_SEC_SUCCEEDED in pm_evt_handler().

    When we actually disconnect in BLE_GAP_EVT_DISCONNECTED event in on_ble_evt() we will erase bond information.

    Please try to debug and check the behavior. Check if m_peer_to_be_deleted set to anything and then if pm_peer_delete(m_peer_to_be_deleted) is called or not and returns success or not.

  • Hi Hung Bui,

    I use Microchip RN4020 (Core 4.1) as central to test nRF52832. Following your advice, I tried again to debug gls example. The conclusion can be described as below:

    Firstly, I configure RN4020 without MITM. When connecting, I find UART print "Collector did not use MITM, disconnecting" and "Collector's bond deleted". That is right and it proves bond information has been erased.

    Secondly, I re-configure RN4020 with MIMT.At this time, UART print "Connected to a previously bonded device." and then "connected". This is very strange as bond information has been erased before.

    So why? I guess maybe bond information has not been erased correctly even though "pm_peer_delete(m_peer_to_be_deleted);" is excuted and return SUCCESS. Whether function "pm_peer_delete(...)" can be excuted correctly or not?

    Thank you very much!

    Best Regards

  • Hi Longycy,

    I suspect the bond deleting was not executed properly. Note that deleting doesn't happen at the same time when you see the debug text. It happens after that when the softdevice schedule the flash operation.

    I suspect that the program crashed right after that and reset. Please set optimization level to 0 and add DEBUG into C/C++ preprocessor symbols and add a break point inside app_error_handler() in app_error.c to check if there is any assertion.

    Also make sure you do a full erase of flash before you try to test.

    You can also add a break point inside im_ble_evt_handler() in id_manager.c to check if the address matched is actually correct or not.

    Also please send me the full UART log you have when testing.

Related