提交 58a58d67 编写于 作者: 杜庆泉's avatar 杜庆泉

增加onWifiConnectedWithPartialInfo

上级 f60e237f
...@@ -86,6 +86,10 @@ ...@@ -86,6 +86,10 @@
console.log("onWifiConnected"); console.log("onWifiConnected");
console.log(res); console.log(res);
}); });
uni.onWifiConnectedWithPartialInfo(function(res){
console.log("onWifiConnectedWithPartialInfo");
console.log(res);
});
},fail:(res)=>{ },fail:(res)=>{
console.log("fail: " + JSON.stringify(res)); console.log("fail: " + JSON.stringify(res));
......
...@@ -39,7 +39,9 @@ ...@@ -39,7 +39,9 @@
"getWifiList":"getWifiList", "getWifiList":"getWifiList",
"onGetWifiList":"onGetWifiList", "onGetWifiList":"onGetWifiList",
"onWifiConnected":"onWifiConnected", "onWifiConnected":"onWifiConnected",
"connectWifi":"connectWifi" "connectWifi":"connectWifi",
"onWifiConnectedWithPartialInfo":"onWifiConnectedWithPartialInfo",
"offWifiConnectedWithPartialInfo":"offWifiConnectedWithPartialInfo"
} }
}, },
"dependencies": [], "dependencies": [],
......
...@@ -66,6 +66,7 @@ class Global { ...@@ -66,6 +66,7 @@ class Global {
static getWifiListCallbackList: UTSCallback[] = [] static getWifiListCallbackList: UTSCallback[] = []
// wifi链接监听 // wifi链接监听
static onWifiConnectCallbackList: UTSCallback[] = [] static onWifiConnectCallbackList: UTSCallback[] = []
static onWifiConnectWithPartialInfoCallbackList: UTSCallback[] = []
} }
...@@ -140,8 +141,6 @@ class UniWifiInfo { ...@@ -140,8 +141,6 @@ class UniWifiInfo {
* 根据connectInfo 链接信息对wifi数据结构进行初始化 * 根据connectInfo 链接信息对wifi数据结构进行初始化
*/ */
wrapConnectInfo(connectInfo: WifiInfo): void { wrapConnectInfo(connectInfo: WifiInfo): void {
console.log(connectInfo.getSSID());
console.log(connectInfo.getBSSID());
if (connectInfo.getBSSID() == null) { if (connectInfo.getBSSID() == null) {
return return
} }
...@@ -160,7 +159,8 @@ class UniWifiInfo { ...@@ -160,7 +159,8 @@ class UniWifiInfo {
this.frequency = connectInfo.getFrequency(); this.frequency = connectInfo.getFrequency();
} }
} }
/** /**
...@@ -230,6 +230,16 @@ function getSecurityType(result:ScanResult):string { ...@@ -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变化广播监听器 * 自定义wifi变化广播监听器
*/ */
...@@ -257,7 +267,8 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -257,7 +267,8 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout // BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
// @ts-ignore // @ts-ignore
let winfo = this.mWifiManager!.getConnectionInfo(); let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null) {
while (winfo.bssid == null || zeroCountNum(winfo.bssid) > 4) {
Thread.sleep(1000) Thread.sleep(1000)
winfo = this.mWifiManager!.getConnectionInfo(); winfo = this.mWifiManager!.getConnectionInfo();
} }
...@@ -273,6 +284,13 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -273,6 +284,13 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
for (let perCallback in Global.onWifiConnectCallbackList) { for (let perCallback in Global.onWifiConnectCallbackList) {
perCallback(res); perCallback(res);
} }
// 封装仅SSID 数据对象
var connectedWithPartialInfo = {
SSID:uniWifiInfo.SSID
}
for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) {
perCallback(connectedWithPartialInfo);
}
}, 100); }, 100);
} }
...@@ -377,6 +395,7 @@ export function startWifi(option: WifiOption) { ...@@ -377,6 +395,7 @@ export function startWifi(option: WifiOption) {
Global.scanList = [] Global.scanList = []
Global.getWifiListCallbackList = [] Global.getWifiListCallbackList = []
Global.onWifiConnectCallbackList = [] Global.onWifiConnectCallbackList = []
Global.onWifiConnectWithPartialInfoCallbackList = []
} }
}); });
...@@ -434,6 +453,12 @@ export function onWifiConnected(callback: UTSCallback) { ...@@ -434,6 +453,12 @@ export function onWifiConnected(callback: UTSCallback) {
Global.onWifiConnectCallbackList.push(callback) Global.onWifiConnectCallbackList.push(callback)
} }
export function onWifiConnectedWithPartialInfo(callback: UTSCallback) {
Global.onWifiConnectWithPartialInfoCallbackList.push(callback)
}
/** /**
* wifi 链接成功的回调取消注册 * wifi 链接成功的回调取消注册
*/ */
...@@ -444,6 +469,13 @@ export function offWifiConnected(callback: UTSCallback) { ...@@ -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列表的监听事件 * 注册Wifi列表的监听事件
*/ */
...@@ -567,15 +599,19 @@ export function connectWifi(option: WifiConnectOption) { ...@@ -567,15 +599,19 @@ export function connectWifi(option: WifiConnectOption) {
} catch (e) { } catch (e) {
connected = false; connected = false;
// TODO: handle exception
console.log(e); console.log(e);
// e.printStackTrace();
} }
if(!connected){ if(!connected){
// 出错了,返回错误 // 出错了,返回错误
// 兜底的报错
result.errCode = 12010
result.errMsg = "connectWifi:fail:unknown error"
option.fail?.(result)
option.complete?.(result)
return
} }
result.errCode = 0 result.errCode = 0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册