fix: wait for NM auto-reconnect before starting AP on reboot

After WiFi is configured and device reboots, NetworkManager needs
a few seconds to auto-connect to saved WiFi. Without waiting,
AP starts immediately and occupies wlan0, preventing NM reconnect.

- Add hasSavedWifiConnection() to check for saved WiFi profiles
- Wait up to 20s for NM auto-connect before falling back to AP
- First boot (no saved WiFi) still starts AP immediately

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-16 12:27:25 +08:00
parent b42e59fab8
commit f58db93b64
4 changed files with 89 additions and 23 deletions

View File

@@ -173,6 +173,22 @@ function sleep(ms) {
execSync(`sleep ${ms / 1000}`, { timeout: ms + 2000 });
}
/**
* 检测是否有已保存的 WiFi STA 连接(排除自身热点)
*/
function hasSavedWifiConnection() {
try {
const out = run('nmcli -t -f NAME,TYPE connection show');
for (const line of out.split('\n')) {
const [name, type] = line.split(':');
if (type === '802-11-wireless' && name !== CON_NAME) {
return true;
}
}
} catch (_) {}
return false;
}
/**
* 检测 wlan0 是否以 STA 模式连接了 WiFi排除自身热点
*/
@@ -192,6 +208,7 @@ function isWifiStaConnected() {
module.exports = {
hasInternet,
hasSavedWifiConnection,
isWifiStaConnected,
getWifiIface,
scanWifi,