From 127fbd5fb24b32faddf5e5fce148a519e973887a Mon Sep 17 00:00:00 2001 From: duqingquan Date: Fri, 18 Nov 2022 17:44:27 +0800 Subject: [PATCH] android wifi connect ing --- pages/index/index.vue | 10 +- .../utssdk/app-android/index.uts | 2 +- .../utssdk/app-android/WifiConnector.uts | 151 ++++++++++++++++++ .../uni-wifi/utssdk/app-android/index.uts | 128 +++++++++++++-- 4 files changed, 278 insertions(+), 13 deletions(-) create mode 100644 uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts diff --git a/pages/index/index.vue b/pages/index/index.vue index 03c8c8a..2470b1b 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -35,7 +35,15 @@ console.log(res); }, testConnnectWifi(){ - uni.connectWifi({}); + + uni.connectWifi({ + partialInfo:false, + SSID:"Xiaomi_20D0", + password:"BBBB", + complete:(res)=>{ + console.log(res); + } + }); }, testGetConnnectWifi(){ uni.getConnectedWifi({ diff --git a/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts b/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts index ec5f6c9..4c6bfc5 100644 --- a/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts +++ b/uni_modules/uni-usercapturescreen/utssdk/app-android/index.uts @@ -45,7 +45,7 @@ class ScreenFileObserver extends FileObserver { constructor(screenFile: string) { - super(File(allScreen)) + super(new File(screenFile)) this.allScreen = new File(screenFile); } diff --git a/uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts b/uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts new file mode 100644 index 0000000..079c26e --- /dev/null +++ b/uni_modules/uni-wifi/utssdk/app-android/WifiConnector.uts @@ -0,0 +1,151 @@ +import WifiManager from "android.net.wifi.WifiManager"; +import WifiConfiguration from 'android.net.wifi.WifiConfiguration'; + + +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 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; +} + + // 查看以前是否也配置过这个网络 +function isExsits(SSID:string ,wifiManager:WifiManager):WifiConfiguration | null{ + let existingConfigs = wifiManager.getConfiguredNetworks(); + for (let existingConfig in existingConfigs) { + if (existingConfig.SSID.equals("\" + SSID + "\")) { + return existingConfig; + } + } + return null; +} + +function createWifiInfo(SSID:string ,password:string):WifiConfiguration { + + let config = new WifiConfiguration(); + config.allowedAuthAlgorithms.clear(); + config.allowedGroupCiphers.clear(); + config.allowedKeyManagement.clear(); + config.allowedPairwiseCiphers.clear(); + config.allowedProtocols.clear(); + config.SSID = "\"" + SSID + "\""; + + // // nopass + // // if (Type == WifiCipherType.WIFICIPHER_NOPASS) { + // // config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + // // } + // // // wep + // // if (Type == WifiCipherType.WIFICIPHER_WEP) { + // 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 (Type == WifiCipherType.WIFICIPHER_WPA) { + // 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; +} + + +class ConnectRunnable extends Runnable { + + ssid:string = "" + password:string = "" + + constructor(ssid:string,password:string) { + this.ssid = ssid + this.password = password + } + + override run():void{ + try { + // 打开wifi wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING + WifiConfiguration wifiConfig = createWifiInfo(ssid, password, + type); + + WifiConfiguration tempConfig = isExsits(ssid); + + if (tempConfig != null) { + wifiManager.removeNetwork(tempConfig.networkId); + } + + int netID = wifiManager.addNetwork(wifiConfig); + boolean enabled = wifiManager.enableNetwork(netID, true); + boolean connected = wifiManager.reconnect(); + } catch (Exception e) { + // TODO: handle exception + sendMsg(e.getMessage()); + e.printStackTrace(); + } + } +} + + +class WifiConnector { + + wifiManager:WifiManager; + + + //WIFICIPHER_WEP是WEP ,WIFICIPHER_WPA是WPA,WIFICIPHER_NOPASS没有密码 + // enum WifiCipherType { + // WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID + // } + + // 构造函数 + constructor(wifiManager:WifiManager) { + this.wifiManager = wifiManager; + } + + // 提供一个外部接口,传入要连接的无线网 + connect(ssid:string ,password:string) { + Thread thread = new Thread(new ConnectRunnable(ssid,password)); + thread.start(); + } + + + + + + + +} diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 9f96000..67ebd6c 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -12,6 +12,7 @@ import Gson from "com.google.gson.Gson"; 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'; /** * Wifi 函数通用入参封装 @@ -34,8 +35,26 @@ type WifiConnectOption = { success?: (res: object) => void; fail?: (res: object) => void; complete?: (res: object) => void; + + + + } + +function wrapWifiConfiguration(SSID:string ,password:string):WifiConfiguration { + + let config = new WifiConfiguration(); + + config.allowedAuthAlgorithms.clear(); + config.allowedGroupCiphers.clear(); + config.allowedKeyManagement.clear(); + config.allowedPairwiseCiphers.clear(); + config.allowedProtocols.clear(); + config.SSID = "\"" + SSID + "\""; + + return config; +} /** * Wifi信息统一数据结构 */ @@ -46,6 +65,9 @@ class UniWifiInfo { secure: boolean = false; signalStrength: Number = 0; frequency: Number = 0; + + /*下面的字段属于扩展字段*/ + securityType:string = "" constructor(scanResult?: ScanResult) { if (scanResult != null) { @@ -64,6 +86,9 @@ class UniWifiInfo { } else { this.secure = true; } + + /*扩展字段*/ + this.securityType = getSecurityType(scanResult); } } @@ -120,6 +145,18 @@ class Global { } +function getSecurityType(result:ScanResult):string { + if (result.capabilities.contains("WEP")) { + return "WEP"; + } else if (result.capabilities.contains("PSK")) { + return "WPA"; + } else if (result.capabilities.contains("EAP")) { + return "EAP"; + } + return "NONE"; +} + + /** * 自定义wifi变化广播监听器 */ @@ -208,14 +245,12 @@ class CustomBroadcastReceiver extends BroadcastReceiver { */ export function getWifiList(option: WifiOption) { - - if (Global.mReceiver == null) { // 还没调用startWifi 提示报错 var result = { errCode: 12000, errMsg: "getWifiList:fail:not invoke startWifi", - errSubject:"uni-wifi" + errSubject:"uni-getWifiList" } option.fail?.(result) option.complete?.(result) @@ -259,7 +294,6 @@ export function offWifiConnected(callback: UTSCallback) { * 注册Wifi列表的监听事件 */ export function onGetWifiList(callback: UTSCallback) { - Global.getWifiListCallbackList.push(callback) } /** @@ -272,15 +306,87 @@ export function offGetWifiList(callback: UTSCallback) { } } -export function connectWifi(_option: WifiConnectOption) { - // todo - console.log(_option) - if(_option.maunal == true){ - // 指定了手动模式 - let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); - getUniActivity()!!.startActivity(manunalIntent); + +/** + * 链接指定wifi + */ +export function connectWifi(option: WifiConnectOption) { + + + + var result = { + errCode: 12000, + errMsg: "connectWifi:fail:not invoke startWifi", + errSubject:"uni-connectWifi" } + if (Global.mReceiver == null || Global.scanList.length < 1) { + // 还没调用startWifi 提示报错 + option.fail?.(result) + option.complete?.(result) + + return + } + + // if(option.maunal == true){ + // // 指定了手动模式 + // let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); + // getUniActivity()!!.startActivity(manunalIntent); + + // result.errCode = 0 + // result.errMsg = "connectWifi:ok" + + // option.success + + // ?.(result) + // option.complete?.(result) + // return + // } + + // 执行后续的逻辑 + let scanWifiInfo:UniWifiInfo|null = null + for (let scanResult in Global.scanList) { + + if (scanResult.SSID.equals(option.SSID)) { + scanWifiInfo = scanResult + } + } + + if(scanWifiInfo == null){ + // 不在扫描列表中返回错误 + option.fail?.(result) + option.complete?.(result) + + return + } + + console.log(scanWifiInfo); + + let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password); + + let wifiManager: WifiManager = + getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager + + let targetExistConfig:WifiConfiguration|null = null + let existingConfigs = wifiManager.getConfiguredNetworks(); + for (let existingConfig in existingConfigs) { + if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) { + targetExistConfig = existingConfig + } + } + + if (targetExistConfig != null) { + wifiManager.removeNetwork(targetExistConfig.networkId); + } + + let netID = wifiManager.addNetwork(wifiConfigration); + let enabled = wifiManager.enableNetwork(netID, true); + let connected = wifiManager.reconnect(); + + console.log(connected); + + + } /** -- GitLab