ln:1.如果有已绑定的设备需要切换到设备列表页面 2828/1
authorluozheng <1732036298@qq.com>
Sat, 29 Nov 2025 10:42:51 +0000 (18:42 +0800)
committerluozheng <1732036298@qq.com>
Sat, 29 Nov 2025 10:42:51 +0000 (18:42 +0800)
2.优化解绑后设备没有从"我的设备"列表中清除

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/Ln_Bluetooth_apiC.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp

index 6ca3161..0e2f571 100644 (file)
@@ -44,6 +44,7 @@
 #include "ln_api.h"\r
 #include "phoneservice/PhoneService.h"\r
 #include "thunderbird/include/ThunderBirdMainView.h"\r
+#include "thunderbird/include/ThunderBirdBlueView.h"\r
 #include <custom_utils.h>\r
 namespace OHOS {\r
 \r
@@ -143,7 +144,7 @@ void BluetoothModel::Init()
 \r
     BtInitCallBacksEx();\r
     BtInitGatts();\r
-    // BtInitSpp();\r
+    BtInitSpp();\r
     InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化\r
     // PhoneServiceInit();\r
     // gap_remove_all_pairs();\r
@@ -628,16 +629,45 @@ int BluetoothModel::UnBindRemoteDevice(const char *mac)
     }\r
     bool ret = gap_remove_pair(&addr);\r
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_remove_pair %d", ret);\r
-    if(ret == false)\r
+    if(ret == true)\r
     {\r
+        // 从已连接设备列表中移除该设备\r
+        if (btConnedDevicesList_ != nullptr) {\r
+            ListNode<BluetoothCaseInfo> *node = btConnedDevicesList_->Begin();\r
+            while (node != nullptr && node != btConnedDevicesList_->End()) {\r
+                if ((node->data_.addr[0] == addr.addr[0]) && (node->data_.addr[1] == addr.addr[1]) &&\r
+                    (node->data_.addr[2] == addr.addr[2]) && (node->data_.addr[3] == addr.addr[3]) &&\r
+                    (node->data_.addr[4] == addr.addr[4]) && (node->data_.addr[5] == addr.addr[5])) {\r
+                    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice: 从列表中移除设备 %02x%02x%02x%02x%02x%02x",\r
+                        addr.addr[5], addr.addr[4], addr.addr[3], addr.addr[2], addr.addr[1], addr.addr[0]);\r
+                    ListNode<BluetoothCaseInfo> *nextNode = node->next_;\r
+                    btConnedDevicesList_->Remove(node);\r
+                    node = nextNode;\r
+                } else {\r
+                    node = node->next_;\r
+                }\r
+            }\r
+        }\r
+        \r
+        // 刷新设备列表视图\r
+        ThunderBirdBlueView *thunderBirdBlueView = ThunderBirdBlueView::GetInstance();\r
+        if (thunderBirdBlueView != nullptr) {\r
+            thunderBirdBlueView->RefreshDeviceList();\r
+        }\r
+        \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
+            char *bluetoothStatus = "UNBIND_SUCCESS";\r
             do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
         }\r
     }\r
+    else\r
+    {\r
+        char *bluetoothStatus = "UNBIND_FAILED";\r
+        do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));\r
+    }\r
     return ret;\r
 }\r
 \r
index 74694c8..d82bce5 100644 (file)
@@ -6,6 +6,10 @@
 #include "graphic_timer.h"
 #include "wearable_log.h"
 #include <custom_utils.h>
+#include "bts_def.h"
+#include "bts_a2dp_source.h"
+#include "bts_br_gap.h"
+#include "ln_api.h"
 
 namespace OHOS
 {
@@ -252,6 +256,62 @@ extern "C"
         return OHOS::BluetoothModel::GetInstance()->GetLastConnedDeviceBatteryLevel();
     }
 
+    // 上报蓝牙耳机连接状态
+    int ln_bt_report_earphone_connection_status(void)
+    {
+        // 只有在已连接状态下才能发送状态报告
+        if (ln_ws_get_connection_state() != 2)
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: ws not connected");
+            return 0;
+        }
+
+        OHOS::BluetoothModel *bluetoothModel = OHOS::BluetoothModel::GetInstance();
+        bd_addr_t *lastAddr = bluetoothModel->GetLastConnedAddr();
+        
+        // 检查地址是否有效
+        if (lastAddr == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: lastAddr is nullptr");
+            char *bluetoothStatus = "DISCONNECTED";
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
+            return 0;
+        }
+
+        // 检查地址是否为空(全0)
+        if ((lastAddr->addr[0] == 0) && (lastAddr->addr[1] == 0) &&
+            (lastAddr->addr[2] == 0) && (lastAddr->addr[3] == 0) &&
+            (lastAddr->addr[4] == 0) && (lastAddr->addr[5] == 0))
+        {
+            WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: lastAddr is empty");
+            char *bluetoothStatus = "DISCONNECTED";
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
+            return 0;
+        }
+
+        // 检查A2DP连接状态(耳机真实连接状态)
+        int a2dpState = a2dp_src_get_device_connect_state(lastAddr);
+        bool isEarphoneConnected = (a2dpState == PROFILE_STATE_CONNECTED);
+        
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: a2dpState=%d, isEarphoneConnected=%d", 
+                      a2dpState, isEarphoneConnected);
+
+        if (isEarphoneConnected)
+        {
+            // 蓝牙已连接,上报蓝牙状态
+            char *bluetoothStatus = "CONNECTED";
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
+        }
+        else
+        {
+            // 蓝牙已断开,上报蓝牙状态
+            char *bluetoothStatus = "DISCONNECTED";
+            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
+        }
+
+        return 0;
+    }
+
 #ifdef __cplusplus
 }
 #endif
\ No newline at end of file
index c00385b..0e2a445 100644 (file)
@@ -33,6 +33,9 @@ char *ln_bt_get_last_conned_device_name(void);
 
 // 获取最后连接的蓝牙设备电池电量
 int ln_bt_get_last_conned_device_battery_level(void);
+
+// 上报蓝牙耳机连接状态
+int ln_bt_report_earphone_connection_status(void);
 #ifdef __cplusplus
 }
 #endif
index 55e604f..ac4f668 100644 (file)
@@ -21,6 +21,7 @@
  #include "thunderbird/include/ThunderBirdMainView.h"
  #include "thunderbird/include/ThunderBirdBlueView.h"
  #include "thunderbird/include/ThunderBirdPresenter.h"
+ #include "bluetooth/BluetoothModel.h"
  #include "wearable_log.h"
  #include <sys/time.h>
  #include <service/graphic_service.h>
@@ -2647,6 +2648,20 @@ extern "C" {
             //亮屏
             uapi_set_screen_on_api();
             int currentView = ThunderBirdMainView::GetInstance()->GetCurrentView(); //获取当前视图
+            
+            // 检查是否有已绑定的设备
+            BluetoothModel* btModel = BluetoothModel::GetInstance();
+            if (btModel != nullptr) {
+                List<BluetoothCaseInfo>& connedDevices = btModel->GetConnedDevicesCase();
+                if (!connedDevices.IsEmpty()) {
+                    // 如果有已绑定的设备,直接跳转到设备列表页面
+                    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "HandleBluetoothConnect: 已有绑定设备,跳转到设备列表页面");
+                    ThunderBirdMainView::GetInstance()->ChangeView(currentView, THUNDERBIRD_BLUEVIEW, ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_DEVICE_LIST);
+                    return;
+                }
+            }
+            
+            // 没有已绑定设备,跳转到配对准备页面
             ThunderBirdMainView::GetInstance()->ChangeView(currentView, THUNDERBIRD_BLUEVIEW, ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_ENTER);
         }
     }