wifiScan.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --- 模块功能:wifi扫描功能
  2. -- 支持wifi热点扫描
  3. -- @module wifiScan
  4. -- @author openLuat
  5. -- @license MIT
  6. -- @copyright openLuat
  7. -- @release 2020.5.21
  8. require"sys"
  9. module(..., package.seeall)
  10. local sCbFnc
  11. --- wifi扫描热点请求
  12. -- @function cbFnc 扫描到热点返回或者超时未返回的回调函数,回调函数的调用形式为:
  13. -- cbFnc(result,cnt,info)
  14. -- result:true或者false,true表示扫描成功,false表示扫描失败或者超时失败
  15. -- cnt:number类型,表示扫描到的热点个数
  16. -- info:table或者nil类型;result为false时,为nil;result为true时,表示扫码到的热点mac和信号信息,table类型,例如:
  17. -- {
  18. -- ["1a:fe:34:9e:a1:77"] = -63,
  19. -- ["8c:be:be:2d:cd:e9"] = -81,
  20. -- ["20:4e:7f:82:c2:c4"] = -70,
  21. -- }
  22. -- @number[opt=10000] timeout 等待扫描热点返回的超时时间,单位毫秒,默认为10秒
  23. -- @usage
  24. -- wifiScan.request(cbFnc)
  25. -- wifiScan.request(cbFnc,5000)
  26. function request(cbFnc,timeout)
  27. sCbFnc = cbFnc
  28. sys.timerStart(sCbFnc,timeout or 10000,false)
  29. wifi.getinfo()
  30. end
  31. local function wifiMsg(msg)
  32. log.info("wifiScan.wifiMsg",msg.cnt,msg.info,sys.timerIsActive(sCbFnc,false))
  33. if sys.timerIsActive(sCbFnc,false) then
  34. sys.timerStop(sCbFnc,false)
  35. local num,info = msg.cnt,msg.info
  36. if num==0 then
  37. sCbFnc(false,0)
  38. else
  39. --9a0074bdb0e8,183,2;40313cd7b4bb,185,4;828917c49d9a,173,2;8107999c460,175,8;c4b548f863e,160,7;
  40. log.info("wifi.getinfo",num,info)
  41. local tInfo,cnt = {},0
  42. for mac,rssi,channel in string.gmatch(info,"(.-),(.-),(.-);") do
  43. cnt = cnt+1
  44. if mac:len()<12 then mac=string.rep("0",12-mac:len())..mac end
  45. 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)
  46. end
  47. sCbFnc(true,cnt,tInfo)
  48. end
  49. end
  50. end
  51. --注册core上报的rtos.MSG_WIFI消息的处理函数
  52. rtos.on(rtos.MSG_WIFI,wifiMsg)