I modified the ble_app_gls example using the whitelist paring mode. The code is from ble_app_hids_keyboard.
1. The code goes fine when using whitelist paring mode.
2. The delete_bonds should be runned when the button is pressed, and advertising_start returned error 12804 after deletion.
The code is below, I marked the key line with STEP 1/2/3/4, 12804 error code returned by STEP 4.
static void bsp_event_handler(bsp_event_t event)
{
ret_code_t err_code;
switch (event)
{
case BSP_EVENT_KEY_0:
delete_bonds(); //STEP1: delete bonds
break;
}
}
static void pm_evt_handler(pm_evt_t const * p_evt)
{
pm_handler_on_pm_evt(p_evt);
pm_handler_disconnect_on_sec_failure(p_evt);
pm_handler_flash_clean(p_evt);//delete old bonding info when flash is full
switch (p_evt->evt_id)
{
case PM_EVT_PEERS_DELETE_SUCCEEDED:
advertising_start(); //STEP2: start advertising after deletion
break;
}
}
static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
uint32_t err_code;
switch (ble_adv_evt)
{
case BLE_ADV_EVT_WHITELIST_REQUEST:
{
ble_gap_addr_t whitelist_addrs[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
ble_gap_irk_t whitelist_irks[BLE_GAP_WHITELIST_ADDR_MAX_COUNT];
uint32_t addr_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
uint32_t irk_cnt = BLE_GAP_WHITELIST_ADDR_MAX_COUNT;
err_code = pm_whitelist_get(whitelist_addrs, &addr_cnt,
whitelist_irks, &irk_cnt);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("pm_whitelist_get returns %d addr in whitelist and %d irk whitelist",
addr_cnt, irk_cnt);
// Set the correct identities list (no excluding peers with no Central Address Resolution).
identities_set(PM_PEER_ID_LIST_SKIP_NO_IRK); //STEP3: identities_set
// Apply the whitelist.
err_code = ble_advertising_whitelist_reply(&m_advertising,
whitelist_addrs,
addr_cnt,
whitelist_irks,
irk_cnt);
APP_ERROR_CHECK(err_code);
} break; //BLE_ADV_EVT_WHITELIST_REQUEST
case BLE_ADV_EVT_PEER_ADDR_REQUEST:
{
pm_peer_data_bonding_t peer_bonding_data;
// Only Give peer address if we have a handle to the bonded peer.
if (m_peer_id != PM_PEER_ID_INVALID)
{
err_code = pm_peer_data_bonding_load(m_peer_id, &peer_bonding_data);
if (err_code != NRF_ERROR_NOT_FOUND)
{
APP_ERROR_CHECK(err_code);
// Manipulate identities to exclude peers with no Central Address Resolution.
identities_set(PM_PEER_ID_LIST_SKIP_ALL);
ble_gap_addr_t * p_peer_addr = &(peer_bonding_data.peer_ble_id.id_addr_info);
err_code = ble_advertising_peer_addr_reply(&m_advertising, p_peer_addr);
APP_ERROR_CHECK(err_code);
}
}
} break; //BLE_ADV_EVT_PEER_ADDR_REQUEST
default:
break;
}
}
static void identities_set(pm_peer_id_list_skip_t skip)
{
pm_peer_id_t peer_ids[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
uint32_t peer_id_count = BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT;
ret_code_t err_code = pm_peer_id_list(peer_ids, &peer_id_count, PM_PEER_ID_INVALID, skip);
APP_ERROR_CHECK(err_code);
err_code = pm_device_identities_list_set(peer_ids, peer_id_count);//STEP4: pm_device_identities_list_set returned 12804 error
NRF_LOG_INFO("pm_device_identities_list_set:%d", err_code);
APP_ERROR_CHECK(err_code);
}