增加蓝牙状态与事件区分,修改蓝牙上报操作
authorxue <2144439972@qq.com>
Mon, 19 Jan 2026 07:40:42 +0000 (15:40 +0800)
committerxue <2144439972@qq.com>
Mon, 19 Jan 2026 07:40:42 +0000 (15:40 +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/Ln_Bluetooth_apiC.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp
code/sdk/zhihui/leiniao/imsl_ln/ln_api.c
code/sdk/zhihui/leiniao/imsl_ln/ln_api.h
code/sdk/zhihui/leiniao/imsl_ln/ln_bluetooth_api.c

index 0f53a6a..57b68da 100644 (file)
@@ -1228,7 +1228,7 @@ static void A2dpSrcConnectstateChanged(bd_addr_t *bdAddr, int connState)
         }
     }
     //上报蓝牙耳机连接状态
-    GraphicService::GetInstance()-> PostGraphicEvent(std::bind(ln_bt_report_earphone_connection_status));
+    GraphicService::GetInstance()-> PostGraphicEvent([]() { ln_bt_report_earphone_connection_status(true); });  // 显示弹窗
 }
 
 uint8_t A2dpSrcPlayingState=0;
index 090717c..a6fdb13 100644 (file)
@@ -280,7 +280,7 @@ extern "C"
     }
 
     // 上报蓝牙耳机连接状态
-    int ln_bt_report_earphone_connection_status(void)
+    int ln_bt_report_earphone_connection_status(bool show_status)
     {
         // 只有在已连接状态下才能发送状态报告
         if (ln_ws_get_connection_state() != 2)
@@ -292,32 +292,37 @@ extern "C"
         OHOS::BluetoothModel *bluetoothModel = OHOS::BluetoothModel::GetInstance();
         bd_addr_t *lastAddr = bluetoothModel->GetLastConnedAddr();
         
+        const char *bluetoothStatus = nullptr;
+        int cmd = show_status ? LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT : LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT_NO_POPUP;
+        
         // 检查地址是否有效
         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;
+            bluetoothStatus = "DISCONNECTED";
         }
-
         // 检查地址是否为空(全0)
-        if ((lastAddr->addr[0] == 0) && (lastAddr->addr[1] == 0) &&
+        else 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_LOGE(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;
+            bluetoothStatus = "DISCONNECTED";
+        }
+        else
+        {
+            // 检查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);
+            
+            bluetoothStatus = isEarphoneConnected ? "CONNECTED" : "DISCONNECTED";
         }
 
-        // 检查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);
+        // 根据 show_status 选择对应的命令,通过消息队列发送
+        do_ln_http_server_send_data(cmd, (char *)bluetoothStatus, strlen(bluetoothStatus));
 
         // 检查当前是否在蓝牙界面,如果在则设置标志
         OHOS::ThunderBirdMainView *mainView = OHOS::ThunderBirdMainView::GetInstance();
@@ -327,29 +332,10 @@ extern "C"
             isInBlueView = (currentView == 7);
         }
 
-        if (isEarphoneConnected)
-        {
-            // 蓝牙已连接,上报蓝牙状态
-            char *bluetoothStatus = "CONNECTED";
-            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
-            
-            // 如果当前在蓝牙界面,设置标志表示已上报
-            if (isInBlueView && mainView != nullptr) {
-                mainView->m_btStatusReportedInBlueView_ = true;
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: 在蓝牙界面内上报状态,设置标志");
-            }
-        }
-        else
-        {
-            // 蓝牙已断开,上报蓝牙状态
-            char *bluetoothStatus = "DISCONNECTED";
-            do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus));
-            
-            // 如果当前在蓝牙界面,设置标志表示已上报
-            if (isInBlueView && mainView != nullptr) {
-                mainView->m_btStatusReportedInBlueView_ = true;
-                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: 在蓝牙界面内上报状态,设置标志");
-            }
+        // 如果当前在蓝牙界面,设置标志表示已上报
+        if (isInBlueView && mainView != nullptr) {
+            mainView->m_btStatusReportedInBlueView_ = true;
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ln_bt_report_earphone_connection_status: 在蓝牙界面内上报状态,设置标志");
         }
 
         return 0;
index 7955b0d..991994a 100644 (file)
@@ -38,7 +38,7 @@ 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);
+int ln_bt_report_earphone_connection_status(bool show_status);
 
 // 增加耳机音量(按键方式,音量调节值由耳机固件决定,通常为固定步进值)
 int ln_bt_earphone_volume_up(void);
index 68f1950..00bc895 100644 (file)
@@ -491,7 +491,7 @@ namespace OHOS
         if (model != nullptr && model->m_wasInDownloadView)
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView::OnStart: 从下载界面返回,上报蓝牙连接状态");
-            ln_bt_report_earphone_connection_status();
+            ln_bt_report_earphone_connection_status(false);  // 不需要弹窗
             model->m_wasInDownloadView = false;  // 清除标志位
         }
         View<ThunderBirdPresenter>::OnStart();
@@ -1178,7 +1178,7 @@ namespace OHOS
                 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ThunderBirdMainView::ChangeView: 蓝牙界面已上报过状态,退出时不再上报");
                 m_btStatusReportedInBlueView_ = false; // 清除标志
             } else {
-                ln_bt_report_earphone_connection_status();
+                ln_bt_report_earphone_connection_status(true);  // 显示弹窗
             }
         }
         if(showView == THUNDERBIRD_BLUEVIEW )
index d9751c8..2183140 100644 (file)
@@ -1359,7 +1359,7 @@ static void ln_http_server_send_task(void const *arg)
         }
         else if(mail.cmd == LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT] 蓝牙状态上报");
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT] 蓝牙状态上报(需要弹窗)");
             if (mail.data && mail.len > 0)
             {
                 /* 确保字符串以\0结尾,避免乱码 */
@@ -1368,11 +1368,28 @@ static void ln_http_server_send_task(void const *arg)
                 {
                     memcpy(bluetooth_status, mail.data, mail.len);
                     bluetooth_status[mail.len] = '\0';
-                    ln_send_bluetooth_status_report(bluetooth_status);
+                    ln_send_bluetooth_status_report(bluetooth_status, true);  /* 需要弹窗 */
                     free(bluetooth_status);
                 }
             }
         }
+        else if(mail.cmd == LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT_NO_POPUP)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT_NO_POPUP] 蓝牙状态上报(不需要弹窗)");
+            if (mail.data && mail.len > 0)
+            {
+                /* 确保字符串以\0结尾,避免乱码 */
+                char *bluetooth_status = (char *)malloc(mail.len + 1);
+                if (bluetooth_status)
+                {
+                    memcpy(bluetooth_status, mail.data, mail.len);
+                    bluetooth_status[mail.len] = '\0';
+                    ln_send_bluetooth_status_report(bluetooth_status, false);  /* 不需要弹窗 */
+                    free(bluetooth_status);
+                }
+            }
+        }
+
         else if (mail.cmd == LN_HTTP_SERVICE_CMD_CHAT_SESSION_START)
         {
             printf("LN_HTTP_SERVICE_CMD_CHAT_SESSION_START:语音对话流开始\n");
index b4eb46e..67fdc5b 100644 (file)
@@ -139,7 +139,8 @@ typedef struct
 #define LN_HTTP_SERVICE_CMD_WS_EXIT_SLEEP 0x1014 /* 退出休眠 */
 
 //蓝牙状态上报相关命令
-#define LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT 0x1015 /* 蓝牙状态上报 */
+#define LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT 0x1015 /* 蓝牙状态上报(需要弹窗) */
+#define LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT_NO_POPUP 0x101C /* 蓝牙状态上报(不需要弹窗) */
 
 /* 语音对话流相关命令 */
 #define LN_HTTP_SERVICE_CMD_CHAT_SESSION_START 0x1016 /* 语音对话流开始 */
@@ -373,7 +374,7 @@ void ln_parse_bluetooth_connect(const char *json_str, int json_len);
 /* 解析蓝牙解绑命令 */
 void ln_parse_bluetooth_unbind(const char *json_str, int json_len);
 /* 发送蓝牙状态上报(使用消息队列方式) */
-void ln_send_bluetooth_status_report(const char *bluetooth_status);
+void ln_send_bluetooth_status_report(const char *bluetooth_status, bool show_status);
 /* 设置睡眠模式状态标志位 */
 void uapi_set_sleep_mode_active(bool active);
 /* 获取睡眠模式状态标志位 */
index ed637ae..32de86a 100644 (file)
@@ -56,7 +56,7 @@ int ln_get_bluetooth_device_battery_level(void)
 
 /* 发送蓝牙状态上报 BluetoothStatusReport(内部实现,通过WebSocket发送) */
 static int ln_ws_send_bluetooth_status_report(const char *bluetooth_address, const char *bluetooth_name,
-                                              int battery_level, const char *bluetooth_status, const char *msg)
+                                              int battery_level, const char *bluetooth_status, const char *msg, bool show_status)
 {
     if (ln_ws_get_connection_state() != 2)
     {
@@ -101,6 +101,7 @@ static int ln_ws_send_bluetooth_status_report(const char *bluetooth_address, con
     cJSON_AddNumberToObject(data, "batteryLevel", battery);
 
     cJSON_AddStringToObject(data, "bluetoothStatus", (bluetooth_status && bluetooth_status[0]) ? bluetooth_status : "");
+    cJSON_AddBoolToObject(data, "showStatus", show_status);
     cJSON_AddStringToObject(data, "message", msg ? msg : "");
     cJSON_AddItemToObject(json, "data", data);
 
@@ -119,7 +120,7 @@ static int ln_ws_send_bluetooth_status_report(const char *bluetooth_address, con
 }
 
 /* 发送蓝牙状态上报(使用消息队列方式) */
-void ln_send_bluetooth_status_report(const char *bluetooth_status)
+void ln_send_bluetooth_status_report(const char *bluetooth_status, bool show_status)
 {
     if (!bluetooth_status || !bluetooth_status[0])
     {
@@ -129,7 +130,7 @@ void ln_send_bluetooth_status_report(const char *bluetooth_status)
     char *msg = NULL;
     if(strcmp(bluetooth_status, BT_STATUS_CONNECTED) == 0)
     {
-        msg = "蓝牙已连接";
+        msg = "蓝牙连接成功";
     }
     else if(strcmp(bluetooth_status, BT_STATUS_CONNECTING) == 0)
     {
@@ -164,7 +165,8 @@ void ln_send_bluetooth_status_report(const char *bluetooth_status)
     ln_get_bluetooth_device_name(), 
     ln_get_bluetooth_device_battery_level(),
     bluetooth_status, 
-    msg);
+    msg,
+    show_status);
 }
 
 /* 解析蓝牙连接命令 BluetoothConnect */