fix: select strongest AP for matching WiFi SSID (#2655)

This commit is contained in:
Leopoldo Pla Sempere
2026-07-20 18:17:16 +03:00
committed by GitHub
parent 3319aa1720
commit e03aa16330
@@ -354,6 +354,11 @@ void WifiSelectionActivity::attemptConnection() {
WiFi.disconnect(true, true); // Abort any in-progress SDK auto-connect and clear NVS-saved SSID
delay(100);
// Scan all channels so networks with multiple APs use the strongest matching
// BSSID instead of the first match found by the framework's default fast scan.
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
// Set hostname so routers show "CrossPoint-Reader-AABBCCDDEEFF" instead of "esp32-XXXXXXXXXXXX"
String mac = WiFi.macAddress();
mac.replace(":", "");
@@ -382,6 +387,16 @@ void WifiSelectionActivity::checkConnectionStatus() {
connectedIP = ipStr;
autoConnecting = false;
#if defined(ENABLE_SERIAL_LOG) && LOG_LEVEL >= 2
uint8_t connectedBssid[6] = {};
WiFi.BSSID(connectedBssid);
LOG_DBG("WIFI", "Connected BSSID: %02x:%02x:%02x:%02x:%02x:%02x, channel: %d, RSSI: %d dBm",
static_cast<unsigned>(connectedBssid[0]), static_cast<unsigned>(connectedBssid[1]),
static_cast<unsigned>(connectedBssid[2]), static_cast<unsigned>(connectedBssid[3]),
static_cast<unsigned>(connectedBssid[4]), static_cast<unsigned>(connectedBssid[5]), WiFi.channel(),
WiFi.RSSI());
#endif
// Sync RTC from NTP on the first successful WiFi connection only. The DS3231
// drifts ~2 ppm so one sync is enough; users can force a re-sync from
// Settings > Customise Status Bar > Sync clock now.