From 2262ae3694415a6c4969715bc7847d5aa185b7ae Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Fri, 28 Nov 2025 15:03:32 +0800 Subject: [PATCH] =?utf8?q?ln=EF=BC=9A=E8=A7=A3=E5=86=B3=E8=93=9D=E7=89=99?= =?utf8?q?=E6=89=AB=E6=8F=8F=E4=B8=8D=E5=88=B0=E8=AE=BE=E5=A4=87=E5=88=B0?= =?utf8?q?=E6=97=B6=E9=97=B4=E5=90=8E=E6=9C=AA=E5=88=87=E6=8D=A2=E5=88=B0?= =?utf8?q?=E9=94=99=E8=AF=AF=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/bluetooth/src/BluetoothModel.cpp | 60 +++++++++++-------- .../bluetooth/src/Ln_Bluetooth_apiC.cpp | 3 +- .../include/bluetooth/BluetoothModel.h | 1 + .../thunderbird/src/ThunderBirdBlueView.cpp | 53 +++++++++++++++- .../src/AiVoiceBluetoothFailedStartView.cpp | 1 - 5 files changed, 89 insertions(+), 29 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 ea1a8029a..eb21b342c 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 @@ -145,9 +145,11 @@ void BluetoothModel::Init() #endif BtInitCallBacksEx(); BtInitGatts(); - BtInitSpp(); + // BtInitSpp(); InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化 // PhoneServiceInit(); + gap_remove_all_pairs(); + BtA2dpInit(); /* 实验室测试需要,开机开广播 */ BleAdvStart(); @@ -617,15 +619,13 @@ int BluetoothModel::UnBindRemoteDevice(const char *mac) WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gap_remove_pair %d", ret); if(ret == false) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0"); - char *bluetoothStatus = "UNBIND_FAILED"; - do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 1"); - char *bluetoothStatus = "UNBIND_SUCCESS"; - do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + // 只有在已连接状态下才能发送状态报告 + if (ln_ws_get_connection_state() == 2) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UnBindRemoteDevice gap_remove_pair ret = 0"); + char *bluetoothStatus = "UNBIND_FAILED"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } } return ret; } @@ -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 */ if(status == GAP_PAIR_NONE){ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "取消配对"); - // char *bluetoothStatus = "UNBIND_SUCCESS"; - // do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + if (ln_ws_get_connection_state() == 2) + { + char *bluetoothStatus = "UNBIND_SUCCESS"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } } else if(status == GAP_PAIR_PAIRING){ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "正在配对"); } else if(status == GAP_PAIR_PAIRED){ @@ -874,10 +877,13 @@ static void ACLConnectionSuss(const bd_addr_t &bdAddr) //亮屏操作 uapi_set_screen_on_api(); - //蓝牙已连接,上报蓝牙状态 - char *bluetoothStatus = "CONNECTED"; - do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); - + if (ln_ws_get_connection_state() == 2) + { + //蓝牙已连接,上报蓝牙状态 + char *bluetoothStatus = "CONNECTED"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } + ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance(); if (thunderBirdMainView == nullptr) { @@ -904,15 +910,19 @@ static void ACLConnectionFail(const bd_addr_t &bdAddr) if(strcmp((const char *)caseInfo.deviceName, "") == 0){ return; } - if(strcmp((const char *)caseInfo.deviceName, BluetoothModel::GetInstance()->GetLastConnedDeviceName()) == 0){ + if(strcmp((const char *)caseInfo.deviceName, BluetoothModel::GetInstance()->GetLastConnedDeviceName()) == 0) + { //亮屏操作 uapi_set_screen_on_api(); - //蓝牙已断开,上报蓝牙状态 - char *bluetoothStatus = "DISCONNECTED"; - do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); - + if (ln_ws_get_connection_state() == 2) + { + //蓝牙已断开,上报蓝牙状态 + char *bluetoothStatus = "DISCONNECTED"; + do_ln_http_server_send_data(LN_HTTP_SERVICE_CMD_BLUETOOTH_STATUS_REPORT, bluetoothStatus, strlen(bluetoothStatus)); + } + ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance(); if (thunderBirdMainView == nullptr) { @@ -951,8 +961,6 @@ void BluetoothModel::BtGapAclStateChangedHandle(gap_acl_state_t state, unsigned if ((!tag) && (memcmp(info->addr, bdAddr.addr, BD_ADDR_LEN) == 0)) { AddConnedDevicesCase(*info); } - } else { - ACLConnectionFail(bdAddr); } if ((state == GAP_ACL_STATE_CONNECTED) || (state == GAP_ACL_STATE_LE_CONNECTED)) { @@ -1467,11 +1475,15 @@ void BtInitCallBacks() g_btGapCallBacks.remote_battery_level_changed_callback = BtGapRemoteBatteryLevelChangedCallback; gap_register_callbacks(&g_btGapCallBacks); +} + +void BtA2dpInit(void) +{ + /* a2dp source */ g_a2dpSrcCallbacks.connectstate_changed_cb = A2dpSrcConnectstateChanged; g_a2dpSrcCallbacks.configuration_changed_cb = A2dpSrcConfigureChanged; g_a2dpSrcCallbacks.playing_state_changed_cb = A2dpSrcPlayingStateChanged; a2dp_src_register_callbacks(&g_a2dpSrcCallbacks); - /* a2dp sink */ a2dp_snk_register_callbacks(&g_a2dp_snk_callbacks); } 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 9211103cf..29bbb9604 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 @@ -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) { diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h index 70f3c9e74..b92fbc358 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/include/bluetooth/BluetoothModel.h @@ -147,6 +147,7 @@ void BtInitCallBacksEx(void); void BtInitGatts(void); void BtInitSpp(void); void BleAdvStart(void); +void BtA2dpInit(void); #ifdef __cplusplus } #endif diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp index 9b1f754b4..bf6263871 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp @@ -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); + } + } } // 获取当前选中的设备索引 diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceBluetoothFailedStartView.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceBluetoothFailedStartView.cpp index 0b188355b..b9705bbcc 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceBluetoothFailedStartView.cpp +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/aivoice/src/AiVoiceBluetoothFailedStartView.cpp @@ -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); -- 2.17.1