Files
BR_YKC/4G/code/lib/wifiScan.lua
2026-03-31 15:46:04 +08:00

58 lines
2.1 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--- 模块功能wifi扫描功能
-- 支持wifi热点扫描
-- @module wifiScan
-- @author openLuat
-- @license MIT
-- @copyright openLuat
-- @release 2020.5.21
require"sys"
module(..., package.seeall)
local sCbFnc
--- wifi扫描热点请求
-- @function cbFnc 扫描到热点返回或者超时未返回的回调函数,回调函数的调用形式为:
-- cbFnc(result,cnt,info)
-- resulttrue或者falsetrue表示扫描成功false表示扫描失败或者超时失败
-- cntnumber类型表示扫描到的热点个数
-- infotable或者nil类型result为false时为nilresult为true时表示扫码到的热点mac和信号信息table类型例如
-- {
-- ["1a:fe:34:9e:a1:77"] = -63,
-- ["8c:be:be:2d:cd:e9"] = -81,
-- ["20:4e:7f:82:c2:c4"] = -70,
-- }
-- @number[opt=10000] timeout 等待扫描热点返回的超时时间单位毫秒默认为10秒
-- @usage
-- wifiScan.request(cbFnc)
-- wifiScan.request(cbFnc,5000)
function request(cbFnc,timeout)
sCbFnc = cbFnc
sys.timerStart(sCbFnc,timeout or 10000,false)
wifi.getinfo()
end
local function wifiMsg(msg)
log.info("wifiScan.wifiMsg",msg.cnt,msg.info,sys.timerIsActive(sCbFnc,false))
if sys.timerIsActive(sCbFnc,false) then
sys.timerStop(sCbFnc,false)
local num,info = msg.cnt,msg.info
if num==0 then
sCbFnc(false,0)
else
--9a0074bdb0e8,183,2;40313cd7b4bb,185,4;828917c49d9a,173,2;8107999c460,175,8;c4b548f863e,160,7;
log.info("wifi.getinfo",num,info)
local tInfo,cnt = {},0
for mac,rssi,channel in string.gmatch(info,"(.-),(.-),(.-);") do
cnt = cnt+1
if mac:len()<12 then mac=string.rep("0",12-mac:len())..mac end
tInfo[mac:sub(1,2)..":"..mac:sub(3,4)..":"..mac:sub(5,6)..":"..mac:sub(7,8)..":"..mac:sub(9,10)..":"..mac:sub(11,12)] = tonumber(rssi)
end
sCbFnc(true,cnt,tInfo)
end
end
end
--注册core上报的rtos.MSG_WIFI消息的处理函数
rtos.on(rtos.MSG_WIFI,wifiMsg)