zoukankan      html  css  js  c++  java
  • BLE主机或从机绑定信息丢失问题

    在Nordic产品开发中,有遇到主机和从机之间建立绑定,以及当某一方(主/从机)的绑定信息丢失后出现连接失败问题,有以下几种应用场景。针对不同的场景需修改或添加部分代码,以下代码是基于nRF5_SDK_15.3.0_59ac345examplesle_peripheralle_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 ",

                               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 ",

                              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 ");

                }

                else

                {

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

                  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 ",

                              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 ");

                    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 ", err_code);

                    }

                }

                else

                {

                    sprintf(m_message, "Connection failed, disconnect. ");

                    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

  • 相关阅读:
    QQ家园熄灭不了解决方法
    那时我们还年轻[转]
    QQ游戏图标熄灭大全
    FlashDevelop快捷键
    linux 全局搜索某一文件并将文件内容并进行替换的命令
    navigate 10.0.5 regist cn
    线程、socket、stl 以及并发设计
    drupal真不错
    网卡问题解决思路linux版
    socket错误:Program received signal SIGPIPE, Broken pipe
  • 原文地址:https://www.cnblogs.com/lim11/p/11132131.html
Copyright © 2011-2022 走看看