diff --git a/pages/index/index.vue b/pages/index/index.vue index 79bc2e13c4ae41a8d1333373195303244a21be8d..72d824bb83208e619ed53c050014ac98b74f643b 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 d6b43d8c1807354ee8bf0b7ad04df2d77486ec01..a59d72dfe3fe80c5b06c3aa278e3c60fad173f45 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 2092a528dc0210ea1c8daafc0b8ace5bc72971fd..2e1e5f15a6083234024e22110481ae3a16b34460 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