This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[FAQ] 使用软件断开BLE连接

在某些时候,连接已经完成了工作,需要断开连接,可以通过Btool或者从Peripheral设备发送GAP_TerminateLinkReq()来终止连接。

一、使用Btool断开连接

在BTool中,只需按Terminate按钮即可断开连接,此外请确保输入正确的连接句柄。这时会触发一个GAP_TerminateLink事件,reason为0x16(十进制为22)。

 

二、从Peripheral设备终止连接

可以在程序中添加一个按键,使其按下时断开连接,使用GAP_TerminateLinkReq()函数告诉GAP终止连接。该消息连同连接终止的原因一起被广播到central设备,同时GAP_LINK_TERMINATED_EVENT在GAP消息事件队列中触发(无论是否终止,都会触发此事件)。

使用CC13X2或CC26X2 Launchpad上的左边的按键BTN-1( CONFIG_PIN_BTN1)调用函数GAP_TerminateLinkReq()。

以例程project_zero为例

在project_zero.c中找到ProjectZero_handleButtonPress(),添加以下代码:

case CONFIG_PIN_BTN1:

       ButtonService_SetParameter(BS_BUTTON0_ID,

                                   sizeof(pState->state),

                                   &pState->state);

       uint8_t i;

       for(i = 0; i < MAX_NUM_BLE_CONNS; i++)

       {

//For existing connections, terminate the link.

           if (connList[i].connHandle != 0xFFFF)

           {

               GAP_TerminateLinkReq(connList[i].connHandle, HCI_DISCONNECT_REMOTE_USER_TERM);

               Log_info0("Terminating connection(s).");

           }

       }

       break;

重新编译并下载到开发板,若烧录后没有广播,则需要先烧录bim文件,从以下路径导入bim 文件C:\ti\simplelink_cc13x2_26x2_sdk_4_40_04_04\examples\nortos\CC26X2R1_LAUNCHXL\bim编译后下载,按下BTN-1可以在Btool中可以看到连接终止,reason为0x13(Peer requested)