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

static passkey implementation in Central code

Hi,

I am new to NRF development. I am using 2 nrf52840 DK one as central and other as peripheral(for reference). I am using ble_gap_gatts example from central side. i am able to scan and connect to nordic peripheral board using ble_gap_gatts_c example code.

on other side, i have BLE4 sensor parameters are not in my hand. I want connect my central device to this sensor as peripheral. I tried it with normal android mobile, it has passkey "000000", after giving this able to connect using android mobile. Same i want to do using nrf52840DK board as central. I dont have keyboard or display on nay side. I read few threads, but i am not getting how to add passkey and.I am referring this sequence charts  and threads given below but not able to replicate this in the code.

Can anybody please help me?

Output log:

<debug> ble_scan: Scanning parameters have been changed successfully

<debug> ble_scan: Scanning parameters have been changed successfully

<info> app: Starting scan.

<debug> ble_scan: Scanning

<debug> ble_scan: Connecting

<debug> ble_scan: Connection status: 0

<debug> peer_manager_handler: Connected, securing connection. conn_handle: 0

<debug> peer_manager_handler: Event PM_EVT_CONN_SEC_PARAMS_REQ

<debug> peer_manager_handler: Security parameter request

<debug> peer_manager_handler: Event PM_EVT_CONN_SEC_START

<debug> peer_manager_handler: Connection security procedure started: role: Central, conn_handle: 0, procedure: Bonding

<debug> peer_manager_handler: Event PM_EVT_CONN_SEC_PARAMS_REQ

<debug> peer_manager_handler: Security parameter request

<debug> peer_manager_handler: Event PM_EVT_CONN_SEC_FAILED

<info> peer_manager_handler: Connection security failed: role: Central, conn_handle: 0x0, procedure: Bonding, error: 136

<warning> peer_manager_handler: Disconnecting conn_handle 0.

<info> app: Disconnected. conn_handle: 0x0, reason: 0x16

sequence chart - http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s130.api.v2.0.0%2Fgroup___b_l_e___g_a_p___c_e_n_t_r_a_l___b_o_n_d_i_n_g___p_k___p_e_r_i_p_h___o_o_b___m_s_c.html&cp=2_6_2_1_0_2_1_3_5_2_2 .

https://devzone.nordicsemi.com/f/nordic-q-a/16846/passkey-activation

https://devzone.nordicsemi.com/f/nordic-q-a/8049/static-passkey-and-mitm

  • Hi,

    Attached is a diff file showing the changes I made to the ble_gap_gatts_c and ble_gap_gatts example to enable bonding with static passkey. Hope this helps.

    diff --git a/examples/ble_central/ble_app_gatts/main.c b/examples/ble_central/ble_app_gatts/main.c
    index 7d5022d..34e769c 100644
    --- a/examples/ble_central/ble_app_gatts/main.c
    +++ b/examples/ble_central/ble_app_gatts/main.c
    @@ -85,10 +85,10 @@
     #define APP_SOC_OBSERVER_PRIO     1                                /**< Applications' SoC observer priority. You shouldn't need to modify this value. */
     
     #define SEC_PARAM_BOND            1                                /**< Perform bonding. */
    -#define SEC_PARAM_MITM            0                                /**< Man In The Middle protection not required. */
    +#define SEC_PARAM_MITM            1                                /**< Man In The Middle protection not required. */
     #define SEC_PARAM_LESC            0                                /**< LE Secure Connections not enabled. */
     #define SEC_PARAM_KEYPRESS        0                                /**< Keypress notifications not enabled. */
    -#define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_NONE             /**< No I/O capabilities. */
    +#define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_KEYBOARD_ONLY    /**< No I/O capabilities. */
     #define SEC_PARAM_OOB             0                                /**< Out Of Band data not available. */
     #define SEC_PARAM_MIN_KEY_SIZE    7                                /**< Minimum encryption key size. */
     #define SEC_PARAM_MAX_KEY_SIZE    16                               /**< Maximum encryption key size. */
    @@ -293,6 +293,19 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
                                                  BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                 APP_ERROR_CHECK(err_code);
             } break;
    +        
    +        case BLE_GAP_EVT_AUTH_KEY_REQUEST:
    +        {
    +          
    +          NRF_LOG_DEBUG("GAP Passkey request.");
    +          uint8_t passkey[] = "000000"; 
    +          if (p_gap_evt->params.auth_key_request.key_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
    +          {
    +              err_code = sd_ble_gap_auth_key_reply(p_gap_evt->conn_handle, BLE_GAP_AUTH_KEY_TYPE_PASSKEY, passkey);
    +              APP_ERROR_CHECK(err_code);
    +          }
    +          
    +        } break;
     
             default:
                 break;
    diff --git a/examples/ble_central/ble_app_gatts/pca10040/s132/config/sdk_config.h b/examples/ble_central/ble_app_gatts/pca10040/s132/config/sdk_config.h
    index c48d10b..96949d4 100644
    --- a/examples/ble_central/ble_app_gatts/pca10040/s132/config/sdk_config.h
    +++ b/examples/ble_central/ble_app_gatts/pca10040/s132/config/sdk_config.h
    @@ -7434,7 +7434,7 @@
     // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
     //==========================================================
     #ifndef NRF_LOG_BACKEND_RTT_ENABLED
    -#define NRF_LOG_BACKEND_RTT_ENABLED 0
    +#define NRF_LOG_BACKEND_RTT_ENABLED 1
     #endif
     // <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings. 
     // <i> Size of the buffer is a trade-off between RAM usage and processing.
    @@ -7467,7 +7467,7 @@
     // <e> NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend
     //==========================================================
     #ifndef NRF_LOG_BACKEND_UART_ENABLED
    -#define NRF_LOG_BACKEND_UART_ENABLED 1
    +#define NRF_LOG_BACKEND_UART_ENABLED 0
     #endif
     // <o> NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin 
     #ifndef NRF_LOG_BACKEND_UART_TX_PIN
    diff --git a/examples/ble_peripheral/ble_app_gatts_c/main.c b/examples/ble_peripheral/ble_app_gatts_c/main.c
    index ebd2823..74a8a9c 100644
    --- a/examples/ble_peripheral/ble_app_gatts_c/main.c
    +++ b/examples/ble_peripheral/ble_app_gatts_c/main.c
    @@ -147,6 +147,7 @@ static void gap_params_init(void)
         ret_code_t              err_code;
         ble_gap_conn_params_t   gap_conn_params;
         ble_gap_conn_sec_mode_t sec_mode;
    +    ble_opt_t               ble_opt;
     
         BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
     
    @@ -164,6 +165,11 @@ static void gap_params_init(void)
     
         err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
         APP_ERROR_CHECK(err_code);
    +
    +    ble_opt.gap_opt.passkey.p_passkey = "000000";
    +
    +    err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &ble_opt);
    +    APP_ERROR_CHECK(err_code);
     }
     
     
    diff --git a/examples/ble_peripheral/ble_app_gatts_c/pca10040/s132/config/sdk_config.h b/examples/ble_peripheral/ble_app_gatts_c/pca10040/s132/config/sdk_config.h
    index 93b1b15..efe405d 100644
    --- a/examples/ble_peripheral/ble_app_gatts_c/pca10040/s132/config/sdk_config.h
    +++ b/examples/ble_peripheral/ble_app_gatts_c/pca10040/s132/config/sdk_config.h
    @@ -90,7 +90,7 @@
     // <4=> BLE_GAP_IO_CAPS_KEYBOARD_DISPLAY 
     
     #ifndef SEC_PARAM_IO_CAPABILITIES
    -#define SEC_PARAM_IO_CAPABILITIES 3
    +#define SEC_PARAM_IO_CAPABILITIES 0
     #endif
     
     // <o> SEC_PARAM_MAX_KEY_SIZE - Maximum encryption key size in octets between SEC_PARAM_MIN_KEY_SIZE and 16.  <0-16> 
    @@ -111,7 +111,7 @@
      
     
     #ifndef SEC_PARAM_MITM
    -#define SEC_PARAM_MITM 0
    +#define SEC_PARAM_MITM 1
     #endif
     
     // <q> SEC_PARAM_OOB  - Out Of Band data not available or not
    @@ -7419,7 +7419,7 @@
     // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
     //==========================================================
     #ifndef NRF_LOG_BACKEND_RTT_ENABLED
    -#define NRF_LOG_BACKEND_RTT_ENABLED 0
    +#define NRF_LOG_BACKEND_RTT_ENABLED 1
     #endif
     // <o> NRF_LOG_BACKEND_RTT_TEMP_BUFFER_SIZE - Size of buffer for partially processed strings. 
     // <i> Size of the buffer is a trade-off between RAM usage and processing.
    @@ -7452,7 +7452,7 @@
     // <e> NRF_LOG_BACKEND_UART_ENABLED - nrf_log_backend_uart - Log UART backend
     //==========================================================
     #ifndef NRF_LOG_BACKEND_UART_ENABLED
    -#define NRF_LOG_BACKEND_UART_ENABLED 1
    +#define NRF_LOG_BACKEND_UART_ENABLED 0
     #endif
     // <o> NRF_LOG_BACKEND_UART_TX_PIN - UART TX pin 
     #ifndef NRF_LOG_BACKEND_UART_TX_PIN
    

Related