From 7b43849332f5c72c2dec76675f93fb9a201fe77c Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Fri, 28 Nov 2025 10:15:16 +0800 Subject: [PATCH] =?utf8?q?ln:=E4=BC=98=E5=8C=96=E6=89=AB=E6=8F=8F=E5=88=97?= =?utf8?q?=E8=A1=A8=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/bluetooth/src/BluetoothModel.cpp | 16 ++--- .../bluetooth/src/Ln_Bluetooth_apiC.cpp | 66 +++++++++++++++---- .../thunderbird/include/ThunderBirdBlueView.h | 2 + .../src/ThunderBirdBlueListAdapter.cpp | 42 +++++++----- .../thunderbird/src/ThunderBirdBlueView.cpp | 17 +++++ .../thunderbird/src/ThunderBirdMainView.cpp | 1 + .../thunderbird/src/ThunderBirdPresenter.cpp | 4 ++ 7 files changed, 109 insertions(+), 39 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 b9c84f74f..ea1a8029a 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 @@ -752,21 +752,13 @@ void BluetoothModel::BtGapDiscoveryResultCallbakHandle(const bd_addr_t &bdAddr) bool ret = gap_read_remote_rssi_value(&bdAddr); // 获取rssi WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "gap_read_remote_rssi_value ret = %d", ret); - ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance(); - if (thunderBirdMainView == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "thunderBirdMainView nullptr"); - return; - } - int currentView = thunderBirdMainView->GetCurrentView(); tag = GetScanStatus(); if (tag) { - // 跳转 - SetScanStatus(false); - thunderBirdMainView->ChangeView( - currentView, - THUNDERBIRD_BLUEVIEW, - ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_DEVICE_LIST); + // 扫描状态下不立即跳转,等待定时器触发(至少10秒后) + // 只记录设备,由定时器控制跳转时机 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "扫描中,已发现 %d 个设备", btScansDevicesList_.size()); } else { + // 已在设备列表界面,刷新列表 ThunderBirdBlueView *thunderBirdBlueView = ThunderBirdBlueView::GetInstance(); if (thunderBirdBlueView == nullptr) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "thunderBirdBlueView nullptr"); 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 abf0b40db..9211103cf 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 @@ -11,7 +11,11 @@ namespace OHOS { GraphicTimer *g_stopScanHandle_ln = nullptr; - static constexpr uint64_t MS_SCAN_TIME = 25 * 1000; + static constexpr uint64_t MS_SCAN_TIME = 25 * 1000; // 最大扫描时间25秒 + static constexpr uint64_t MS_SCAN_INTERVAL = 1 * 1000; // 每秒检查一次 + static constexpr uint32_t MIN_SCAN_SECONDS = 10; // 最少扫描10秒 + static uint32_t g_scanElapsedSeconds = 0; // 已扫描秒数 + static bool g_hasJumpedToList = false; // 是否已跳转到列表 // 取消蓝牙扫描 void LN_CancelBrScanHandle(void) @@ -21,6 +25,7 @@ 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); } @@ -34,7 +39,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) { @@ -53,14 +58,45 @@ namespace OHOS return; } - // 定时器回调函数 + // 定时器回调函数(每秒触发) static void LnBluetoothScanTimerCallbackProc(void *data) { (void)data; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "扫描结束"); - LN_CancelBrScanHandle(); - LN_EndScanHandle(); + g_scanElapsedSeconds++; + + size_t deviceCount = BluetoothModel::GetInstance()->GetCase().size(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "扫描中... 已扫描 %d 秒, 发现设备 %d 个", + g_scanElapsedSeconds, deviceCount); + + // 检查是否满足跳转条件:已扫描至少10秒 且 有设备 且 还未跳转 + if (g_scanElapsedSeconds >= MIN_SCAN_SECONDS && deviceCount > 0 && !g_hasJumpedToList) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "满足跳转条件,跳转到设备列表"); + g_hasJumpedToList = true; + + // 跳转到设备列表界面 + BluetoothModel::GetInstance()->SetScanStatus(false); + ThunderBirdMainView *thunderBirdMainView = ThunderBirdMainView::GetInstance(); + if (thunderBirdMainView != nullptr) { + int currentView = thunderBirdMainView->GetCurrentView(); + thunderBirdMainView->ChangeView( + currentView, + THUNDERBIRD_BLUEVIEW, + ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_DEVICE_LIST); + } + + // 停止扫描定时器 + LN_CancelBrScanHandle(); + return; + } + + // 检查是否达到最大扫描时间 + if (g_scanElapsedSeconds >= (MS_SCAN_TIME / 1000)) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "达到最大扫描时间,扫描结束"); + LN_CancelBrScanHandle(); + LN_EndScanHandle(); + } } + void LN_BluetoothScanTimerCallback(void *data) { GraphicService::GetInstance()->PostGraphicEvent(std::bind(LnBluetoothScanTimerCallbackProc, data)); @@ -68,6 +104,12 @@ namespace OHOS void LN_BluetoothScanStart(void) { + if (g_stopScanHandle_ln != nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_stopScanHandle_ln != nullptr"); + return; + } + bool ret; bool results; // 经典蓝牙扫描 @@ -86,16 +128,16 @@ namespace OHOS results = gap_br_start_discovery(); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动扫描 results %d", results); } - if (g_stopScanHandle_ln != nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "g_stopScanHandle_ln != nullptr"); - return; - } + // 重置扫描状态 + g_scanElapsedSeconds = 0; + g_hasJumpedToList = false; + // 扫描的时候保持屏幕常亮 /* -------------------------------------------------------------------------- */ ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); /* -------------------------------------------------------------------------- */ - uint32_t tick = GetOSTick(MS_SCAN_TIME); + // 改为每秒触发一次定时器(false表示重复触发) + uint32_t tick = GetOSTick(MS_SCAN_INTERVAL); g_stopScanHandle_ln = new GraphicTimer(tick, LN_BluetoothScanTimerCallback, nullptr, true); if (g_stopScanHandle_ln == nullptr) { diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueView.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueView.h index 7d7ae2ee6..30ee34a49 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueView.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueView.h @@ -28,6 +28,8 @@ namespace OHOS void RefreshDeviceList(void); void HandleBluetoothViewOperation(int16_t view); void ExecuteDisconnect(void); + void OnConnectFailed(void); + void CloseScan(void); // 按键操作接口(设备列表页面使用) void OnKeyUp(void); // 音量+键:选择上一个设备 diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueListAdapter.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueListAdapter.cpp index 69cab2d20..549550c38 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueListAdapter.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueListAdapter.cpp @@ -222,23 +222,35 @@ void ThunderBirdBlueListAdapter::BuildDisplayList() headerItem.isSelected = false; displayList_.push_back(headerItem); - // 添加已连接的设备 - for (auto& device : scanDevices) { - if (connectedCount >= MAX_CONNECTED_DEVICES) { - break; - } - - // 检查设备是否已连接 - if (bt_is_acl_connected((const bd_addr_t*)&device.addr)) { - DeviceListItem item; - item.type = DeviceItemType::DEVICE_CONNECTED; - item.deviceInfo = device; - item.isSelected = false; - displayList_.push_back(item); - connectedCount++; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added connected device: %s", device.deviceName); + // 直接添加已连接的设备(从 lastConnedAddr 获取,而不是从扫描列表) + DeviceListItem connectedItem; + connectedItem.type = DeviceItemType::DEVICE_CONNECTED; + connectedItem.isSelected = false; + + // 复制已连接设备地址 + (void)memcpy_s(connectedItem.deviceInfo.addr, BD_ADDR_LEN, + lastConnedAddr->addr, BD_ADDR_LEN); + + // 获取已连接设备名称 + char* connectedName = btModel->GetLastConnedDeviceName(); + if (connectedName != nullptr && strlen(connectedName) > 0) { + (void)strcpy_s((char*)connectedItem.deviceInfo.deviceName, BD_NAME_LEN, connectedName); + } else { + // 如果没有名称,尝试从扫描列表或配对列表中查找 + bool foundName = btModel->FindNameFromList(&connectedItem.deviceInfo); + if (!foundName) { + // 使用 MAC 地址作为名称 + snprintf((char*)connectedItem.deviceInfo.deviceName, BD_NAME_LEN, + "%02X:%02X:%02X:%02X:%02X:%02X", + lastConnedAddr->addr[5], lastConnedAddr->addr[4], + lastConnedAddr->addr[3], lastConnedAddr->addr[2], + lastConnedAddr->addr[1], lastConnedAddr->addr[0]); } } + + displayList_.push_back(connectedItem); + connectedCount = 1; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added connected device: %s", connectedItem.deviceInfo.deviceName); } // 2. 添加附近设备分组 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 5ff9d4c7d..9b1f754b4 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 @@ -728,6 +728,17 @@ namespace OHOS } } } + + // 处理连接失败操作:重新发送连接操作 + void ThunderBirdBlueView::OnConnectFailed(void) + { + if (currentShowView_ != THUNDERBIRD_BULEVIEW_CONNECT_FAILED) { + return; + } + + // 重新发送连接操作 + OnKeyConfirm(); + } // 获取当前选中的设备索引 int16_t ThunderBirdBlueView::GetSelectedDeviceIndex(void) const @@ -752,6 +763,12 @@ namespace OHOS ln_bt_scan_stop(); } } + + //关闭扫描 + void ThunderBirdBlueView::CloseScan(void) + { + ln_bt_scan_stop(); + } // 执行蓝牙断开连接操作 void ThunderBirdBlueView::ExecuteDisconnect(void) diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp index b2d327b35..02f1f59a5 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdMainView.cpp @@ -194,6 +194,7 @@ namespace OHOS } if (g_pThunderBirdBlueView) { + g_pThunderBirdBlueView->CloseScan(); delete g_pThunderBirdBlueView; g_pThunderBirdBlueView = nullptr; } diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp index 0201030d2..640dcc536 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdPresenter.cpp @@ -845,6 +845,10 @@ namespace OHOS case ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_DEVICE_LIST: ThunderBirdBlueView::GetInstance()->OnKeyUp();// 音量+键:选择上一个设备 break; + case ThunderBirdBlueView::THUNDERBIRD_BULEVIEW_CONNECT_FAILED: + //重新发送连接操作 + ThunderBirdBlueView::GetInstance()->OnConnectFailed(); + break; } } else if(keyType == KEY_TYPE_VENDOR_DOWN) -- 2.17.1