From 7927c4e176a89b6c35ee7173df557d693f6e9dac Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Sat, 29 Nov 2025 11:39:02 +0800 Subject: [PATCH] =?utf8?q?ln:=E4=BC=98=E5=8C=96=E8=93=9D=E7=89=99=E8=AE=BE?= =?utf8?q?=E5=A4=87=E5=88=97=E8=A1=A8=E6=93=8D=E4=BD=9C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/bluetooth/src/BluetoothModel.cpp | 19 ++++- .../include/ThunderBirdBlueListAdapter.h | 5 +- .../src/ThunderBirdBlueListAdapter.cpp | 76 ++++++------------- .../thunderbird/src/ThunderBirdBlueView.cpp | 12 +++ 4 files changed, 52 insertions(+), 60 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 5b24f5281..6ca3161e8 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 @@ -140,15 +140,13 @@ void BluetoothModel::Init() InitBluetoothInfo(); BtInitCallBacks(); wrapper_ble_register(); -#ifdef SUPPORT_ALIPAY_SEC - svr_alipay_bluetooth_init(); -#endif + BtInitCallBacksEx(); BtInitGatts(); // BtInitSpp(); InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化 // PhoneServiceInit(); - gap_remove_all_pairs(); + // gap_remove_all_pairs(); BtA2dpInit(); /* 实验室测试需要,开机开广播 */ @@ -307,6 +305,19 @@ void BluetoothModel::AddConnedDevicesCase(BluetoothCaseInfo caseInfo) List& BluetoothModel::GetConnedDevicesCase() { + // 确保已连接设备列表已经初始化 + if (btConnedDevicesList_ == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetConnedDevicesCase: btConnedDevicesList_ is nullptr, calling SetUpConnedDevicesList"); + SetUpConnedDevicesList(); + } + + // 如果仍然为空,返回一个静态空列表,防止野指针导致异常 + if (btConnedDevicesList_ == nullptr) { + static List emptyList; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetConnedDevicesCase: btConnedDevicesList_ still nullptr, return empty list"); + return emptyList; + } + return *btConnedDevicesList_; } diff --git a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueListAdapter.h b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueListAdapter.h index a312cc585..1a9487c8e 100644 --- a/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueListAdapter.h +++ b/code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueListAdapter.h @@ -85,8 +85,9 @@ public: uint16_t GetDeviceCount() const; // 限制常量 - static constexpr uint16_t MAX_CONNECTED_DEVICES = 1; // 最多显示1个已连接设备 - static constexpr uint16_t MAX_NEARBY_DEVICES = 10; // 最多显示10个附近设备 + // “我的设备”中显示已绑定的音频设备,最多显示2个 + static constexpr uint16_t MAX_CONNECTED_DEVICES = 2; + static constexpr uint16_t MAX_NEARBY_DEVICES = 10; // “附近的设备”最多显示10个 private: // 构建显示列表 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 549550c38..977a208e4 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 @@ -187,70 +187,35 @@ void ThunderBirdBlueListAdapter::BuildDisplayList() return; } - // 获取最后连接的设备地址 - bd_addr_t* lastConnedAddr = btModel->GetLastConnedAddr(); - bool hasConnectedDevice = false; - - // 检查是否有已连接的设备 - if (lastConnedAddr != nullptr) { - // 检查地址是否为全0(表示没有连接) - bool isValidAddr = false; - for (int i = 0; i < BD_ADDR_LEN; i++) { - if (lastConnedAddr->addr[i] != 0) { - isValidAddr = true; - break; - } - } - if (isValidAddr && bt_is_acl_connected(lastConnedAddr)) { - hasConnectedDevice = true; - } - } - - // 获取扫描到的设备列表 + // 获取已绑定设备列表(“我的设备”)和扫描到的附近设备列表 + List& connedDevices = btModel->GetConnedDevicesCase(); std::list& scanDevices = btModel->GetCase(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "scanDevices size=%d hasConnected=%d", - scanDevices.size(), hasConnectedDevice); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BuildDisplayList: connedDevices=%d, scanDevices=%d", + connedDevices.Size(), scanDevices.size()); // 构建显示列表 uint16_t connectedCount = 0; uint16_t nearbyCount = 0; - // 1. 添加已连接设备分组(如果有已连接设备) - if (hasConnectedDevice) { + // 1. “我的设备”分组:显示已绑定的音频设备列表(最多显示 MAX_CONNECTED_DEVICES 个) + if (!connedDevices.IsEmpty()) { DeviceListItem headerItem; headerItem.type = DeviceItemType::SECTION_HEADER_CONNECTED; headerItem.isSelected = false; displayList_.push_back(headerItem); - // 直接添加已连接的设备(从 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]); - } + // 遍历已绑定设备列表,最多添加 MAX_CONNECTED_DEVICES 个 + ListNode* node = connedDevices.Begin(); + while (node != connedDevices.End() && connectedCount < MAX_CONNECTED_DEVICES) { + DeviceListItem item; + item.type = DeviceItemType::DEVICE_CONNECTED; + item.deviceInfo = node->data_; + item.isSelected = false; + displayList_.push_back(item); + connectedCount++; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added my device: %s", item.deviceInfo.deviceName); + node = node->next_; } - - displayList_.push_back(connectedItem); - connectedCount = 1; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added connected device: %s", connectedItem.deviceInfo.deviceName); } // 2. 添加附近设备分组 @@ -328,11 +293,14 @@ UIView* ThunderBirdBlueListAdapter::GetView(UIView* inView, int16_t index) WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Setting SECTION_HEADER_NEARBY"); item->SetSectionHeader("附近的设备"); break; - case DeviceItemType::DEVICE_CONNECTED: + case DeviceItemType::DEVICE_CONNECTED: { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Setting DEVICE_CONNECTED: %s", it->deviceInfo.deviceName); - item->SetDeviceInfo(it->deviceInfo, true); + // 仅对当前实际已连接的设备显示橙色小圆点 + bool isConnectedNow = bt_is_acl_connected(reinterpret_cast(it->deviceInfo.addr)); + item->SetDeviceInfo(it->deviceInfo, isConnectedNow); item->SetSelected(index == selectedIndex_); break; + } case DeviceItemType::DEVICE_NEARBY: WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Setting DEVICE_NEARBY: %s", it->deviceInfo.deviceName); item->SetDeviceInfo(it->deviceInfo, false); 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 eb52bbd59..703427ea3 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 @@ -768,6 +768,18 @@ namespace OHOS DeviceList->RefreshList(); int16_t selectedIndex = deviceListAdapter_->GetSelectedIndex(); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateDeviceListSelection: selectedIndex=%d", selectedIndex); + + // 让当前选中的项滚动到可见区域(模拟手指滑动) + // 特殊处理:“我的设备”分组的第一项(索引一般为1),需要保证分组标题也能看到 + if (selectedIndex >= 0) { + // 如果选中的是“我的设备”中的第一台设备(通常是索引1), + // 则滚动到索引0,这样标题“我的设备”也会一起显示出来。 + if (selectedIndex == 1) { + DeviceList->ScrollTo(0); + } else { + DeviceList->ScrollTo(selectedIndex); + } + } } // 按键操作:选择上一个设备(音量+键) -- 2.17.1