蓝牙外围设备丢LTK的处理方法
案例使用的Nordic蓝牙芯片进行开发
- 参数说明
PM_LINK_SECURED_PROCEDURE_ENCRYPTION, /**< @brief Using an LTK that was shared during a previous bonding procedure to encrypt the link. */`在这里插入代码片`
#define PM_CONN_SEC_ERROR_PIN_OR_KEY_MISSING (PM_CONN_SEC_ERROR_BASE + 0x06) /**< @brief Encryption failed because the peripheral has lost the LTK for this bond. See also @ref BLE_HCI_STATUS_CODE_PIN_OR_KEY_MISSING and Table 3.7 ("Pairing Failed Reason Codes") in the Bluetooth Core Specification 4.2, section 3.H.3.5.5 (@linkBLEcore). */
- 事件处理方法
case PM_EVT_CONN_SEC_FAILED:
{
BLEDBG_STR("PM_EVT_CONN_SEC_FAILED\r\n");
BLEDBG_FMTSTR("peer_id=0x%04x, error=0x%04x\r\n",p_evt->peer_id, p_evt->params.conn_sec_failed.error);
BLEDBG_FMTSTR("procedure=%d\r\n",p_evt->params.conn_sec_failed.procedure);
/* Often, when securing fails, it shouldn't be restarted, for security reasons.
* Other times, it can be restarted directly.
* Sometimes it can be restarted, but only after changing some Security Parameters.
* Sometimes, it cannot be restarted until the link is disconnected and reconnected.
* Sometimes it is impossible, to secure the link, or the peer device does not support it.
* How to handle this error is highly application dependent. */
ble_cntrlinfo.flg_commFail = TRUE; //置上通讯失败标志位
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
BLEDBG_STR("Waiting for host to fix bonding\r\n");
}
else
{ //IOS手机主动点击了取消配对,设备端需要断开连接,
sprintf(m_message, "Security procedure failed, disconnect.\r\n");
dev_ctrl_send_msg(m_message, strlen(m_message));//这里可以增加回复信息给手机
err_code = sd_ble_gap_disconnect(p_evt->conn_handle,BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_INVALID_CONN_HANDLE))
{
APP_ERROR_CHECK(err_code);
}
}
} break;