蓝牙主动断开,设备同意解绑
authorxue <2144439972@qq.com>
Fri, 16 Jan 2026 07:18:51 +0000 (15:18 +0800)
committerxue <2144439972@qq.com>
Fri, 16 Jan 2026 07:18:51 +0000 (15:18 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp

index 679437d..0f53a6a 100644 (file)
@@ -948,7 +948,7 @@ static void ACLConnectionSuss(const bd_addr_t &bdAddr)
 }
 
 // 回调:ACL连接失败
-static void ACLConnectionFail(const bd_addr_t &bdAddr)
+static void ACLConnectionFail(const bd_addr_t &bdAddr, bool isPeerDisconnect = false)
 {
     // 如果当前正处于“切换到新耳机”的流程中(用户点了另一台设备准备自动重连),
     // 就不要弹出“连接已断开”页面,直接走后续新设备连接流程,避免多余页面跳转。
@@ -972,6 +972,18 @@ static void ACLConnectionFail(const bd_addr_t &bdAddr)
     }
     if(strcmp((const char *)caseInfo.deviceName, BluetoothModel::GetInstance()->GetLastConnedDeviceName()) == 0)
     {
+        // 如果是耳机端主动断开连接,需要解绑设备
+        if (isPeerDisconnect) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ACLConnectionFail: 耳机端自动断开连接,执行解绑操作");
+            // 将 bd_addr_t 转换为字符串格式(MAC地址格式:AA:BB:CC:DD:EE:FF)
+            char macStr[32] = {0};
+            snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X",
+                     bdAddr.addr[5], bdAddr.addr[4], bdAddr.addr[3],
+                     bdAddr.addr[2], bdAddr.addr[1], bdAddr.addr[0]);
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ACLConnectionFail: 解绑设备 MAC: %s", macStr);
+            // 执行解绑操作
+            BluetoothModel::GetInstance()->UnBindRemoteDevice(macStr);
+        }
         
         //亮屏操作
         uapi_set_screen_on_api();
@@ -993,8 +1005,10 @@ void BluetoothModel::BtGapAclStateChangedHandle(gap_acl_state_t state, unsigned
 {
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BtGapAclStateChangedHandle reason is %x", reason);
     bool tag = BluetoothModel::GetInstance()->IsTagScanDeviceClicked();
+    bool isPeerDisconnect = false; // 是否是耳机端主动断开
     if ((reason == 0x13) || (reason == 0x14) || (reason == 0x15) || (reason == 0x3D) || (reason == 0x08 && (!tag))) { // 连接断开
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "蓝牙设备主动断开连接 reason is %x", reason);
+        isPeerDisconnect = true; // 标记为耳机端主动断开
     } else if (reason == 0x16) { // 主动断开连接
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "主动断开与蓝牙设备连接,reason is%x", reason);
         BluetoothCaseInfo *onClickBtCaseInfo = BluetoothModel::GetInstance()->GetTempMacValue();
@@ -1049,7 +1063,7 @@ void BluetoothModel::BtGapAclStateChangedHandle(gap_acl_state_t state, unsigned
     }
     else
     {
-        ACLConnectionFail(bdAddr);
+        ACLConnectionFail(bdAddr, isPeerDisconnect);
     }
 
     if ((state == GAP_ACL_STATE_CONNECTED) || (state == GAP_ACL_STATE_LE_CONNECTED)) {
@@ -1204,7 +1218,7 @@ static void A2dpSrcConnectstateChanged(bd_addr_t *bdAddr, int connState)
         set_a2dp_is_connect(false);
 
  
-        GraphicService::GetInstance()->PostGraphicEvent(std::bind(ACLConnectionFail, *bdAddr));
+        GraphicService::GetInstance()->PostGraphicEvent(std::bind(ACLConnectionFail, *bdAddr, false));
         int earphoneConnectStatus = 0;
         SendMsgToUIService(HAND_BLUETOOTH_EARPHONE_CONNECT_STATUS_NOTIFY, 0, &earphoneConnectStatus, sizeof(earphoneConnectStatus));
         /* 实验室测试耳机回连场景,用户根据需要调整策略 */