提交 3a9c9515 编写于 作者: 杜庆泉's avatar 杜庆泉

uni-wfi 优化onConnect 等待逻辑

上级 6afd901e
...@@ -253,31 +253,22 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -253,31 +253,22 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) { if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
let state = let state =
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN) intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
if (state == WifiManager.WIFI_STATE_ENABLED) { if (state == WifiManager.WIFI_STATE_ENABLED) {
// 获取当前的connectInfo 并且进行数据封装 // 获取当前的connectInfo 并且进行数据封装
// let uniWifiInfo = new UniWifiInfo(null) let uniWifiInfo = new UniWifiInfo("","",false,0,0)
let uniWifiInfo : UniWifiInfo = {
SSID: "",
secure: false,
signalStrength: 0,
frequency: 0,
}
//做一些异步操作 //做一些异步操作
setTimeout(function () { setTimeout(function () {
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout // BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
// @ts-ignore // @ts-ignore
let winfo = this.mWifiManager!.getConnectionInfo(); let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null || zeroCountNum(winfo.bssid) > 4) { if(winfo != null && winfo.bssid != null && zeroCountNum(winfo.bssid) < 3){
Thread.sleep(1000) // 当前设备链接到了某个具体的wifi.封装成数据对象
winfo = this.mWifiManager!.getConnectionInfo();
}
// 封装成数据对象
uniWifiInfo = wrapUniWifiInfoFromConnectInfo(winfo) uniWifiInfo = wrapUniWifiInfoFromConnectInfo(winfo)
let res = new UniWifiResult(0,UniErrorSubject,'onWifiConnected:ok',uniWifiInfo) let res = new UniWifiResult(0,UniErrorSubject,'onWifiConnected:ok',uniWifiInfo)
// wifi状态可用了,分发当前的链接状态给已注册的监听集合 // wifi状态可用了,分发当前的链接状态给已注册的监听集合
...@@ -289,6 +280,9 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -289,6 +280,9 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) { for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) {
perCallback(connectedWithPartialInfo); perCallback(connectedWithPartialInfo);
}
} else {
// 开启了wifi 开关,但是尚未链接到某个具体的wifi
} }
}, 100); }, 100);
...@@ -352,6 +346,7 @@ var startWifiScaning = false ...@@ -352,6 +346,7 @@ var startWifiScaning = false
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export function startWifiImpl(option : WifiOption) { export function startWifiImpl(option : WifiOption) {
// 具备了权限,继续前进 // 具备了权限,继续前进
let wifiManager : WifiManager = let wifiManager : WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
...@@ -668,11 +663,8 @@ export function connectWifi(option : WifiConnectOption) { ...@@ -668,11 +663,8 @@ export function connectWifi(option : WifiConnectOption) {
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
UTSAndroid.getUniActivity()!.startActivity(manunalIntent); UTSAndroid.getUniActivity()!.startActivity(manunalIntent);
let result : UniWifiResult = {
errCode: 0, let result = new UniWifiResult(0,UniErrorSubject,"connectWifi:ok",null)
errMsg: "connectWifi:ok",
errSubject: "uni-connectWifi",
}
option.success?.(result) option.success?.(result)
option.complete?.(result) option.complete?.(result)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册