ln:解决蓝牙扫描不到设备到时间后未切换到错误界面 2817/1 2817/head 2818/target/1
authorluozheng <1732036298@qq.com>
Fri, 28 Nov 2025 07:03:32 +0000 (15:03 +0800)
committerluozheng <1732036298@qq.com>
Fri, 28 Nov 2025 07:03:32 +0000 (15:03 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp
code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceBluetoothFailedStartView.cpp

index ea1a802..eb21b34 100644 (file)
@@ -145,9 +145,11 @@ void BluetoothModel::Init()
 #endif\r
     BtInitCallBacksEx();\r
     BtInitGatts();\r
-    BtInitSpp();\r
+    // BtInitSpp();\r
     InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化\r
     // PhoneServiceInit();\r
+    gap_remove_all_pairs();\r
+    BtA2dpInit();\r
 \r
     /* 实验室测试需要,开机开广播 */\r
     BleAdvStart();\r
@@ -617,15 +619,13 @@ int BluetoothModel::UnBindRemoteDevice(const char *mac)
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_remove_pair %d", ret);\r
     if(ret == false)\r
     {\r
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0");\r
-        char *bluetoothStatus = "UNBIND_FAILED";\r
-        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
-    }\r
-    else\r
-    {\r
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 1");\r
-        char *bluetoothStatus = "UNBIND_SUCCESS";\r
-        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        // 只有在已连接状态下才能发送状态报告\r
+        if (ln_ws_get_connection_state() == 2)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0");\r
+            char *bluetoothStatus = "UNBIND_FAILED";\r
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        }\r
     }\r
     return ret;\r
 }\r
@@ -775,8 +775,11 @@ void BtGapPairStatusChangedCallback(const bd_addr_t *bdAddr, int status)
         bdAddr->addr[3], bdAddr->addr[2], bdAddr->addr[1], bdAddr->addr[0], status); /* addr下标 0 1 2 3 */\r
     if(status == GAP_PAIR_NONE){\r
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "取消配对");\r
-        // char *bluetoothStatus = "UNBIND_SUCCESS";\r
-        // do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        if (ln_ws_get_connection_state() == 2)\r
+        {\r
+            char *bluetoothStatus = "UNBIND_SUCCESS";\r
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        }\r
     } else if(status == GAP_PAIR_PAIRING){\r
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "正在配对");\r
     } else if(status == GAP_PAIR_PAIRED){\r
@@ -874,10 +877,13 @@ static void ACLConnectionSuss(const bd_addr_t &bdAddr)
         //亮屏操作\r
         uapi_set_screen_on_api();\r
         \r
-        //蓝牙已连接,上报蓝牙状态\r
-        char *bluetoothStatus = "CONNECTED";\r
-        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
-    \r
+        if (ln_ws_get_connection_state() == 2)\r
+        {\r
+            //蓝牙已连接,上报蓝牙状态\r
+            char *bluetoothStatus = "CONNECTED";\r
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        }\r
+\r
         ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance();\r
         if (thunderBirdMainView == nullptr)\r
         {\r
@@ -904,15 +910,19 @@ static void ACLConnectionFail(const bd_addr_t &bdAddr)
     if(strcmp((const char *)caseInfo.deviceName, "") == 0){\r
         return;\r
     }\r
-    if(strcmp((const char *)caseInfo.deviceName, BluetoothModel::GetInstance()->GetLastConnedDeviceName()) == 0){\r
+    if(strcmp((const char *)caseInfo.deviceName, BluetoothModel::GetInstance()->GetLastConnedDeviceName()) == 0)\r
+    {\r
         \r
         //亮屏操作\r
         uapi_set_screen_on_api();\r
 \r
-        //蓝牙已断开,上报蓝牙状态\r
-        char *bluetoothStatus = "DISCONNECTED";\r
-        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
-\r
+        if (ln_ws_get_connection_state() == 2)\r
+        {\r
+            //蓝牙已断开,上报蓝牙状态\r
+            char *bluetoothStatus = "DISCONNECTED";\r
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+        }\r
+        \r
         ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance();\r
         if (thunderBirdMainView == nullptr)\r
         {\r
@@ -951,8 +961,6 @@ void BluetoothModel::BtGapAclStateChangedHandle(gap_acl_state_t state, unsigned
         if ((!tag) && (memcmp(info->addr, bdAddr.addr, BD_ADDR_LEN) == 0)) {\r
             AddConnedDevicesCase(*info);\r
         }\r
-    } else {\r
-        ACLConnectionFail(bdAddr);\r
     }\r
 \r
     if ((state == GAP_ACL_STATE_CONNECTED) || (state == GAP_ACL_STATE_LE_CONNECTED)) {\r
@@ -1467,11 +1475,15 @@ void BtInitCallBacks()
     g_btGapCallBacks.remote_battery_level_changed_callback = BtGapRemoteBatteryLevelChangedCallback;\r
     gap_register_callbacks(&g_btGapCallBacks);\r
 \r
+}\r
+\r
+void BtA2dpInit(void)\r
+{\r
+    /* a2dp source */\r
     g_a2dpSrcCallbacks.connectstate_changed_cb = A2dpSrcConnectstateChanged;\r
     g_a2dpSrcCallbacks.configuration_changed_cb = A2dpSrcConfigureChanged;\r
     g_a2dpSrcCallbacks.playing_state_changed_cb = A2dpSrcPlayingStateChanged;\r
     a2dp_src_register_callbacks(&g_a2dpSrcCallbacks);\r
-\r
     /* a2dp sink */\r
     a2dp_snk_register_callbacks(&g_a2dp_snk_callbacks);\r
 }\r
index 9211103..29bbb96 100644 (file)
@@ -25,7 +25,6 @@ namespace OHOS
             g_stopScanHandle_ln->Stop();
             delete g_stopScanHandle_ln;
             g_stopScanHandle_ln = nullptr;
-            BluetoothModel::GetInstance()->SetScanStatus(false);
             bool results = gap_br_cancel_discovery();
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "取消扫描 results %d", results);
         }
@@ -39,7 +38,7 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "扫描结果为空");
             if (BluetoothModel::GetInstance()->GetScanStatus() == true) // 如果是扫描状态
             {
-                
+                BluetoothModel::GetInstance()->SetScanStatus(false);
                 ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance();
                 if (thunderBirdMainView == nullptr)
                 {
index 9b1f754..bf62638 100644 (file)
@@ -713,11 +713,21 @@ namespace OHOS
         BluetoothModel::GetInstance()->SetTempMacValue(selectedDevice);
         BluetoothModel::GetInstance()->SetTagScanDeviceClicked(true);
         
+        // 判断是否已有连接
+        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()->SetTagConnectNew(true);
+        }
         // 发起连接
         int ret = BluetoothModel::GetInstance()->ConnectRemoteDevice(
             (const bd_addr_t*)&selectedDevice->addr);
         
-        if (ret != 0) {
+        if (ret != true) {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ConnectRemoteDevice failed: %d", ret);
             // 可以在这里显示连接失败提示
             if(ThunderBirdMainView::GetInstance()) {
@@ -737,7 +747,46 @@ namespace OHOS
         }
         
         // 重新发送连接操作
-        OnKeyConfirm();
+        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", 
+                      caseInfo->deviceName);
+        
+        // 判断是否已有连接
+        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()->SetTagConnectNew(true);
+        }
+
+        // 发起连接
+        int ret = BluetoothModel::GetInstance()->ConnectRemoteDevice(
+            (const bd_addr_t*)&caseInfo->addr);
+        
+        if (ret != true) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ConnectRemoteDevice failed: %d", ret);
+            // 可以在这里显示连接失败提示
+            if(ThunderBirdMainView::GetInstance()) {
+                int currentView = ThunderBirdMainView::GetInstance()->GetCurrentView();
+                ThunderBirdMainView::GetInstance()->ChangeView(currentView, 
+                    THUNDERBIRD_BLUEVIEW, 
+                    ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_CONNECT_FAILED);
+            }
+        }
     }
     
     // 获取当前选中的设备索引
index 0b18835..b9705bb 100644 (file)
@@ -100,7 +100,6 @@ namespace OHOS
         }
         else
             label->SetTextId(labelconfig.textId);
-        label->SetTextId(labelconfig.textId);
         label->SetLineBreakMode(labelconfig.lineBreakMode);
         label->SetRollSpeed(23);
         label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);