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

How to Delete Bonds when table full.

Hello, I am using the HTS example for the nrf51822.

In the example, bond information can be deleted during initialisation by holding down one of the buttons.

In my program, the processor is not reset, nor is there a button for deleting bonds.

After time the bond table fills up, until there is no memory to store new bonds: (BLE_GAP_EVT_DISCONNECTED => ble_bondmngr_bonded_masters_store => bond_info_store => NRF_ERROR_NO_MEM)

How do I delete old bonds (possibly all of them) when the bond table is full, to make room for new bonds.

I have tried the code below (both in the disconnected event and from main), but it crashes the softdevice:


    // check not connected
    // turn off advertising
    if (m_masters_in_db_count >= BLE_BONDMNGR_MAX_BONDED_MASTERS)
    {
        ble_bondmngr_bonded_masters_delete();
        load_all_from_flash();
        m_is_bondmngr_initialized = true;
    }

  • The code snippet you have there should work as far as I can see, but I'd strongly recommend you to check the error code you get from the function, to see if it succeded.

    Even when you do it from the disconnected event, you must make sure to do it before you start advertising again. Could this be the reason for your problem? (If you do this from the bond manager's event handler, the application's handler may have run before, and started advertising. I'd recommend you to have this kind of functionality in the application and not modify the bond manager anyway.)

    Also, if you give the bond manager an event handler when initializing it, you will get a BLE_BONDMNGR_EVT_BOND_FLASH_FULL event to it when the bond database is full. You can use this event to know when the flash is full, and then for example set a special value in GPREGRET, which is retained over resets, do a software reset and delete bond information on startup if this register is set to the special value.

    If you still have trouble, could you perhaps edit your question and elaborate a little on how exactly the softdevice crashes? Also, attaching your complete project would be helpful.

    Edit: Unfortunately, the above isn't quite correct with the current version of the bond manager. You will not get a FLASH_FULL event, instead you'll just get a call to the error handler with error code NRF_ERROR_NO_MEM (= 0x04). You will therefore have to use this to do a disconnect and delete for now. I've attached a modified version of the HTS application from the SDK that does this.

    ble_app_hts_bond_deletion.zip

  • Hi Ole,

    To fulfil your suggestions, I started with the blank ble_app_hts example provided by Nordic.

    I pasted the following function at the bottom of ble_bondmngr.c (and stuck a prototype at the top of the main.c file).

    void delete_the_bond_table(void)
    {
    	uint32_t err_code;
    	
    	err_code = ble_bondmngr_bonded_masters_delete();
    	APP_ERROR_CHECK(err_code);
    	
    	err_code = load_all_from_flash();
    	APP_ERROR_CHECK(err_code);
    	
    	m_is_bondmngr_initialized = true;
    }
    

    I then changed [main.c => on_ble_evt() => BLE_GAP_EVT_DISCONNECTED] to include a call to this function before storing bonded masters (and before re-starting advertising):

    	// Since we are not in a connection and have not started advertising, store bonds
    	delete_the_bond_table();
    	err_code = ble_bondmngr_bonded_masters_store();
    	APP_ERROR_CHECK(err_code);
    

    I compiled, set breakpoints in app_error_handler and assert_nrf_callback, then launched the debugger.

    I then used the dongle and Nordic Master Control Panel to open and close a connection.

    Result: ble_bondmngr_bonded_masters_store now returns NRF_ERROR_INTERNAL.

    Investigation shows that this is returned by a sub-call to master_update() which fails because m_master.bond.master_handle is now no longer set.

    I then deleted this above call, and entered the following in place of the main() function's eternal loop:

    	uint32_t delete_bonds_flag = false;
    	uint32_t err_code;
    
    	...
    
    	// Enter main loop
    	for(;;)
    	{
    		//power_manage();
    		
    		if (m_conn_handle != BLE_CONN_HANDLE_INVALID) // connected
    		{
    			delete_bonds_flag = true;
    		}
    		else if (delete_bonds_flag) // NOT connected
    		{
    			err_code = sd_ble_gap_adv_stop();
    			APP_ERROR_CHECK(err_code);
    			advertising_start();
    				
    			delete_the_bond_table();
    
    			delete_bonds_flag = false;
    		}
    	}
    

    I compiled, set breakpoints in app_error_handler and assert_nrf_callback, then launched the debugger.

    I then used the dongle and Nordic Master Control Panel to open and close a connection.

    Result: No errors caught using breakpoints, but there is no advertising (checked with Master Control Panel and iPhone BLE app) and no further connection possible. The debugger stops at an unknown location.

    Please advise me what code should be used to clear the bonding table, and where it should be called from in the ble_app_hts example code (without resetting the processor).

  • The actual problem is that when the bond table is full, it is not possible to bond with a new device. So the first 4 devices are fine, but the 5th will not connect. If there was some way to make the bond table re-use the oldest bond, that would be the best solution. Also, increasing the size of the bond table (to >4) could help a bit.

    Erasing the bond table also has it's own problem... a host (e.g. iPhone 4S) will think it is still bonded to the device, when it is not since the bond has been deleted from the nrf51822. This also prevents communication.

  • Sorry for the delay, but please see the comment above for an example of how to do such bond deletion.

    However, be aware that you can increase the number of stored bonds in the ble_bondmngr_cfg.h file in your project folder. Using 8 should be no problems, and as long as you don't use whitelisting, you should be able to use even more, up to the number of bonds there are room for in a flash page. How many devices do you intend to keep a bond with?

  • I meant the edit to my previous answer, not the comment. Sorry.

Related