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

remove bond in CTS

Hi, there

     I want to remove the bond in current time service example, because the bond process make me less convenient when I debug. so I changed the code in peer_manager_init 

sec_param.bond = 0;
sec_param.mitm = SEC_PARAM_MITM;
sec_param.lesc = SEC_PARAM_LESC;
sec_param.keypress = SEC_PARAM_KEYPRESS;
sec_param.io_caps = SEC_PARAM_IO_CAPABILITIES;
sec_param.oob = SEC_PARAM_OOB;
sec_param.min_key_size = SEC_PARAM_MIN_KEY_SIZE;
sec_param.max_key_size = SEC_PARAM_MAX_KEY_SIZE;
sec_param.kdist_own.enc = 0;
sec_param.kdist_own.id = 0;
sec_param.kdist_peer.enc = 0;
sec_param.kdist_peer.id = 0;

and comment the peer_manager part in advertising_start

static void advertising_start(bool erase_bonds)
{
if (erase_bonds == true)
{
delete_bonds();
// Advertising is started by PM_EVT_PEERS_DELETE_SUCCEEDED event.
}
else
{
ret_code_t ret;

// memset(m_whitelist_peers, PM_PEER_ID_INVALID, sizeof(m_whitelist_peers));
// m_whitelist_peer_cnt = (sizeof(m_whitelist_peers) / sizeof(pm_peer_id_t));

// peer_list_get(m_whitelist_peers, &m_whitelist_peer_cnt);

// ret = pm_whitelist_set(m_whitelist_peers, m_whitelist_peer_cnt);
// APP_ERROR_CHECK(ret);

// // Setup the device identies list.
// // Some SoftDevices do not support this feature.
// ret = pm_device_identities_list_set(m_whitelist_peers, m_whitelist_peer_cnt);
// if (ret != NRF_ERROR_NOT_SUPPORTED)
// {
// APP_ERROR_CHECK(ret);
// }

ret = ble_advertising_start(&m_advertising, BLE_ADV_EVT_SLOW);
APP_ERROR_CHECK(ret);
}
}

it can advertise, connect and discover service in nRF connect on my phone, but my phone attempt to pairing and bonding , but failed and then the MCU stopped. so how to stop the phone's attempts to pairing and make the cts service work ?

how to remove the bonding procedure   completely?

Parents
  • Hi,

    Try this:

          // Pairing, no bonding.
          sec_param.bond = false;
          sec_param.mitm = false;
          sec_param.lesc = 0;
          sec_param.keypress = 0;
          sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
          sec_param.oob = false;
          sec_param.min_key_size = 7;
          sec_param.max_key_size = 16;
          sec_param.kdist_own.enc = 0;
          sec_param.kdist_own.id = 0;
          sec_param.kdist_peer.enc = 0;
          sec_param.kdist_peer.id = 0;

    Other steps you should do:

    1) In sec_req_timeout_handler() comment out pm_conn_secure().

    Snippet:

    static void sec_req_timeout_handler(void * p_context)
    {
        ret_code_t           err_code;
        pm_conn_sec_status_t status;
    
        if (m_cur_conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            err_code = pm_conn_sec_status_get(m_cur_conn_handle, &status);
            APP_ERROR_CHECK(err_code);
    
            // If the link is still not secured by the peer, initiate security procedure.
            if (!status.encrypted)
            {
             //   err_code = pm_conn_secure(m_cur_conn_handle, false);
             //   APP_ERROR_CHECK(err_code);
            }
        }
    }
    

    2) On the event BLE_GAP_EVT_CONNECTED, start discovery of peer's services with ble_db_discovery_start()

    Snippet:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected.");
                err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                APP_ERROR_CHECK(err_code);
                m_cur_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = app_timer_start(m_sec_req_timer_id, SECURITY_REQUEST_DELAY, NULL);
                APP_ERROR_CHECK(err_code);
                // Discover peer's services.
                err_code  = ble_db_discovery_start(&m_ble_db_discovery, p_ble_evt->evt.gap_evt.conn_handle);
                APP_ERROR_CHECK(err_code);
    
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected.");
                m_cur_conn_handle = BLE_CONN_HANDLE_INVALID;
                if (p_ble_evt->evt.gap_evt.conn_handle == m_cts_c.conn_handle)
                {
                    m_cts_c.conn_handle = BLE_CONN_HANDLE_INVALID;
                }
                break; // BLE_GAP_EVT_DISCONNECTED
    
    #ifndef S140
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                NRF_LOG_DEBUG("PHY update request.");
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);
            } break;
    #endif
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                NRF_LOG_DEBUG("GATT Client Timeout.");
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_TIMEOUT:
                // Disconnect on GATT Server timeout event.
                NRF_LOG_DEBUG("GATT Server Timeout.");
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }

    3) In advertising_init(), turn off whitelist advertising by setting ble_adv_whitelist_enabled to false

    Snippet:

    static void advertising_init()
    {
        ret_code_t             err_code;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type                = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance       = true;
        init.advdata.flags                    = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
        init.advdata.uuids_solicited.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.advdata.uuids_solicited.p_uuids  = m_adv_uuids;
    
        init.config.ble_adv_whitelist_enabled = false;
        init.config.ble_adv_fast_enabled      = true;
        init.config.ble_adv_fast_interval     = APP_ADV_FAST_INTERVAL;
        init.config.ble_adv_fast_timeout      = APP_ADV_FAST_TIMEOUT;
        init.config.ble_adv_slow_enabled      = true;
        init.config.ble_adv_slow_interval     = APP_ADV_SLOW_INTERVAL;
        init.config.ble_adv_slow_timeout      = APP_ADV_SLOW_TIMEOUT;
    
        init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }

Reply
  • Hi,

    Try this:

          // Pairing, no bonding.
          sec_param.bond = false;
          sec_param.mitm = false;
          sec_param.lesc = 0;
          sec_param.keypress = 0;
          sec_param.io_caps = BLE_GAP_IO_CAPS_NONE;
          sec_param.oob = false;
          sec_param.min_key_size = 7;
          sec_param.max_key_size = 16;
          sec_param.kdist_own.enc = 0;
          sec_param.kdist_own.id = 0;
          sec_param.kdist_peer.enc = 0;
          sec_param.kdist_peer.id = 0;

    Other steps you should do:

    1) In sec_req_timeout_handler() comment out pm_conn_secure().

    Snippet:

    static void sec_req_timeout_handler(void * p_context)
    {
        ret_code_t           err_code;
        pm_conn_sec_status_t status;
    
        if (m_cur_conn_handle != BLE_CONN_HANDLE_INVALID)
        {
            err_code = pm_conn_sec_status_get(m_cur_conn_handle, &status);
            APP_ERROR_CHECK(err_code);
    
            // If the link is still not secured by the peer, initiate security procedure.
            if (!status.encrypted)
            {
             //   err_code = pm_conn_secure(m_cur_conn_handle, false);
             //   APP_ERROR_CHECK(err_code);
            }
        }
    }
    

    2) On the event BLE_GAP_EVT_CONNECTED, start discovery of peer's services with ble_db_discovery_start()

    Snippet:

    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        ret_code_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected.");
                err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                APP_ERROR_CHECK(err_code);
                m_cur_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = app_timer_start(m_sec_req_timer_id, SECURITY_REQUEST_DELAY, NULL);
                APP_ERROR_CHECK(err_code);
                // Discover peer's services.
                err_code  = ble_db_discovery_start(&m_ble_db_discovery, p_ble_evt->evt.gap_evt.conn_handle);
                APP_ERROR_CHECK(err_code);
    
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected.");
                m_cur_conn_handle = BLE_CONN_HANDLE_INVALID;
                if (p_ble_evt->evt.gap_evt.conn_handle == m_cts_c.conn_handle)
                {
                    m_cts_c.conn_handle = BLE_CONN_HANDLE_INVALID;
                }
                break; // BLE_GAP_EVT_DISCONNECTED
    
    #ifndef S140
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                NRF_LOG_DEBUG("PHY update request.");
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);
            } break;
    #endif
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                NRF_LOG_DEBUG("GATT Client Timeout.");
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_TIMEOUT:
                // Disconnect on GATT Server timeout event.
                NRF_LOG_DEBUG("GATT Server Timeout.");
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    }

    3) In advertising_init(), turn off whitelist advertising by setting ble_adv_whitelist_enabled to false

    Snippet:

    static void advertising_init()
    {
        ret_code_t             err_code;
        ble_advertising_init_t init;
    
        memset(&init, 0, sizeof(init));
    
        init.advdata.name_type                = BLE_ADVDATA_FULL_NAME;
        init.advdata.include_appearance       = true;
        init.advdata.flags                    = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
        init.advdata.uuids_solicited.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
        init.advdata.uuids_solicited.p_uuids  = m_adv_uuids;
    
        init.config.ble_adv_whitelist_enabled = false;
        init.config.ble_adv_fast_enabled      = true;
        init.config.ble_adv_fast_interval     = APP_ADV_FAST_INTERVAL;
        init.config.ble_adv_fast_timeout      = APP_ADV_FAST_TIMEOUT;
        init.config.ble_adv_slow_enabled      = true;
        init.config.ble_adv_slow_interval     = APP_ADV_SLOW_INTERVAL;
        init.config.ble_adv_slow_timeout      = APP_ADV_SLOW_TIMEOUT;
    
        init.evt_handler = on_adv_evt;
    
        err_code = ble_advertising_init(&m_advertising, &init);
        APP_ERROR_CHECK(err_code);
    
        ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }

Children
Related