ln:优化蓝牙设备列表操作逻辑
authorluozheng <1732036298@qq.com>
Sat, 29 Nov 2025 03:39:02 +0000 (11:39 +0800)
committerluozheng <1732036298@qq.com>
Sat, 29 Nov 2025 03:39:02 +0000 (11:39 +0800)
code/sdk/application/wearable/ln/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/include/ThunderBirdBlueListAdapter.h
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueListAdapter.cpp
code/sdk/application/wearable/ln/nativeapp/nativeui/thunderbird/src/ThunderBirdBlueView.cpp

index 5b24f52..6ca3161 100644 (file)
@@ -140,15 +140,13 @@ void BluetoothModel::Init()
     InitBluetoothInfo();\r
     BtInitCallBacks();\r
     wrapper_ble_register();\r
-#ifdef SUPPORT_ALIPAY_SEC\r
-    svr_alipay_bluetooth_init();\r
-#endif\r
+\r
     BtInitCallBacksEx();\r
     BtInitGatts();\r
     // BtInitSpp();\r
     InitBtHeadsetEvent(); // 蓝牙ct和tg适配初始化\r
     // PhoneServiceInit();\r
-    gap_remove_all_pairs();\r
+    // gap_remove_all_pairs();\r
     BtA2dpInit();\r
 \r
     /* 实验室测试需要,开机开广播 */\r
@@ -307,6 +305,19 @@ void BluetoothModel::AddConnedDevicesCase(BluetoothCaseInfo caseInfo)
 \r
 List<BluetoothCaseInfo>& BluetoothModel::GetConnedDevicesCase()\r
 {\r
+    // 确保已连接设备列表已经初始化\r
+    if (btConnedDevicesList_ == nullptr) {\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetConnedDevicesCase: btConnedDevicesList_ is nullptr, calling SetUpConnedDevicesList");\r
+        SetUpConnedDevicesList();\r
+    }\r
+\r
+    // 如果仍然为空,返回一个静态空列表,防止野指针导致异常\r
+    if (btConnedDevicesList_ == nullptr) {\r
+        static List<BluetoothCaseInfo> emptyList;\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetConnedDevicesCase: btConnedDevicesList_ still nullptr, return empty list");\r
+        return emptyList;\r
+    }\r
+\r
     return *btConnedDevicesList_;\r
 }\r
 \r
index a312cc5..1a9487c 100644 (file)
@@ -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:
     // 构建显示列表
index 549550c..977a208 100644 (file)
@@ -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<BluetoothCaseInfo>& connedDevices = btModel->GetConnedDevicesCase();
     std::list<BluetoothCaseInfo>& 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<BluetoothCaseInfo>* 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<const bd_addr_t*>(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);
index eb52bbd..703427e 100644 (file)
@@ -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);
+            }
+        }
     }
     
     // 按键操作:选择上一个设备(音量+键)