I am working on a BLE peripheral (nRF52832 & SDK 15) which uses the Peer Manager to handle encryption and bonding to a previously existing central device. Sometimes bonding works, but often I am seeing a failure mode where the encryption and bonding process succeeds (the connection completes and data flows from central to peripheral and vice versa) but the Peer Manager doesn't save the bond. I discover this when I disconnect and reconnect, and I get PM_EVT_CONN_SEC_FAILED with PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING.
So far I have attempted to verify that the bond data isn't being stored by looking at what happens after I get PM_EVT_CONN_SEC_SUCCEEDED during bonding. I went into peer_database.c and added some NRF_LOG()s in write_buf_store(). I see the write happen and no error is reported. However I never get either PM_EVT_PEER_DATA_UPDATE_SUCCEEDED or PM_EVT_PEER_DATA_UPDATE_FAILED. I put log msgs into pdb_pds_evt_handler() in peer_database.c, and in my own pm_evt_handler. It appears that the bond save is failing silently. This is very frustrating.
Sometimes the bond *is* successfully saved, and I'm able to reconnect to the central indefinitely. I can't yet figure out why it sometimes succeeds.
I discovered this issue when starting to write code to handle a possible failure which could be caused by the central losing its bond data... the idea being that if the connection fails, both sides will the delete their bonds automatically and can then be manually rebonded by the user. I test this scenario by manually deleting the bond in the central... on the next connection attempt, reestablishing the link fails and the peripheral deletes its bonds. This all seems to be working as expected. But when I manually rebond, I'm stuck... the nRF52 usually isn't saving the new bond.
This current project is to replace a remote control based on another vendor's BLE chip. Encryption and bonding have worked for years with the central so I have no reason to distrust it.
Any advice on how to debug this further would be much appreciated.
Darren