修复蓝牙重连黑脸的bug
authorxue <2144439972@qq.com>
Fri, 5 Dec 2025 10:02:37 +0000 (18:02 +0800)
committerxue <2144439972@qq.com>
Fri, 5 Dec 2025 10:02:37 +0000 (18:02 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueView.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp

index 775d264..f2f904a 100644 (file)
@@ -69,6 +69,7 @@ namespace OHOS
         void HandleBluetoothViewOperation(int16_t view);
         void ExecuteDisconnect(void);
         void OnConnectFailed(void);
+        void OnConnectFailedAsync(void);  // 异步执行耗时操作
         void CloseScan(void);
         
         // 按键操作接口(设备列表页面使用)
@@ -89,7 +90,8 @@ namespace OHOS
             THUNDERBIRD_BULEVIEW_DISCONNECT_CONFIRM, // 断连询问
             THUNDERBIRD_BULEVIEW_DISCONNECT_SUCCESS, // 断连成功
             THUNDERBIRD_BULEVIEW_DISCONNECT_SUCCESS_NEW, // 解绑(新)
-            THUNDERBIRD_BULEVIEW_DISCONNECT_FAILED   // 断连失败
+            THUNDERBIRD_BULEVIEW_DISCONNECT_FAILED,   // 断连失败
+            THUNDERBIRD_BULEVIEW_EXIT_OR_RETRY,      // 退出配对或重新搜索
         };
 
     private:
index 751ffd1..f2b8c16 100644 (file)
@@ -452,13 +452,13 @@ namespace OHOS
             if (RemindLabel)
             {
                 RemindLabel->SetPosition(66, 275, 272, 25);
-                RemindLabel->SetText("按     重新扫描    按     确认");
+                RemindLabel->SetText("按     /     选择    按     确认");
                 RemindLabel->SetVisible(true);
             }
             if (RemindLabel_2)
             {
                 RemindLabel_2->SetPosition(109, 310, 150, 25);
-                RemindLabel_2->SetText("长按     退出配对");
+                RemindLabel_2->SetText("长按     退出/重搜");
                 RemindLabel_2->SetVisible(true);
             }
             if (RemindIcon)
@@ -475,11 +475,11 @@ namespace OHOS
             {
                 RemindIcon2->SetPosition(120, 275, 22, 22);
                 RemindIcon2->SetSrc("/user/res/volume_up.bin");
-                RemindIcon2->SetVisible(false);
+                RemindIcon2->SetVisible(true);
             }
             if (RemindIcon3)
             {
-                RemindIcon3->SetPosition(229, 275, 22, 22);
+                RemindIcon3->SetPosition(227, 275, 22, 22);
                 RemindIcon3->SetSrc("/user/res/voice.bin");
                 RemindIcon3->SetVisible(true);
             }
@@ -700,6 +700,47 @@ namespace OHOS
             }
             break;
         }
+        case THUNDERBIRD_BULEVIEW_EXIT_OR_RETRY:
+        {
+            if (TextLabel)
+            {
+                TextLabel->SetPosition(30, 148, 180, 42);
+                TextLabel->SetText("是否退出配对或重新搜索");
+                TextLabel->SetVisible(true);
+            } 
+            if (RemindLabel)
+            {
+                RemindLabel->SetPosition(80, 275, 200, 25);
+                RemindLabel->SetText("按     取消    按     重搜");
+                RemindLabel->SetVisible(true);
+            }
+            if (RemindLabel_2)
+            {
+                RemindLabel_2->SetPosition(109, 310, 150, 25);
+                RemindLabel_2->SetText("长按     退出配对");
+                RemindLabel_2->SetVisible(true);
+            }
+            
+            if (RemindIcon1)
+            {
+                RemindIcon1->SetPosition(102, 276, 22, 22);
+                RemindIcon1->SetSrc("/user/res/volume_down.bin");
+                RemindIcon1->SetVisible(true);
+            }
+            if (RemindIcon2)
+            {
+                RemindIcon2->SetPosition(208, 276, 22, 22);
+                RemindIcon2->SetSrc("/user/res/volume_up.bin");
+                RemindIcon2->SetVisible(true);
+            }
+            if (RemindIcon3)
+            {
+                RemindIcon3->SetPosition(150, 310, 22, 22);
+                RemindIcon3->SetSrc("/user/res/voice.bin");
+                RemindIcon3->SetVisible(true);
+            }
+            break;
+        }
         default:
             break;
         }
@@ -873,21 +914,42 @@ namespace OHOS
             return;
         }
         
-        // 重新发送连接操作
+        // 前台处理:只做状态检查和设置,不执行耗时操作
         if (deviceListAdapter_ == nullptr) {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "deviceListAdapter_ nullptr");
             return;
         }
         
-        // 获取临时MAC值并发起连接
+        // 获取临时MAC值(快速操作)
         OHOS::BluetoothCaseInfo *caseInfo = BluetoothModel::GetInstance()->GetTempMacValue();
         if (caseInfo == nullptr) {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetTempMacValue failed");
             return;
         }
+        
+        // 设置标志位(快速操作)
         BluetoothModel::GetInstance()->SetTagScanDeviceClicked(true);
+        
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "OnConnectFailed: 前台处理完成,耗时操作将在后台异步执行");
+        // 耗时操作将在 OnConnectFailedAsync() 中异步执行
+    }
+    
+    void ThunderBirdBlueView::OnConnectFailedAsync(void)
+    {
+        // 后台耗时操作:断开连接、刷新列表、发起连接
+        if (deviceListAdapter_ == nullptr) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "deviceListAdapter_ nullptr");
+            return;
+        }
+        
+        // 获取临时MAC值
+        OHOS::BluetoothCaseInfo *caseInfo = BluetoothModel::GetInstance()->GetTempMacValue();
+        if (caseInfo == nullptr) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetTempMacValue failed");
+            return;
+        }
 
-        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "OnKeyConfirm: connecting to %s,addr:%02x%02x%02x%02x%02x%02x",
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "OnConnectFailedAsync: connecting to %s,addr:%02x%02x%02x%02x%02x%02x",
             caseInfo->deviceName,
             caseInfo->addr[5], 
             caseInfo->addr[4], 
@@ -896,28 +958,29 @@ namespace OHOS
             caseInfo->addr[1], 
             caseInfo->addr[0]);
         
-        // 判断是否已有连接
+        // 判断是否已有连接(耗时操作)
         bd_addr_t *lastAddr = BluetoothModel::GetInstance()->GetLastConnedAddr();
         if (bt_is_acl_connected(lastAddr))
         {
             WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "LastDevice addr: %02x%02x%02x%02x%02x%02x is connected",
                           lastAddr->addr[5], lastAddr->addr[4], lastAddr->addr[3],
                           lastAddr->addr[2], lastAddr->addr[1], lastAddr->addr[0]);
-            BluetoothModel::GetInstance()->DisconnectRemoteDevice();
+            BluetoothModel::GetInstance()->DisconnectRemoteDevice();  // 耗时操作
             BluetoothModel::GetInstance()->SetTagConnectNew(true);
         }
 
-        RefreshDeviceList();
+        RefreshDeviceList();  // 耗时操作:刷新UI列表
 
-        // 发起连接
+        // 发起连接(耗时操作)
         // 注意:BluetoothCaseInfo::addr 只是一个 6 字节数组,这里需要组装成 bd_addr_t 结构再传给 ConnectRemoteDevice
+        bd_addr_t targetAddr;
         memset(&targetAddr, 0, sizeof(bd_addr_t));
         (void)memcpy_s(targetAddr.addr, BD_ADDR_LEN, caseInfo->addr, BD_ADDR_LEN);
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                      "OnConnectFailed: ConnectRemoteDevice targetAddr=%02x%02x%02x%02x%02x%02x",
+                      "OnConnectFailedAsync: ConnectRemoteDevice targetAddr=%02x%02x%02x%02x%02x%02x",
                       targetAddr.addr[5], targetAddr.addr[4], targetAddr.addr[3],
                       targetAddr.addr[2], targetAddr.addr[1], targetAddr.addr[0]);
-        BluetoothModel::GetInstance()->ConnectRemoteDevice(&targetAddr);
+        BluetoothModel::GetInstance()->ConnectRemoteDevice(&targetAddr);  // 耗时操作
         return;
     }