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

ble pairing at the first of every connection try !!

hello guys,

I'm working in a project & I added some sort of security to the project, I enabled the bonding & MTIM flags through:

#define SEC_PARAM_TIMEOUT               50                                          /**< Timeout for Pairing Request or Security Request (in seconds). */
#define SEC_PARAM_BOND                  1                                           /**< Perform bonding. */
#define SEC_PARAM_MITM                  1                                           /**< Man In The Middle protection not required. */
#define SEC_PARAM_IO_CAPABILITIES       BLE_GAP_IO_CAPS_DISPLAY_ONLY//DISPLAY_YESNO     	        /**< 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. */
#define ATTR_DATA_SIZE                  BLE_ANCS_ATTR_DATA_MAX                      /**< Allocated size for attribute data. */
#define DEAD_BEEF                       0xDEADBEEF                                  /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

and added a passkey to be requested while pairing at the first time, the problem is that it pairs everytime I try to connect to the development kit with my mobile, I don't know what is the reason for that and how to make it pair only at the first time of connection, then it remember the devices previously connected to it.

Note that I used

BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&sec_mode)

and the ble event handling stack is:

/**@brief Function for handling the Application's BLE Stack events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 */
static void on_ble_evt(ble_evt_t * p_ble_evt)
{
		uint32_t                         err_code;
		static ble_gap_master_id_t p_master_id;
		static ble_gap_sec_keyset_t keys_exchanged;
		static ble_gap_evt_auth_status_t m_auth_status;

		
	switch (p_ble_evt->header.evt_id)
		{
				case BLE_GAP_EVT_CONNECTED:
						m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
						nrf_delay_ms(200);
						break;
				case BLE_GAP_EVT_DISCONNECTED:
						advertising_start();
						break;
				case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
						//err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
						err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &m_sec_param, &keys_exchanged);//&keys_exchanged	
						APP_ERROR_CHECK(err_code);
						break;
				case BLE_GAP_EVT_AUTH_KEY_REQUEST:
						err_code = sd_ble_gap_auth_key_reply(m_conn_handle,BLE_GAP_AUTH_KEY_TYPE_PASSKEY, passkey);
						APP_ERROR_CHECK(err_code);
						break;
				case BLE_GATTS_EVT_SYS_ATTR_MISSING:
						err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0,BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS);
						APP_ERROR_CHECK(err_code);
						break;

				case BLE_GAP_EVT_AUTH_STATUS:
						m_auth_status = p_ble_evt->evt.gap_evt.params.auth_status;
						if (m_auth_status.auth_status != BLE_GAP_SEC_STATUS_SUCCESS)
								sd_nvic_SystemReset();
						break;
				case BLE_GAP_EVT_SEC_INFO_REQUEST:
						//p_enc_info = keys_exchanged.keys_central.p_enc_key
						if (p_master_id.ediv == p_ble_evt->evt.gap_evt.params.sec_info_request.master_id.ediv)
						{
								err_code = sd_ble_gap_sec_info_reply(m_conn_handle, &keys_exchanged.keys_central.p_enc_key->enc_info, &keys_exchanged.keys_central.p_id_key->id_info, NULL);

								APP_ERROR_CHECK(err_code);
								p_master_id.ediv = p_ble_evt->evt.gap_evt.params.sec_info_request.master_id.ediv;
						}
						else
						{	

								// No keys found for this device
								err_code = sd_ble_gap_sec_info_reply(m_conn_handle, NULL, NULL,NULL);

								APP_ERROR_CHECK(err_code);
						}
						break;

				case BLE_GAP_EVT_TIMEOUT:
						if (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
						{
								err_code = sd_power_system_off();
								APP_ERROR_CHECK(err_code);
						}
						break;

				default:
						// No implementation needed.
						break;
		}
}

also Note that I'm not using the whitelist advertising mode.

Parents Reply Children
No Data
Related