From 58a58d6708123ad498db526295c2411f542bba25 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Sat, 3 Dec 2022 14:11:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0onWifiConnectedWithPartialInf?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 4 ++ uni_modules/uni-wifi/package.json | 4 +- .../uni-wifi/utssdk/app-android/index.uts | 50 ++++++++++++++++--- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 79bc2e1..72d824b 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -86,6 +86,10 @@ console.log("onWifiConnected"); console.log(res); }); + uni.onWifiConnectedWithPartialInfo(function(res){ + console.log("onWifiConnectedWithPartialInfo"); + console.log(res); + }); },fail:(res)=>{ console.log("fail: " + JSON.stringify(res)); diff --git a/uni_modules/uni-wifi/package.json b/uni_modules/uni-wifi/package.json index d6b43d8..a59d72d 100644 --- a/uni_modules/uni-wifi/package.json +++ b/uni_modules/uni-wifi/package.json @@ -39,7 +39,9 @@ "getWifiList":"getWifiList", "onGetWifiList":"onGetWifiList", "onWifiConnected":"onWifiConnected", - "connectWifi":"connectWifi" + "connectWifi":"connectWifi", + "onWifiConnectedWithPartialInfo":"onWifiConnectedWithPartialInfo", + "offWifiConnectedWithPartialInfo":"offWifiConnectedWithPartialInfo" } }, "dependencies": [], diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 2092a52..2e1e5f1 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -66,6 +66,7 @@ class Global { static getWifiListCallbackList: UTSCallback[] = [] // wifi链接监听 static onWifiConnectCallbackList: UTSCallback[] = [] + static onWifiConnectWithPartialInfoCallbackList: UTSCallback[] = [] } @@ -140,8 +141,6 @@ class UniWifiInfo { * 根据connectInfo 链接信息对wifi数据结构进行初始化 */ wrapConnectInfo(connectInfo: WifiInfo): void { - console.log(connectInfo.getSSID()); - console.log(connectInfo.getBSSID()); if (connectInfo.getBSSID() == null) { return } @@ -160,7 +159,8 @@ class UniWifiInfo { this.frequency = connectInfo.getFrequency(); } - + + } /** @@ -230,6 +230,16 @@ function getSecurityType(result:ScanResult):string { } +function zeroCountNum(source:string):number{ + var splitted = source.split(":") + var countNum = 0; + for(perItem in splitted){ + if(perItem == "00"){ + countNum += 1 + } + } + return countNum +} /** * 自定义wifi变化广播监听器 */ @@ -257,7 +267,8 @@ class CustomBroadcastReceiver extends BroadcastReceiver { // BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout // @ts-ignore let winfo = this.mWifiManager!.getConnectionInfo(); - while (winfo.bssid == null) { + + while (winfo.bssid == null || zeroCountNum(winfo.bssid) > 4) { Thread.sleep(1000) winfo = this.mWifiManager!.getConnectionInfo(); } @@ -273,6 +284,13 @@ class CustomBroadcastReceiver extends BroadcastReceiver { for (let perCallback in Global.onWifiConnectCallbackList) { perCallback(res); } + // 封装仅SSID 数据对象 + var connectedWithPartialInfo = { + SSID:uniWifiInfo.SSID + } + for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) { + perCallback(connectedWithPartialInfo); + } }, 100); } @@ -377,6 +395,7 @@ export function startWifi(option: WifiOption) { Global.scanList = [] Global.getWifiListCallbackList = [] Global.onWifiConnectCallbackList = [] + Global.onWifiConnectWithPartialInfoCallbackList = [] } }); @@ -434,6 +453,12 @@ export function onWifiConnected(callback: UTSCallback) { Global.onWifiConnectCallbackList.push(callback) } + +export function onWifiConnectedWithPartialInfo(callback: UTSCallback) { + Global.onWifiConnectWithPartialInfoCallbackList.push(callback) +} + + /** * wifi 链接成功的回调取消注册 */ @@ -444,6 +469,13 @@ export function offWifiConnected(callback: UTSCallback) { } } +export function offWifiConnectedWithPartialInfo(callback: UTSCallback) { + let callbackIndex = Global.onWifiConnectWithPartialInfoCallbackList.indexOf(callback) + if (callbackIndex > 0) { + Global.onWifiConnectWithPartialInfoCallbackList.splice(callbackIndex, 1); + } +} + /** * 注册Wifi列表的监听事件 */ @@ -567,15 +599,19 @@ export function connectWifi(option: WifiConnectOption) { } catch (e) { connected = false; - // TODO: handle exception console.log(e); - // e.printStackTrace(); - } if(!connected){ // 出错了,返回错误 + // 兜底的报错 + result.errCode = 12010 + result.errMsg = "connectWifi:fail:unknown error" + + option.fail?.(result) + option.complete?.(result) + return } result.errCode = 0 -- GitLab