BLE主机或从机绑定信息丢失问题

在Nordic产品开发中,有遇到主机和从机之间建立绑定,以及当某一方(主/从机)的绑定信息丢失后出现连接失败问题,有以下几种应用场景。针对不同的场景需修改或添加部分代码,以下代码是基于nRF5_SDK_15.3.0_59ac345\examples\ble_peripheral\ble_app_ancs_c 示例,主要修改main.c 文件的pm_evt_handler函数里面的内容。

场景1: 主机端删除了绑定信息,但是nRF52从机端仍然保留了原绑定信息, nRF52从机需要允许重新配对,通过新的绑定信息去覆盖原来的绑定信息,需对代码进行以下修改:

        case PM_EVT_CONN_SEC_CONFIG_REQ:

        {

            NRF_LOG_INFO("PM_EVT_CONN_SEC_CONFIG_REQ: peer_id=%d, accept to fix bonding\r\n",

                           p_evt->peer_id);

            // Accept pairing request from an already bonded peer.

            pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};

            pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config);

        } break;

在重新配对操作时需要对增加的白名单做个判断,即已经添加过的记录不应当再加进来,基本的代码逻辑如下:

already_added= false;
for (uint8_t i = 0; i<m_whitelist_peer_cnt;i++)
{
    if (m_whitelist_peers[i] = m_peer_id)
    {
        already_added= true;
        break;
    }
}
       
if (!already_added)
{
   m_whitelist_peers[m_whitelist_peer_cnt++] = m_peer_id;
   err_code= pm_whitelist...
   。。。。。。
}

 场景2: 主机端保留了原绑定信息,但是nRF52从机端删除了绑定信息,nRF52端需要在密码较验失败时仍保持连接,代码修改如下:

      case PM_EVT_CONN_SEC_FAILED:

        {

            NRF_LOG_INFO("PM_EVT_CONN_SEC_FAILED: peer_id=%d, procedure=%d, error=0x%04x\r\n",

                          p_evt->peer_id,

                          p_evt->params.conn_sec_failed.procedure,

                          p_evt->params.conn_sec_failed.error);

            if (p_evt->params.conn_sec_failed.procedure == PM_LINK_SECURED_PROCEDURE_ENCRYPTION &&

                p_evt->params.conn_sec_failed.error == PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING)

            {

                // Local device lost bond info, don't disconnect and wait for re-bond

                NRF_LOG_INFO("Waiting for host to fix bonding\r\n");

            }

            else

            {

              sprintf(m_message, "Security procedure failed, disconnect.\r\n");

              dev_ctrl_send_msg(m_message, strlen(m_message));

              (void)sd_ble_gap_disconnect(p_evt->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

            }

        } break;

 

主机端如果是手机或平板电脑,根据操作系统的区别有不同的处理方式: 

iOS:    没有解决方案,只能通过手动删除主机端绑定信息 。

Android:   可以执行新的绑定程序,删除掉旧的绑定信息。

如果主机端为nRF52 设备时,可以强制重新配对并建立绑定连接产生新的绑定信息, 修改代码如下:

       case PM_EVT_CONN_SEC_FAILED:

        {

            NRF_LOG_INFO("PM_EVT_CONN_SEC_FAILED: peer_id=%d, procedure=%d, error=0x%04x\r\n",

                          p_evt->peer_id,

                          p_evt->params.conn_sec_failed.procedure,

                          p_evt->params.conn_sec_failed.error);

            if (p_evt->params.conn_sec_failed.procedure == PM_LINK_SECURED_PROCEDURE_ENCRYPTION &&

                p_evt->params.conn_sec_failed.error == PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING)

            {

                // Peer device lost bond info, do re-bonding

                NRF_LOG_INFO("Peer lost bond info. Start re-bonding\r\n");

                err_code = pm_conn_secure(p_evt->conn_handle, true);

                if (err_code != NRF_SUCCESS)

                {

                    NRF_LOG_WARNING("Cannot fix out-of-sync bonding: 0x%08x\r\n", err_code);

                }

            }

            else

            {

                sprintf(m_message, "Connection failed, disconnect.\r\n");

                dev_ctrl_send_msg(m_message, strlen(m_message));

                (void)sd_ble_gap_disconnect(p_evt->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);

            }

        } break;

概述如下:

场景

iOS

Android

nRF52

Comment

主机丢失绑定信息

O

O

O

i.e. Central side bond info is removed

从机丢失绑定信息

X

O

O

i.e. Peripheral side bond info is removed

posted @ 2019-07-04 15:32  SZ_LM  阅读(2287)  评论(0编辑  收藏  举报