Hi,
This is my first time to use Nordic product and I have used nRF52832 based SDK 12.0 and SoftDevice132 v2.0 about 2 months. And the IDE used for devlopment is KEIL MDK.
In my project, I do paring operation configured with static passkey and enable bond. The problem is that after every time I reset nRF52832, my smart phone using andriod system will be asked to input static passkey again when want to connect the nRF52832.
And I used 5 days to search research usefully referrence document in Chinnese website,unfortunately I can not find any useful informations about the problem in China. So,finally I can not solve the problem by self.
I do not know the detailed operations about how to paire and bond with a BLE central device like a smart phone using andriod system only one time until now, even if nRF52832 will be reset we do not need to do bonding operations again.
So, I think I need your useful help to guide me the forever bond operations step by step, as more detailed as possible, when a project need security connectivity like paire operations.
Thanks & Cheers.
The following is my key code about the configure paire and bond operations in my project.
ble_gap_sec_params_t m_sec_params;
ble_gap_sec_keyset_t m_sec_keyset;
ble_gap_enc_key_t m_own_enc_key; //need to be stored in flash
ble_gap_enc_key_t m_peer_enc_key;
static void sec_params_init()
{
memset(&m_sec_params, 0, sizeof(ble_gap_sec_params_t));
//Initialize m_sec_params
m_sec_params.bond = 1;
m_sec_params.mitm = 1;
m_sec_params.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
m_sec_params.min_key_size = 7;
m_sec_params.max_key_size = 16;
m_sec_params.kdist_own.enc = 1;
m_sec_params.kdist_own.id = 0;
m_sec_params.kdist_own.sign = 0;
m_sec_params.kdist_peer.enc = 1;
m_sec_params.kdist_peer.id = 0;
m_sec_params.kdist_peer.sign = 0;
memset(&m_sec_keyset, 0, sizeof(ble_gap_sec_keyset_t));
//Initialize m_sec_keyset
m_sec_keyset.keys_own.p_enc_key = &m_own_enc_key;
m_sec_keyset.keys_own.p_id_key = NULL;
m_sec_keyset.keys_own.p_pk = NULL;
m_sec_keyset.keys_own.p_sign_key = NULL;
m_sec_keyset.keys_peer.p_enc_key = &m_peer_enc_key;
m_sec_keyset.keys_peer.p_id_key = NULL;
m_sec_keyset.keys_peer.p_pk = NULL;
m_sec_keyset.keys_peer.p_sign_key = NULL;
}
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_CONNECTED:
err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
APP_ERROR_CHECK(err_code);
m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
err_code = sd_ble_gap_authenticate(m_conn_handle,&m_sec_params);
APP_ERROR_CHECK(err_code);
break;
case BLE_GAP_EVT_DISCONNECTED:
err_code = bsp_indication_set(BSP_INDICATE_IDLE);
APP_ERROR_CHECK(err_code);
m_conn_handle = BLE_CONN_HANDLE_INVALID;
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported
// err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
// APP_ERROR_CHECK(err_code);
sec_params_init();
err_code = sd_ble_gap_sec_params_reply( m_conn_handle,
BLE_GAP_SEC_STATUS_SUCCESS,
&m_sec_params,
&m_sec_keyset);
APP_ERROR_CHECK(err_code);
break;
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
// No system attributes have been stored.
err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
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("\r\npair success!!!\r\n");
}
else{
printf("\r\npair failed!!!\r\n");
sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
}
case BLE_GAP_EVT_PASSKEY_DISPLAY:
printf("\r\npasskey: %s\r\n",p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
case BLE_GAP_EVT_SEC_INFO_REQUEST:
sd_ble_gap_sec_info_reply(m_conn_handle, &(m_own_enc_key.enc_info), NULL, NULL);
printf("BLE_GAP_EVT_SEC_INFO_REQUEST\n");
break;
default:
// No implementation needed.
break;
}
}
static void gap_params_init(void)
{
uint32_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *) DEVICE_NAME,
strlen(DEVICE_NAME));
APP_ERROR_CHECK(err_code);
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
APP_ERROR_CHECK(err_code);
uint8_t passkey[] = "123456";
m_static_pin_option.gap_opt.passkey.p_passkey = passkey;
err_code=sd_ble_opt_set(BLE_GAP_OPT_PASSKEY,&m_static_pin_option);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
uint32_t err_code;
bool erase_bonds;
// Initialize.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
uart_init();
buttons_leds_init(&erase_bonds); //button 1(@pcb key2,pin14) is used as delete bonding information
ble_stack_init();
gap_params_init();
services_init();
advertising_init();
conn_params_init();
printf("\r\nUART Start!\r\n");
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
power_manage();
}
}