From 409284d3e9641db9a0892038389247b9584d5436 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Tue, 22 Nov 2022 12:52:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0=20connect?= =?UTF-8?q?=20wifi=20=E5=87=BD=E6=95=B0=E5=8E=9F=E5=9E=8B=EF=BC=8C?= =?UTF-8?q?=E7=AD=89=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.vue | 2 +- .../uni-wifi/utssdk/app-android/index.uts | 119 ++++++++++++++---- 2 files changed, 97 insertions(+), 24 deletions(-) diff --git a/pages/index/index.vue b/pages/index/index.vue index 2470b1b..c848180 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -37,7 +37,7 @@ testConnnectWifi(){ uni.connectWifi({ - partialInfo:false, + maunal:false, SSID:"Xiaomi_20D0", password:"BBBB", 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 67ebd6c..8bb023d 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -13,6 +13,9 @@ import JSONObject from "com.alibaba.fastjson.JSONObject"; import Intent from "android.content.Intent"; import Thread from "java.lang.Thread"; import WifiConfiguration from 'android.net.wifi.WifiConfiguration'; +import AuthAlgorithm from 'android.net.wifi.WifiConfiguration.AuthAlgorithm'; +import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt'; +import TextUtils from 'android.text.TextUtils'; /** * Wifi 函数通用入参封装 @@ -37,12 +40,36 @@ type WifiConnectOption = { complete?: (res: object) => void; - - } -function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration { +function isHex(key:string):boolean { + for (var i = key.length - 1; i >= 0; i--) { + let c = key.charAt(i); + if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' + && c <= 'f')) { + return false; + } + } + + return true; +} + +/** + * 判断是否是wep格式的key + */ +function isHexWepKey(wepKey:string):boolean { + let len = wepKey.length; + + // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?) + if (len != 10 && len != 26 && len != 58) { + return false; + } + + return isHex(wepKey); +} + +function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration { let config = new WifiConfiguration(); @@ -53,6 +80,43 @@ function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration { config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\""; + + // nopass + if ("NONE".equals(passwordType)) { + config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + } + // // wep + if ("WEP".equals(passwordType)) { + if (!TextUtils.isEmpty(password)) { + if (isHexWepKey(password)) { + config.wepKeys[0] = password; + } else { + config.wepKeys[0] = "\"" + password + "\""; + } + } + config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); + config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); + config.allowedKeyManagement.set(KeyMgmt.NONE); + config.wepTxKeyIndex = 0; + } + // wpa + if ("WPA".equals(passwordType)) { + config.preSharedKey = "\"" + password + "\""; + config.hiddenSSID = true; + config.allowedAuthAlgorithms + .set(WifiConfiguration.AuthAlgorithm.OPEN); + config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); + config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + config.allowedPairwiseCiphers + .set(WifiConfiguration.PairwiseCipher.TKIP); + // 此处需要修改否则不能自动重联 + config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); + config.allowedPairwiseCiphers + .set(WifiConfiguration.PairwiseCipher.CCMP); + config.status = WifiConfiguration.Status.ENABLED; + } + return config; } /** @@ -312,8 +376,7 @@ export function offGetWifiList(callback: UTSCallback) { */ export function connectWifi(option: WifiConnectOption) { - - + var result = { errCode: 12000, errMsg: "connectWifi:fail:not invoke startWifi", @@ -328,20 +391,18 @@ export function connectWifi(option: WifiConnectOption) { return } - // if(option.maunal == true){ - // // 指定了手动模式 - // let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); - // getUniActivity()!!.startActivity(manunalIntent); + if(option.maunal == true){ + // 指定了手动模式 + let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); + getUniActivity()!!.startActivity(manunalIntent); - // result.errCode = 0 - // result.errMsg = "connectWifi:ok" + result.errCode = 0 + result.errMsg = "connectWifi:ok" - // option.success - - // ?.(result) - // option.complete?.(result) - // return - // } + option.success?.(result) + option.complete?.(result) + return + } // 执行后续的逻辑 let scanWifiInfo:UniWifiInfo|null = null @@ -360,13 +421,14 @@ export function connectWifi(option: WifiConnectOption) { return } - console.log(scanWifiInfo); + console.log(JSON.stringify(scanWifiInfo.securityType)); - let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password); + let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType); let wifiManager: WifiManager = getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager + // 如果已经存在了指定wifi 配置,移除之 let targetExistConfig:WifiConfiguration|null = null let existingConfigs = wifiManager.getConfiguredNetworks(); for (let existingConfig in existingConfigs) { @@ -379,12 +441,23 @@ export function connectWifi(option: WifiConnectOption) { wifiManager.removeNetwork(targetExistConfig.networkId); } - let netID = wifiManager.addNetwork(wifiConfigration); - let enabled = wifiManager.enableNetwork(netID, true); - let connected = wifiManager.reconnect(); + try { + let netID = wifiManager.addNetwork(wifiConfigration); + let enabled = wifiManager.enableNetwork(netID, true); + let connected = wifiManager.reconnect(); + console.log(connected); + } catch (e) { + // TODO: handle exception + console.log(e); + // e.printStackTrace(); + + } - console.log(connected); + result.errCode = 0 + result.errMsg = "connectWifi:ok" + option.success?.(result) + option.complete?.(result) } -- GitLab