From 4fd9a53fa5713c7e1fcb7884b624bbf9492b6eb5 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Tue, 28 Mar 2023 20:26:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=A1=A5=E4=B8=8E=E5=BE=AE=E4=BF=A1wi?= =?UTF-8?q?fi=20=E9=83=A8=E5=88=86api=20=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 16 ++-- .../uni-wifi/utssdk/app-android/index.uts | 74 ++++++++++++------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 445436c..05da7ac 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -43,14 +43,14 @@ uni.startWifi({ success:(res)=> { console.log("success: " + JSON.stringify(res)); - uni.connectWifi({ - maunal:false, - SSID:"Xiaomi_20D0", - password:"BBB111", - complete:(res)=>{ - console.log(res); - } - }); + // uni.connectWifi({ + // maunal:false, + // SSID:"Xiaomi_20D0", + // password:"BBB111", + // complete:(res)=>{ + // console.log(res); + // } + // }); },fail:(res)=>{ console.log("fail: " + JSON.stringify(res)); },complete:(res)=>{ diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 0859c56..bac1493 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -32,7 +32,9 @@ class Global { // 扫描wifi结果 static scanList : AndroidUniWifiInfo[] = [] // 获取wifi列表监听 - static getWifiListCallbackList : UTSCallback[] = [] + static onGetWifiListCallback : UTSCallback | null = null + static supendGetWifiSuccess : ((res: UniWifiResult) => void) | null = null + static supendGetWifiComplete : ((res: UniWifiResult) => void) | null = null // wifi链接监听 static onWifiConnectCallbackList : UTSCallback[] = [] static onWifiConnectWithPartialInfoCallbackList : UTSCallback[] = [] @@ -303,9 +305,23 @@ class CustomBroadcastReceiver extends BroadcastReceiver { if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) { startWifiScaning = false; + + let ret : UniWifiResult = { + errCode: 0, + errSubject: "uni-getWifiList", + errMsg: "getWifiList:ok" + } + if(Global.supendGetWifiSuccess != null){ + Global.supendGetWifiSuccess?.(ret) + } + if(Global.supendGetWifiComplete != null){ + Global.supendGetWifiComplete?.(ret) + } + // wifi 扫描结果回调 let results = this.mWifiManager!.scanResults; + if (results != null) { Global.scanList = [] for (let scanResult in results) { @@ -316,13 +332,16 @@ class CustomBroadcastReceiver extends BroadcastReceiver { } // 挨个通知,所有的监听器 - for (let perCallback in Global.getWifiListCallbackList) { - + if(Global.onGetWifiListCallback != null){ const data = new JSONObject(); - // let mainJsonStr = Gson().toJson(Global.scanList); data["wifiList"] = Global.scanList - perCallback(data); + Global.onGetWifiListCallback?.(data); } + // for (let perCallback in Global.onGetWifiListCallback) { + // const data = new JSONObject(); + // data["wifiList"] = Global.scanList + // perCallback(data); + // } } @@ -378,9 +397,18 @@ export function startWifi(option : WifiOption) { return; } // 初始化wifi 状态广播监听,后续所有的api,均基于此 - if (Global.mReceiver == null) { - Global.mReceiver = new CustomBroadcastReceiver(wifiManager) + if(Global.mReceiver != null){ + // 说明已经注册过了 + result.errCode = 0 + result.errMsg = "startWifi:ok" + + option.success?.(result) + option.complete?.(result) + return } + + + Global.mReceiver = new CustomBroadcastReceiver(wifiManager) let filter = new IntentFilter() filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) @@ -401,7 +429,7 @@ export function startWifi(option : WifiOption) { Global.mReceiver = null Global.scanList = [] - Global.getWifiListCallbackList = [] + Global.onGetWifiListCallback = null Global.onWifiConnectCallbackList = [] Global.onWifiConnectWithPartialInfoCallbackList = [] } @@ -442,15 +470,13 @@ export function getWifiList(option : WifiOption) { let wifiManager : WifiManager = UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager - wifiManager.startScan() - let ret : UniWifiResult = { - errCode: 0, - errSubject: "uni-getWifiList", - errMsg: "getWifiList:ok" - } - option.success?.(ret) - option.complete?.(ret) + + Global.supendGetWifiSuccess = option.success + Global.supendGetWifiComplete = option.complete + + wifiManager.startScan() + } @@ -505,21 +531,14 @@ export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { * 注册Wifi列表的监听事件 */ export function onGetWifiList(callback : UTSCallback) { - Global.getWifiListCallbackList.push(callback) + Global.onGetWifiListCallback = callback } /** * 取消注册Wifi列表的监听事件 */ export function offGetWifiList(callback? : UTSCallback) { - if(callback == null){ - Global.getWifiListCallbackList = [] - return - } - let callbackIndex = Global.getWifiListCallbackList.indexOf(callback) - if (callbackIndex >= 0) { - Global.getWifiListCallbackList.splice(callbackIndex, 1); - } + Global.onGetWifiListCallback = null } @@ -737,10 +756,11 @@ export function stopWifi(option : WifiOption) { //TODO handle the exception } - Global.getWifiListCallbackList = [] + Global.onGetWifiListCallback = null Global.onWifiConnectWithPartialInfoCallbackList = [] Global.onWifiConnectCallbackList = [] - + Global.mReceiver = null + let result : UniWifiResult = { errCode: 0, errSubject: "uni-stopWifi", -- GitLab