<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/62439/questions-about-implementing-ble-bonding-with-nus</link><description>Hi 
 This is Joe. I would like to ask questions about implementing BLE bonding. 
 SDK: 16.0.0 (nRF5SDK160098a08e2) 
 Projects: The base project I start with is NUS example (both Peripheral and Central) example/ble_peripheral/ble_app_uart example/ble_central</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Jul 2020 11:50:18 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/62439/questions-about-implementing-ble-bonding-with-nus" /><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/259425?ContentTypeID=1</link><pubDate>Fri, 10 Jul 2020 11:50:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:917913f2-d1ed-428e-af9f-e5365a80387b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello Joe,&lt;/p&gt;
&lt;p&gt;Thank you for sharing, and I am glad to hear that it worked out.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Have a nice weekend.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/259304?ContentTypeID=1</link><pubDate>Fri, 10 Jul 2020 02:44:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:17a1dc91-a59a-47ca-8992-e8e3adb660f5</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;br /&gt;&lt;br /&gt;Thank you very much for your help so far.&lt;br /&gt;Finally I have get my device working correctly.&lt;br /&gt;&lt;br /&gt;My purpose is 1-to-1 connection, so I just followed your suggestion.&lt;br /&gt;What I do is add a disconnect process and wait for the disconnect event before deleting the bonding.&lt;br /&gt;&lt;br /&gt;Here is my code for reference, in case someone need it in the future.&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void button_handler(uint8_t pin_no, uint8_t button_action)
{
    uint32_t err_code;
    //NRF_LOG_INFO(&amp;quot;button_handler&amp;quot;);
    if(button_action == APP_BUTTON_PUSH)
    {
        NRF_LOG_INFO(&amp;quot;APP_BUTTON_PUSH (pin_no = %d)&amp;quot;, pin_no);
        switch(pin_no)
        {
            case DELETE_BUTTON_PIN:
                  if (m_is_connected) {
                      m_delete_bond_after_disconnect = true;
                      err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                      if (err_code != NRF_ERROR_INVALID_STATE)
                      {
                          APP_ERROR_CHECK(err_code);
                      }
                  } else {
                      advertising_start(true);
                  }
                break;
            default:
                break;
        }
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt-&amp;gt;header.evt_id)
    {
        // ...
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO(&amp;quot;Disconnected&amp;quot;);
            // ...
            advertising_start(m_delete_bond_after_disconnect);
            m_delete_bond_after_disconnect = false;
        break;
    // ...
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258953?ContentTypeID=1</link><pubDate>Wed, 08 Jul 2020 09:11:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aeeab265-ddaa-494f-8015-910802f295c7</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Thanks for sharing your progress. It is very useful for other users who encounter similar problems.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The softdevice can only have a certain number of connections (declared in the application). If you are in a connection, and you have set the maximum number of connections to 1, then sd_ble_gap_adv_start() will return NRF_ERROR_CONN_COUNT. So in your case, you are probably still connected, and you start advertising again.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you only intend to support one connection at the time, you need to disconnect before you start advertising. Deleting the bonds will not disconnect your current connections, so you need to do this &amp;quot;manually&amp;quot;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Perhaps your button handler can disconnect from the central before it deletes the bonds. Remember that disconnecting can take some time, especially if you have a long connection interval. If so, perhaps you should set a flag (a local variable), and disconnect. When you get the disconnected event, you can check this flag, and call advertising_start(true) if the flag is set, and advertising_start(false) if the flag is not set (just a normal disconnect).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you intend to support more than one connection, you need to increase the connection count. Please refer to the multilink examples. ble_app_multilink_central for multilink central, and&amp;nbsp;ble_app_multiperipheral for multiperipheral.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258894?ContentTypeID=1</link><pubDate>Wed, 08 Jul 2020 03:53:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:92079345-8e81-43b3-aa61-c8931422b974</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;/p&gt;
&lt;p&gt;I have solved the problem finally. It is because I missed the whitelist in the Peripheral side.&lt;br /&gt;After integrating the whitelist by referencing example ble_app_hids_keyboard, the Problem #2 is fixed.&lt;br /&gt;After some simple tests, bonded device will only connect to each other correctly.&lt;/p&gt;
&lt;p&gt;And I found a new problem about advertising on Peripheral.&lt;/p&gt;
&lt;p&gt;Background:&lt;br /&gt;There is a button on Peripheral, the button will erase the the saved bonding information on Peripheral. And the same for Central.&lt;br /&gt;After erasing, I want the device to restart scan for Central and advertising for Peripheral.&lt;/p&gt;
&lt;p&gt;Scenario:&lt;br /&gt;(Code sample are below)&lt;br /&gt;1. Peripheral and Central are bonded and connected.&lt;br /&gt;2. On Peripheral, press on the button to call advertising_start(true), which will call delete_bond().&lt;br /&gt;3. pm_peers_delete() will trigger event PM_EVT_PEERS_DELETE_SUCCEEDED in pm_evt_handler.&lt;br /&gt;4. PM_EVT_PEERS_DELETE_SUCCEEDED event will call advertising_start(false)&lt;br /&gt;5. When ble_advertising_start is called, the error NRF_ERROR_CONN_COUNT is returned.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_8.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void advertising_start(bool erase_bonds)
{
    NRF_LOG_INFO(&amp;quot;advertising_start(%d)&amp;quot;, erase_bonds);
    if (erase_bonds == true)
    {
        delete_bonds();
        // Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
    }
    else
    {
        whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR);

        ret_code_t err_code = ble_advertising_start(&amp;amp;m_advertising, BLE_ADV_MODE_FAST);
        APP_ERROR_CHECK(err_code);	// Line 1180

        m_is_advertising = true;
        NRF_LOG_INFO(&amp;quot;Advertising start&amp;quot;);
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void delete_bonds(void)
{
    ret_code_t err_code;

    NRF_LOG_INFO(&amp;quot;Erase bonds&amp;quot;);

    err_code = pm_peers_delete();
    APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void pm_evt_handler(pm_evt_t const * p_evt)
{
    pm_handler_on_pm_evt(p_evt);
    pm_handler_flash_clean(p_evt);

    switch (p_evt-&amp;gt;evt_id)
    {
        case PM_EVT_PEERS_DELETE_SUCCEEDED:
            NRF_LOG_INFO(&amp;quot;PM_EVT_PEERS_DELETE_SUCCEEDED&amp;quot;);
            advertising_start(false);
            break;
		//...
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I found that the error is from sd_ble_gap_adv_start(...), line 653 of ble_advertising.c&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if (p_advertising-&amp;gt;adv_mode_current != BLE_ADV_MODE_IDLE)
{

	ret = sd_ble_gap_adv_set_configure(&amp;amp;p_advertising-&amp;gt;adv_handle, p_advertising-&amp;gt;p_adv_data, &amp;amp;p_advertising-&amp;gt;adv_params);
	if (ret != NRF_SUCCESS)
	{
		return ret;
	}
	ret = sd_ble_gap_adv_start(p_advertising-&amp;gt;adv_handle, p_advertising-&amp;gt;conn_cfg_tag);		// Line 653

	if (ret != NRF_SUCCESS)
	{
		return ret;
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;According to nrf_error.h:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define NRF_ERROR_CONN_COUNT (NRF_ERROR_BASE_NUM + 18) ///&amp;lt; Maximum connection count exceeded.&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Does it mean that I cannot advertise when there is an active connection?&lt;br /&gt;If so, does it mean that I have to disconnect from Central before start to advertise?&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258698?ContentTypeID=1</link><pubDate>Tue, 07 Jul 2020 00:48:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b40682b7-c528-4d2d-8ebf-5172230bb909</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hi Edvin,&lt;br /&gt;&lt;br /&gt;Thank you for your reply.&lt;br /&gt;May be I have do something wrong when integrating my project code from the sample code.&lt;br /&gt;Let me try to figure it out why&amp;nbsp;&lt;span&gt;PM_EVT_CONN_SEC_CONFIG_REQ&amp;nbsp;does not appear first.&lt;br /&gt;And I will use manually erasing as you suggested for testing.&lt;br /&gt;&lt;br /&gt;Best Regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Joe&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258547?ContentTypeID=1</link><pubDate>Mon, 06 Jul 2020 09:12:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53c86fd9-2ea9-4d63-a984-bbe39609c60c</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;I don&amp;#39;t think I follow what you are doing anymore. If one of the devices still has bonding information and the other one doesn&amp;#39;t, I believe you should get an event to which you should call a reply with .allow_repairing = true or false. I believe this event is the&amp;nbsp;PM_EVT_CONN_SEC_CONFIG_REQ event.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you are not sure how whether your bonds are deleted, I suggest you work with manually erasing the chip for now, until you figure out how to handle the different setups (one device having the bonding information, while the other doesn&amp;#39;t).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258209?ContentTypeID=1</link><pubDate>Fri, 03 Jul 2020 02:29:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58b329cd-3436-4f69-be42-4546562afe71</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hello Edvin,&lt;/p&gt;
&lt;p&gt;Just like some additional observation.&lt;/p&gt;
&lt;p&gt;Instead of deleting bond by pressing button to call delete_bond from the problem, I tried Target &amp;gt; Erase All to remove the whole device memory by J-Link.&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_7.png" /&gt;&lt;/p&gt;
&lt;p&gt;(The observed result is same as previous.)&lt;br /&gt;Scenario 1:&lt;br /&gt;If I perform remove all to the Peripheral, the Central can no longer connect due to PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING.&lt;br /&gt;Scenario 2:&lt;br /&gt;On the otherhand, if I perform remove all to the Central, the Peripheral still can connect again.&lt;/p&gt;
&lt;p&gt;According to the observation, I understand the situation as follow:&lt;br /&gt;Scenario 1 proved that the Central is actually remembering the Peripheral, because when the Peripheral device is cleared the key is also lost, therefore, the &amp;quot;old&amp;quot; Central rejects the connection of the &amp;quot;new&amp;quot; Peripheral?&lt;br /&gt;Scenario 2 proved that the Peripheral is not remembering the Central, because the Peripheral can connect to a &amp;quot;new&amp;quot; Central, even it has been bonded with the &amp;quot;old&amp;quot; Central.&lt;/p&gt;
&lt;p&gt;So I guess the problem is due to some security or bonding setting?&lt;br /&gt;I think should look for the problem from the code on Peripheral side rather than on Central side.&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/258000?ContentTypeID=1</link><pubDate>Thu, 02 Jul 2020 08:34:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:93d12a74-2b1f-4b43-8f7e-21cdf31ef22b</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hello Edvin,&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have pm_conn_sec_params_reply at all in my code.&lt;br /&gt;I the only similar one is pm_conn_sec_config_reply.&lt;br /&gt;But I believe this event has never been fired, as it didn&amp;#39;t shown in the log.&lt;/p&gt;
&lt;p&gt;Here is my pm_evt_handler function of the Central device.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void pm_evt_handler(pm_evt_t const * p_evt)
{
    pm_handler_on_pm_evt(p_evt);
    pm_handler_flash_clean(p_evt);

    switch (p_evt-&amp;gt;evt_id)
    {
        case PM_EVT_PEERS_DELETE_SUCCEEDED:
            // Bonds are deleted. Start scanning.
            NRF_LOG_INFO(&amp;quot;PM_EVT_PEERS_DELETE_SUCCEEDED&amp;quot;);
            scan_start();
            break;

        case PM_EVT_CONN_SEC_CONFIG_REQ:
            NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_CONFIG_REQ&amp;quot;);
            pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};
            pm_conn_sec_config_reply(p_evt-&amp;gt;conn_handle, &amp;amp;conn_sec_config);
            break;

        case PM_EVT_BONDED_PEER_CONNECTED: // 0
            NRF_LOG_INFO(&amp;quot;PM_EVT_BONDED_PEER_CONNECTED&amp;quot;);
            break;

        case PM_EVT_CONN_SEC_START: // 1
            NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_START&amp;quot;);
            break;

        case PM_EVT_CONN_SEC_SUCCEEDED: // 2
            NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_SUCCEEDED&amp;quot;);
            break;

        case PM_EVT_CONN_SEC_FAILED: // 3
            NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_FAILED&amp;quot;);
            break;

        case PM_EVT_CONN_SEC_PARAMS_REQ: // 5
            NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_PARAMS_REQ&amp;quot;);
            break;

        case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED: // 8
            NRF_LOG_INFO(&amp;quot;PM_EVT_PEER_DATA_UPDATE_SUCCEEDED&amp;quot;);
            break;

        case PM_EVT_LOCAL_DB_CACHE_APPLIED: // 14
            NRF_LOG_INFO(&amp;quot;PM_EVT_LOCAL_DB_CACHE_APPLIED&amp;quot;);
            break;

        case PM_EVT_SLAVE_SECURITY_REQ: // 18
            NRF_LOG_INFO(&amp;quot;PM_EVT_SLAVE_SECURITY_REQ&amp;quot;);
            break;

        default:
            NRF_LOG_INFO(&amp;quot;p_evt-&amp;gt;evt_id = %d&amp;quot;, p_evt-&amp;gt;evt_id);
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If response to the pm_conn_sec_params_reply is needed, shall I just response the same setting as param_sec?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;case PM_EVT_CONN_SEC_PARAMS_REQ: // 5
	NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_PARAMS_REQ&amp;quot;);
	ble_gap_sec_params_t sec_param;

	memset(&amp;amp;sec_param, 0, sizeof(ble_gap_sec_params_t));

	// Security parameters to be used for all security procedures.
	sec_param.bond           = 1;
	sec_param.mitm           = 0;
	sec_param.lesc           = 0;
	sec_param.keypress       = 0;
	sec_param.io_caps        = BLE_GAP_IO_CAPS_NONE;
	sec_param.oob            = 0;
	sec_param.min_key_size   = 7;
	sec_param.max_key_size   = 16;
	sec_param.kdist_own.enc  = 1;
	sec_param.kdist_own.id   = 1;
	sec_param.kdist_peer.enc = 1;
	sec_param.kdist_peer.id  = 1;
	pm_conn_sec_params_reply(p_evt-&amp;gt;conn_handle, &amp;amp;sec_param, NULL);
	break;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257985?ContentTypeID=1</link><pubDate>Thu, 02 Jul 2020 07:32:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0294fc76-04da-47ad-b312-35cf2cc9d7b2</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello Joe,&lt;/p&gt;
&lt;p&gt;I am a bit confused:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="sdtjoe"]However neither Central or Peripheral the event PM_EVT_CONN_SEC_CONFIG_REQ is not being fired.[/quote]
&lt;p&gt;&amp;nbsp;But from the latest screenshot of the log, the line before &amp;quot;Unexpected Behavior&amp;quot; says:&lt;/p&gt;
&lt;p&gt;&amp;quot;PM_EVT_CONN_SEC_PARAMS_REQ&amp;quot; (not the same, but similar). From peer_manager_types.h:&lt;/p&gt;
&lt;p&gt;/**&amp;lt; @brief Security parameters (@ref ble_gap_sec_params_t) are needed for an ongoing security procedure. Reply with @ref pm_conn_sec_params_reply before the event handler returns. If no reply is sent, the parameters given in @ref pm_sec_params_set are used. If a peripheral connection, the central&amp;#39;s sec_params will be available in the event. */&lt;/p&gt;
&lt;p&gt;Do you reply with pm_conn_sec_params_reply? Do you get any events on the peripheral at this point in time?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Where did you set .allow_repairing = true in the central?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257958?ContentTypeID=1</link><pubDate>Thu, 02 Jul 2020 03:50:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c18abb9f-ed34-45ee-bbb6-2dfcc763fd71</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hello Edvin,&lt;/p&gt;
&lt;p&gt;Thank you for your reply.&lt;/p&gt;
&lt;p&gt;For Problem #1 (Solved):&lt;br /&gt;I have followed your suggestion and ignore the COMMUNICATION_ERROR unless the device is connected.&lt;br /&gt;It works correctly and I applied this solution as I believe this is a better workaround.&lt;/p&gt;
&lt;p&gt;For Problem #2:&lt;br /&gt;I have added a log to print when PM_EVT_CONN_SEC_CONFIG_REQ is being called in pm_evt_handler.&lt;br /&gt;However neither Central or Peripheral the event PM_EVT_CONN_SEC_CONFIG_REQ is not being fired.&lt;br /&gt;So I think the allow_pairing did not set at all.&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;case PM_EVT_CONN_SEC_CONFIG_REQ:
NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_CONFIG_REQ&amp;quot;);
pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};
pm_conn_sec_config_reply(p_evt-&amp;gt;conn_handle, &amp;amp;conn_sec_config);
break;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Here is additional information that may help.&lt;br /&gt;I think I am using a Just Work Bonding. My sec_param is set as follow:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;sec_param.bond = 1;
sec_param.mitm = 0;
sec_param.lesc = 0;
sec_param.keypress = 0;
sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
sec_param.oob = 0;
sec_param.min_key_size = 7;
sec_param.max_key_size = 16;
sec_param.kdist_own.enc = 1;
sec_param.kdist_own.id = 1;
sec_param.kdist_peer.enc = 1;
sec_param.kdist_peer.id = 1;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In addition, I call delete_bonds() function for both Central and Peripheral.&lt;br /&gt;It will call pm_peer_delete() same as sample code.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void delete_bonds(void)
{
ret_code_t err_code;

NRF_LOG_INFO(&amp;quot;Erase bonds&amp;quot;);

err_code = pm_peers_delete();
APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Here is my steps and observation:&lt;br /&gt;Scenario 1 (No problem at all):&lt;br /&gt;1. Turn on both devices which are never connected.&lt;br /&gt;2. They will bond.&lt;br /&gt;3. Turn off either Central / Peripheral and they will connect again.&lt;br /&gt;4. Press delete button on Peripheral side, PM_EVT_PEERS_DELETE_SUCCEEDED event is fired.&lt;br /&gt;5. Try to connect again, PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING, which is my expected behavior.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_5.png" /&gt;&lt;/p&gt;
&lt;p&gt;Scenario 2 (Unexpected behavior):&lt;br /&gt;1. Turn on both devices which are never connected.&lt;br /&gt;2. They will bond.&lt;br /&gt;3. Turn off either Central / Peripheral and they will connect again.&lt;br /&gt;4. Press delete button on Central side, PM_EVT_PEERS_DELETE_SUCCEEDED event is fired.&lt;br /&gt;5. Try to connect again, the devices connected successfully. But I expect it should be rejected as I have deleted the Central bonding in step 4.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_6.png" /&gt;&lt;/p&gt;
&lt;p&gt;Maybe I have missed some setting on either side?&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257627?ContentTypeID=1</link><pubDate>Tue, 30 Jun 2020 14:09:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6024e01f-df98-4671-8770-1d87e09a2228</guid><dc:creator>Edvin</dc:creator><description>[quote user="sdtjoe"]Thank you for your suggestion.&lt;br /&gt;I have moved the uart_init() from main to the event handler of event BLE_GAP_EVT_CONNECTED, so the UART will only init after BLE is connected. And also I have created a flag to make sure it run only once.&lt;br /&gt;And the problem seems to be fixed. I will do further test on this later on.[/quote]
&lt;p&gt;&amp;nbsp;Good! An alternative is to just ignore the COMMUNICATION_ERROR event unless the device is connected. You can experiment with different workarounds.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Make sure the allow_repairing is set to true in the device that did not lose the bonding information. Or set it in both devices, if there is a chance that they both lose it.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Does the PM_EVT_CONN_SEC_CONFIG_REQ event occur in any of the devices?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;BR,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257506?ContentTypeID=1</link><pubDate>Tue, 30 Jun 2020 08:38:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b5eaa35-960d-4324-9cfa-98476c7bf742</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hello Edvin,&lt;/p&gt;
&lt;p&gt;Thank you for your reply.&lt;/p&gt;
&lt;p&gt;Actually the Central device is a USB dongle.&lt;br /&gt;What my project do is:&lt;br /&gt;Peripheral will send data through BLE NUS, and the Central device will receive the data and transfer it through UART to PC.&lt;/p&gt;
&lt;p&gt;For Problem #1:&lt;br /&gt;Thank you for your suggestion.&lt;br /&gt;I have moved the uart_init() from main to the event handler of event BLE_GAP_EVT_CONNECTED, so the UART will only init after BLE is connected. And also I have created a flag to make sure it run only once.&lt;br /&gt;And the problem seems to be fixed. I will do further test on this later on.&lt;/p&gt;
&lt;p&gt;For Problem #2:&lt;br /&gt;Would the following description be one of the possibility of this problem?&lt;br /&gt;At first devices bonding are done, so the key is establish and shared.&lt;br /&gt;But either one of them does not save permanently in flash storage, so after power off, one of them has lost the key?&lt;br /&gt;If so, is there anyway I can check if the device has stored the key?&lt;/p&gt;
&lt;p&gt;Actually in pm_evt_handler, I have added allow_repairing for both Peripheral and Central.&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;case PM_EVT_CONN_SEC_CONFIG_REQ:
NRF_LOG_INFO(&amp;quot;PM_EVT_CONN_SEC_CONFIG_REQ&amp;quot;);
pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};
pm_conn_sec_config_reply(p_evt-&amp;gt;conn_handle, &amp;amp;conn_sec_config);
break;&lt;/pre&gt;&lt;br /&gt;But from the log, I did not see any log message &amp;quot;PM_EVT_CONN_SEC_CONFIG_REQ&amp;quot;.&lt;br /&gt;So maybe this part of code is not being called?&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257358?ContentTypeID=1</link><pubDate>Mon, 29 Jun 2020 11:55:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c73c99b4-55ef-4a95-ac87-584e20f79944</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello Joe,&lt;/p&gt;
&lt;p&gt;Problem #1:&lt;/p&gt;
&lt;p&gt;The value in p_evt-&amp;gt;data.error_communication refers to the errorsrc register on the UART peripheral:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/uarte.html?cp=4_2_0_34_9_4#register.ERRORSRC"&gt;https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/uarte.html?cp=4_2_0_34_9_4#register.ERRORSRC&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;When this is 12, it means you have a framing error. Are your UART pins connected to anything? If not, perhaps you should skip the uart_init(), if you are not using it.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Problem #2:&lt;/p&gt;
&lt;p&gt;At this point, does one of the devices contain bonding information, while the other doesn&amp;#39;t?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/257290?ContentTypeID=1</link><pubDate>Mon, 29 Jun 2020 08:29:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:56d9428b-1a3d-494e-9c39-e38071621cb7</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;Sorry I was busy on other project so I was unable to update the progress.&lt;/p&gt;
&lt;p&gt;After referencing the examples, looks like I am able to bond two nRF52 devices.&lt;br /&gt;According to the log, the peer data was updated in flash.&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_1.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;If I just turn the Peripheral device off and on again, the Central remain on, the devices reconnected successfully and no change is made in flash.&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_2.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Problem #1:&lt;br /&gt;However, if I turn the Central off, and turn it on again, it shows the following error:&lt;br /&gt;ERROR 12 [NRF_ERROR_DATA_SIZE]&lt;br /&gt;(The error is occurred on Central side, not Peripheral side)&lt;br /&gt;(The Central program is based on the example ble_uart_c)&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_3.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;case APP_UART_COMMUNICATION_ERROR:
NRF_LOG_ERROR(&amp;quot;Communication error occurred while handling UART.&amp;quot;);
APP_ERROR_HANDLER(p_event-&amp;gt;data.error_communication); // Line 1006
break;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;And I can only erase the Central and re-download the program again.&lt;br /&gt;I have read several posts talking about this.&lt;br /&gt;Most are on Peripheral side, but mine is on Central side.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Problem #2:&lt;br /&gt;After problem #1 occured, the pairing no long success.&lt;br /&gt;When trying to connect, the error of 4102 is returned.&lt;br /&gt;4102 =&amp;gt; 0x1006 =&amp;gt; PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/bonding_5F00_4.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Is there any hint on the above two problems?&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Best Regards&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/254661?ContentTypeID=1</link><pubDate>Fri, 12 Jun 2020 10:12:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9b08535b-2f44-4ab6-88bd-7bc2fba3874c</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;When you see the &amp;quot;fatal error&amp;quot; in the log, define DEBUG in your preprocessor definitions, and the log should point to where the error is coming from.&lt;/p&gt;
&lt;p&gt;For the bonding questions, I suggest you look into some of the examples that use bonding. A peripheral that use this is the ble_app_gls, and a central project that use it is the ble_app_hrs_c.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="sdtjoe"]To create a bonding, do I need to implement on both side (Peripheral and Central)? Or i just have to implement on one side, say the Central device?[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Both sides. If not, then the devices without the whitelist will always accept connections from other devices.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/254631?ContentTypeID=1</link><pubDate>Fri, 12 Jun 2020 08:58:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ae00de91-f141-48fd-8cd0-5270664dc62e</guid><dc:creator>sdtjoe</dc:creator><description>&lt;p&gt;Hi Edvin&lt;/p&gt;
&lt;p&gt;Thank you for your reply.&lt;/p&gt;
&lt;p&gt;Looks like the connection is disconnected due to fatal error on Peripheral.&lt;br /&gt;Here is the RTT log of Peripheral.&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/ble_5F00_bonding5.png" /&gt;&lt;br /&gt;I cannot figure it out where did it come from yet. When I try to debug, it always jump to NRF_BREAKPOINT_COND.&lt;br /&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/ble_5F00_bonding4.png" /&gt;&lt;/p&gt;
&lt;p&gt;And some supplementary about the devices behavior:&lt;br /&gt;Both Peripheral and Central are nRF52 devices. If Central is not paired, it keeps scanning for Peripheral. On the other hand, when Peripheral is not paired, it keeps advertising until it is connected. So basically when both are power on, it should be connected immediately.&lt;/p&gt;
&lt;p&gt;That mean to archive my goal, I have to:&lt;br /&gt;Once the bonding is completed, I can &amp;quot;add it to whitelist&amp;quot;.&lt;br /&gt;So it will always only connect to each other, unless I clear the bonding (the devices become a unpaired &amp;quot;new device&amp;quot;)&lt;/p&gt;
&lt;p&gt;To create a bonding, do I need to implement on both side (Peripheral and Central)? Or i just have to implement on one side, say the Central device?&lt;/p&gt;
&lt;p&gt;Best Regards&lt;br /&gt;Joe&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Questions about implementing BLE bonding with NUS</title><link>https://devzone.nordicsemi.com/thread/254503?ContentTypeID=1</link><pubDate>Thu, 11 Jun 2020 13:06:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58e40e05-218b-42a3-bb6b-ecd8e1613b0b</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;You can see where this error is printed:&lt;/p&gt;
&lt;p&gt;It is from the&amp;nbsp;PM_EVT_CONN_SEC_FAILED event in&amp;nbsp;pm_handler_pm_evt_log() from peer_manager_handler.c:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;            NRF_LOG_INFO(&amp;quot;Connection security failed: role: %s, conn_handle: 0x%x, procedure: %s, error: %d&amp;quot;,
                         m_roles_str[ble_conn_state_role(p_pm_evt-&amp;gt;conn_handle)],
                         p_pm_evt-&amp;gt;conn_handle,
                         m_sec_procedure_str[p_pm_evt-&amp;gt;params.conn_sec_start.procedure],
                         p_pm_evt-&amp;gt;params.conn_sec_failed.error);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can see that the error number is printed as a decimal value (%d), so this means that the decimal value is 0x1100. So what does that mean?&lt;/p&gt;
&lt;p&gt;Looking at the type of the&amp;nbsp;p_pm_evt-&amp;gt;params.conn_sec_failed.error, which is an&amp;nbsp;pm_sec_error_code_t, you can see from the description that it is:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Errors from security procedures in Peer Manager.
 *
 * @details Possible values are defined in @ref PM_SEC_ERRORS and @ref BLE_GAP_SEC_STATUS.
 */
typedef uint16_t pm_sec_error_code_t;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;(from peer_manager_types.h, line 74-78)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So from&amp;nbsp;@defgroup PM_SEC_ERRORS:&lt;/p&gt;
&lt;p&gt;#define PM_CONN_SEC_ERROR_DISCONNECT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(PM_CONN_SEC_ERROR_BASE + 0x100) /**&amp;lt; @brief Pairing or encryption did not finish before the link disconnected for an unrelated reason. */&lt;/p&gt;
&lt;p&gt;(PM_CONN_SEC_ERROR_BASE is 0x1000)&lt;/p&gt;
&lt;p&gt;So the error you receive is because of the disconnect.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Are you prompted with any pairing questions when connecting with nRF Connect for Desktop? Did you try to click the settings icon of the connection and click pair (and check of &amp;quot;bond&amp;quot;)?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]2. Actually what I would like to archive is that I want the previously paired devices remember each other. Just like our earphone always connect to our smartphone when both power on. Therefore even if there are multiple peripherals and centrals are power on, the devices will not paired to a device that is not paired before.&lt;br /&gt;As far as my understanding, the original example of NUS, Central will search for any Peripheral device with NUS UUID. So I cannot guarantee which device connect to which if I have multiple Peripheral advertising and Central scanning at the same time. So I googled and found bonding seems to be the solution. Is my understanding correct? Please correct me if I am wrong.[/quote]
&lt;p&gt;&amp;nbsp;That is almost correct. Bonding means that you store the encryption keys of the connection. After you have bonded, either your central or peripheral (or both) may use something called a whitelist when it is scanning/advertising. If you do this, it will only accept connections to that device.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;That being said. Even if you bond with a phone, it doesn&amp;#39;t mean that this phone will try to connect to the peripheral whenever it is advertising. I don&amp;#39;t know the exact details about earphones, because that is not BLE, but Classic Bluetooth. However, I suspect that it is similar to HID devices. You will see that if you try to flash the ble_app_hids_keyboard example, the phone will automatically reconnect to that device. The reason for this is that it is a HID [device] (Human Interface Device), and the phone&amp;#39;s OS finds it useful. However, if you use most other examples, the phone will not automatically connect even though they are bonded. If you want something custom to automatically connect to your phone, you must write an application on the phone that will scan for and connect to your peripheral.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Back to whitelists: The main purpose of whitelists is to look for specific devices, and deny connection from others. One example that uses a whitelist is the ble_app_gls, and the ble_app_hrs_c.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>