uint16_t connectedCount = 0;
uint16_t nearbyCount = 0;
- // 1. “我的设备”分组:显示已绑定的音频设备列表(最多显示 MAX_CONNECTED_DEVICES 个)
+ // 1. “我的设备”分组:显示已绑定的音频设备列表(优先显示已连接的设备)
if (!connedDevices.IsEmpty()) {
DeviceListItem headerItem;
headerItem.type = DeviceItemType::SECTION_HEADER_CONNECTED;
headerItem.isSelected = false;
displayList_.push_back(headerItem);
- // 遍历已绑定设备列表,最多添加 MAX_CONNECTED_DEVICES 个
+ // 第一步:先收集所有已连接和未连接的设备
+ std::list<BluetoothCaseInfo> connectedDevicesList; // 已连接的设备
+ std::list<BluetoothCaseInfo> unconnectedDevicesList; // 未连接的设备
+
ListNode<BluetoothCaseInfo>* node = connedDevices.Begin();
- while (node != connedDevices.End() && connectedCount < MAX_CONNECTED_DEVICES) {
+ while (node != connedDevices.End()) {
+ if (bt_is_acl_connected((const bd_addr_t*)&node->data_.addr)) {
+ connectedDevicesList.push_back(node->data_);
+ } else {
+ unconnectedDevicesList.push_back(node->data_);
+ }
+ node = node->next_;
+ }
+
+ // 第二步:优先添加已连接的设备
+ for (auto& device : connectedDevicesList) {
+ if (connectedCount >= MAX_CONNECTED_DEVICES) {
+ break;
+ }
DeviceListItem item;
item.type = DeviceItemType::DEVICE_CONNECTED;
- item.deviceInfo = node->data_;
+ item.deviceInfo = device;
item.isSelected = false;
displayList_.push_back(item);
connectedCount++;
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added my device: %s", item.deviceInfo.deviceName);
- node = node->next_;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Added my device (connected): %s", device.deviceName);
+ }
+
+ // 第三步:再添加未连接的设备(如果还有空间)
+ for (auto& device : unconnectedDevicesList) {
+ if (connectedCount >= MAX_CONNECTED_DEVICES) {
+ break;
+ }
+ 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 my device (unconnected): %s", device.deviceName);
}
}