From 9a241240ee09395be16033fc7e4d3d920c6faf4e Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Sat, 29 Nov 2025 18:42:51 +0800 Subject: [PATCH] =?utf8?q?ln=EF=BC=9A1.=E5=A6=82=E6=9E=9C=E6=9C=89?= =?utf8?q?=E5=B7=B2=E7=BB=91=E5=AE=9A=E7=9A=84=E8=AE=BE=E5=A4=87=E9=9C=80?= =?utf8?q?=E8=A6=81=E5=88=87=E6=8D=A2=E5=88=B0=E8=AE=BE=E5=A4=87=E5=88=97?= =?utf8?q?=E8=A1=A8=E9=A1=B5=E9=9D=A2=202.=E4=BC=98=E5=8C=96=E8=A7=A3?= =?utf8?q?=E7=BB=91=E5=90=8E=E8=AE=BE=E5=A4=87=E6=B2=A1=E6=9C=89=E4=BB=8E"?= =?utf8?q?=E6=88=91=E7=9A=84=E8=AE=BE=E5=A4=87"=E5=88=97=E8=A1=A8=E4=B8=AD?= =?utf8?q?=E6=B8=85=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/bluetooth/src/BluetoothModel.cpp | 36 ++++++++++- .../bluetooth/src/Ln_Bluetooth_apiC.cpp | 60 +++++++++++++++++++ .../include/bluetooth/Ln_Bluetooth_apiC.h | 3 + .../thunderbird/src/ThunderBirdModel.cpp | 15 +++++ 4 files changed, 111 insertions(+), 3 deletions(-) diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp index 6ca3161e8..0e2f571b1 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp @@ -44,6 +44,7 @@ #include "ln_api.h" #include "phoneservice/PhoneService.h" #include "thunderbird/include/ThunderBirdMainView.h" +#include "thunderbird/include/ThunderBirdBlueView.h" #include namespace OHOS { @@ -143,7 +144,7 @@ void BluetoothModel::Init() BtInitCallBacksEx(); BtInitGatts(); - // BtInitSpp(); + BtInitSpp(); InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化 // PhoneServiceInit(); // gap_remove_all_pairs(); @@ -628,16 +629,45 @@ int BluetoothModel::UnBindRemoteDevice(const char *mac) } bool ret = gap_remove_pair(&addr); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_remove_pair %d", ret); - if(ret == false) + if(ret == true) { + // 从已连接设备列表中移除该设备 + if (btConnedDevicesList_ != nullptr) { + ListNode *node = btConnedDevicesList_->Begin(); + while (node != nullptr && node != btConnedDevicesList_->End()) { + if ((node->data_.addr[0] == addr.addr[0]) && (node->data_.addr[1] == addr.addr[1]) && + (node->data_.addr[2] == addr.addr[2]) && (node->data_.addr[3] == addr.addr[3]) && + (node->data_.addr[4] == addr.addr[4]) && (node->data_.addr[5] == addr.addr[5])) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice: 从列表中移除设备 %02x%02x%02x%02x%02x%02x", + addr.addr[5], addr.addr[4], addr.addr[3], addr.addr[2], addr.addr[1], addr.addr[0]); + ListNode *nextNode = node->next_; + btConnedDevicesList_->Remove(node); + node = nextNode; + } else { + node = node->next_; + } + } + } + + // 刷新设备列表视图 + ThunderBirdBlueView *thunderBirdBlueView = ThunderBirdBlueView::GetInstance(); + if (thunderBirdBlueView != nullptr) { + thunderBirdBlueView->RefreshDeviceList(); + } + // 只有在已连接状态下才能发送状态报告 if (ln_ws_get_connection_state() == 2) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0"); - char *bluetoothStatus = "UNBIND_FAILED"; + char *bluetoothStatus = "UNBIND_SUCCESS"; do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); } } + else + { + char *bluetoothStatus = "UNBIND_FAILED"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } return ret; } diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp index 74694c81c..d82bce560 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/Ln_Bluetooth_apiC.cpp @@ -6,6 +6,10 @@ #include "graphic_timer.h" #include "wearable_log.h" #include +#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 diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h index c00385b78..0e2a44541 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/Ln_Bluetooth_apiC.h @@ -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 diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp index 55e604fc4..ac4f668c8 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdModel.cpp @@ -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 #include @@ -2647,6 +2648,20 @@ extern "C" { //亮屏 uapi_set_screen_on_api(); int currentView = ThunderBirdMainView::GetInstance()->GetCurrentView(); //获取当前视图 + + // 检查是否有已绑定的设备 + BluetoothModel* btModel = BluetoothModel::GetInstance(); + if (btModel != nullptr) { + List& 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); } } -- 2.17.1