diff --git a/main/bluetooth_provisioning.cc b/main/bluetooth_provisioning.cc index 9cdc542..f46db1f 100644 --- a/main/bluetooth_provisioning.cc +++ b/main/bluetooth_provisioning.cc @@ -1252,13 +1252,47 @@ void BluetoothProvisioning::WiFiEventHandler(void* arg, esp_event_base_t event_b if (ret == ESP_OK) { ESP_LOGI(TAG, "✅ 成功获取WiFi扫描结果"); - for (int i = 0; i < ap_count; i++) { - ESP_LOGD(TAG, "WiFi[%d]: SSID=%s, RSSI=%d, 加密=%d", - i, ap_list[i].ssid, ap_list[i].rssi, ap_list[i].authmode); + // 过滤: 只保留2.4GHz频段(信道1-14),去重相同SSID(保留信号最强的) + // ESP-IDF返回结果已按RSSI降序排列,第一个出现的同名SSID即为信号最强 + uint16_t filtered_count = 0; + for (uint16_t i = 0; i < ap_count; i++) { + // 过滤5GHz频段 (信道 > 14) + if (ap_list[i].primary > 14) { + ESP_LOGD(TAG, "过滤5GHz: SSID=%s, 信道=%d, RSSI=%d", + ap_list[i].ssid, ap_list[i].primary, ap_list[i].rssi); + continue; + } + + // 过滤空SSID(隐藏网络) + uint8_t ssid_len = strlen(reinterpret_cast(ap_list[i].ssid)); + if (ssid_len == 0) { + continue; + } + + // SSID去重: 检查是否已存在相同SSID + bool duplicate = false; + for (uint16_t j = 0; j < filtered_count; j++) { + if (strcmp(reinterpret_cast(ap_list[j].ssid), + reinterpret_cast(ap_list[i].ssid)) == 0) { + duplicate = true; + break; + } + } + if (duplicate) { + continue; + } + + // 将符合条件的AP移到前面 + if (filtered_count != i) { + ap_list[filtered_count] = ap_list[i]; + } + filtered_count++; } - self->SendWiFiList(ap_list, ap_count); - ESP_LOGI(TAG, "📤 WiFi列表已发送,包含 %d 个热点", ap_count); + ESP_LOGI(TAG, "📊 过滤后剩余 %d 个2.4GHz热点 (原始: %d)", filtered_count, ap_count); + + self->SendWiFiList(ap_list, filtered_count); + ESP_LOGI(TAG, "📤 WiFi列表已发送,包含 %d 个热点", filtered_count); } else { ESP_LOGE(TAG, "❌ 获取WiFi扫描结果详细信息失败: %s", esp_err_to_name(ret)); } diff --git a/tests/ble_provision_test.py b/tests/ble_provision_test.py index f6ff8d9..6191c1e 100644 --- a/tests/ble_provision_test.py +++ b/tests/ble_provision_test.py @@ -46,7 +46,7 @@ except ImportError: SERVICE_UUID = "0000abf0-0000-1000-8000-00805f9b34fb" CHAR_WRITE_UUID = "0000abf1-0000-1000-8000-00805f9b34fb" CHAR_NOTIFY_UUID = "0000abf2-0000-1000-8000-00805f9b34fb" -DEFAULT_DEVICE = "Airhub_Ble" +DEFAULT_DEVICE = "Airhub_" # 前缀匹配,设备名格式: Airhub_xx:xx:xx:xx:xx:xx # 命令码 (手机→设备) CMD_SET_SSID = 0x01 @@ -135,16 +135,19 @@ class BleProvisionTester: return self.notifications[start:] async def scan_and_connect(self): - """扫描并连接设备""" - print(f"正在扫描设备 '{self.device_name}'...") - device = await BleakScanner.find_device_by_name( - self.device_name, timeout=10.0 - ) + """扫描并连接设备(支持前缀匹配)""" + print(f"正在扫描设备 '{self.device_name}*'...") + devices = await BleakScanner.discover(timeout=10.0) + device = None + for d in devices: + if d.name and d.name.startswith(self.device_name): + device = d + break if not device: - print(f"未找到设备 '{self.device_name}',请确认设备已开机且处于配网模式") + print(f"未找到以 '{self.device_name}' 开头的设备,请确认设备已开机且处于配网模式") return False - print(f"找到设备: {device.address}") + print(f"找到设备: {device.name} ({device.address})") print(f"正在连接...") self.client = BleakClient(device, timeout=15.0)