fix: pre-scan WiFi before entering AP mode

wlan0 cannot scan while in AP mode (hardware limitation).
Now scan nearby networks before starting hotspot and cache
the results for the captive portal page.

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-16 13:04:00 +08:00
parent 64f4050014
commit c6f55c8c15
3 changed files with 22 additions and 20 deletions

View File

@@ -2,7 +2,7 @@
const http = require('http');
const log = require('./logger');
const { scanWifi } = require('./network');
// scanWifi 不再在此调用AP 模式下无法扫描),改用缓存
const { CAPTIVE_DOMAIN } = require('./dns-hijack');
const PORT = 80;
@@ -30,9 +30,10 @@ const CAPTIVE_DETECT_PATHS = new Set([
*/
class CaptiveServer {
constructor(opts = {}) {
this._server = null;
this._clawId = opts.clawId || '???';
this._onConnect = opts.onConnect || null; // (ssid, password) => Promise<{success, error?}>
this._server = null;
this._clawId = opts.clawId || '???';
this._onConnect = opts.onConnect || null;
this._cachedWifiList = opts.cachedWifiList || [];
}
startListening() {
@@ -96,8 +97,8 @@ class CaptiveServer {
// ── API ──────────────────────────────────────────────────────────────────
_apiScan(req, res) {
const list = scanWifi();
this._json(res, { wifi: list });
// AP 模式下 wlan0 无法扫描,返回开 AP 前的缓存结果
this._json(res, { wifi: this._cachedWifiList, cached: true });
}
async _apiConnect(req, res) {
@@ -227,7 +228,9 @@ async function doScan(){
o.textContent=w.ssid+' ('+w.signal+'% '+w.security+')';
sel.appendChild(o);
});
setStatus('扫描到 '+d.wifi.length+' 个网络','ok');
var msg='发现 '+d.wifi.length+' 个网络';
if(d.wifi.length===0) msg='未发现网络,请手动输入 SSID';
setStatus(msg,'ok');
}catch(e){setStatus('扫描失败: '+e.message,'err')}
$('connectBtn').disabled=false;
}