From 4ce655e29344be16df512c86cc02ca0e25faf7eb Mon Sep 17 00:00:00 2001 From: duqingquan Date: Fri, 10 Nov 2023 16:15:02 +0800 Subject: [PATCH] =?UTF-8?q?wifi=20=E9=80=82=E9=85=8D=20uni=20error.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-wifi/utssdk/app-android/index.uts | 355 ++++++++--------- uni_modules/uni-wifi/utssdk/index.d.ts | 377 ++++++++++++++++++ uni_modules/uni-wifi/utssdk/interface.uts | 75 ++-- uni_modules/uni-wifi/utssdk/unierror.uts | 59 +++ 4 files changed, 647 insertions(+), 219 deletions(-) create mode 100644 uni_modules/uni-wifi/utssdk/index.d.ts create mode 100644 uni_modules/uni-wifi/utssdk/unierror.uts diff --git a/uni_modules/uni-wifi/utssdk/app-android/index.uts b/uni_modules/uni-wifi/utssdk/app-android/index.uts index 40312b4..e3ff3ed 100644 --- a/uni_modules/uni-wifi/utssdk/app-android/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-android/index.uts @@ -1,5 +1,4 @@ import Context from "android.content.Context"; -import { UTSAndroid } from "io.dcloud.uts"; import WifiManager from "android.net.wifi.WifiManager"; import WifiInfo from "android.net.wifi.WifiInfo"; import Manifest from "android.Manifest"; @@ -8,9 +7,7 @@ import ScanResult from "android.net.wifi.ScanResult"; import BroadcastReceiver from "android.content.BroadcastReceiver"; import ActivityCompat from "androidx.core.app.ActivityCompat"; import IntentFilter from "android.content.IntentFilter"; -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'; @@ -18,8 +15,8 @@ import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt'; import TextUtils from 'android.text.TextUtils'; import Build from 'android.os.Build'; -import { UniWifiResult, GetConnectedWifiOptions, WifiConnectOption, WifiOption, UniWifiInfo } from "../interface.uts" - +import { UniWifiResult, UniWifiInfoWithPartialInfo,GetConnectedWifiOptions, WifiConnectOption, WifiOption, UniWifiInfo,UniWifiCallback ,UniWifiResultCallback,UniWifiResultCallbackWithPartialInfo,UniGetWifiListCallback} from "../interface.uts" +import { WifiFailImpl, getErrcode,UniErrorSubject } from '../unierror'; /** @@ -32,12 +29,12 @@ class Global { // 扫描wifi结果 static scanList : AndroidUniWifiInfo[] = [] // 获取wifi列表监听 - static onGetWifiListCallback : UTSCallback | null = null - static supendGetWifiSuccess : ((res: UniWifiResult) => void) | null = null - static supendGetWifiComplete : ((res: UniWifiResult) => void) | null = null + static onGetWifiListCallback : UniGetWifiListCallback|null = null + static supendGetWifiSuccess : UniWifiResultCallback|null = null + static supendGetWifiComplete :UniWifiResultCallback|null = null // wifi链接监听 - static onWifiConnectCallbackList : UTSCallback[] = [] - static onWifiConnectWithPartialInfoCallbackList : UTSCallback[] = [] + static onWifiConnectCallbackList : UniWifiResultCallback[] = [] + static onWifiConnectWithPartialInfoCallbackList : UniWifiResultCallbackWithPartialInfo[] = [] } @@ -280,19 +277,16 @@ class CustomBroadcastReceiver extends BroadcastReceiver { // 封装成数据对象 uniWifiInfo = wrapUniWifiInfoFromConnectInfo(winfo) - let res = { - errMsg: 'onWifiConnected:ok', - errCode: 0, - wifi: uniWifiInfo - } + + let res = new UniWifiResult(0,UniErrorSubject,'onWifiConnected:ok',uniWifiInfo) + // wifi状态可用了,分发当前的链接状态给已注册的监听集合 for (let perCallback in Global.onWifiConnectCallbackList) { perCallback(res); } // 封装仅SSID 数据对象 - var connectedWithPartialInfo = { - SSID: uniWifiInfo.SSID - } + let connectedWithPartialInfo = new UniWifiInfoWithPartialInfo(uniWifiInfo.SSID) + for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) { perCallback(connectedWithPartialInfo); } @@ -305,14 +299,9 @@ class CustomBroadcastReceiver extends BroadcastReceiver { if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) { startWifiScaning = false; - - - - // wifi 扫描结果回调 let results = this.mWifiManager!.scanResults; - if (results != null) { Global.scanList = [] for (let scanResult in results) { @@ -324,7 +313,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver { // 挨个通知,所有的监听器 if(Global.onGetWifiListCallback != null){ - const data = new JSONObject(); + const data = new UTSJSONObject(); data["wifiList"] = Global.scanList Global.onGetWifiListCallback?.(data); /** @@ -335,11 +324,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver { } - let ret : UniWifiResult = { - errCode: 0, - errSubject: "uni-getWifiList", - errMsg: "getWifiList:ok" - } + let ret = new UniWifiResult(0,UniErrorSubject,"getWifiList:ok",null) if(Global.supendGetWifiSuccess != null){ Global.supendGetWifiSuccess?.(ret) Global.supendGetWifiSuccess = null @@ -365,89 +350,84 @@ var startWifiScaning = false * 开启wifi */ @Suppress("DEPRECATION") -export function startWifi(option : WifiOption) { - - // 需要先开启wifi,才能使用后续的功能 - let requestCode = 1001; - let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION"); - - let result : UniWifiResult = { - errCode: 12001, - errMsg: "startWifi:premission loss", - errSubject: "uni-startWifi" - } - - // 检查权限 - if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { - - ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, permissionWifi, requestCode.toInt()) - // 尚不具备权限,返回错误 - let err = new UniError("uni-startWifi",12001,"startWifi:premission loss"); - option.fail?.(err) - option.complete?.(err) - - return; - } - - // 具备了权限,继续前进 - let wifiManager : WifiManager = - UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager - // 用户没有开启wifi 总开关 - if (!wifiManager.isWifiEnabled()) { - // wifi 没开启 - let err = new UniError("uni-startWifi",12005,"wifi not turned on"); - option.fail?.(err); - option.complete?.(err); - return; - } - // 初始化wifi 状态广播监听,后续所有的api,均基于此 - if(Global.mReceiver != null){ - // 说明已经注册过了 - result.errCode = 0 - result.errMsg = "startWifi:ok" - - option.success?.(result) - option.complete?.(result) - return - } - - - Global.mReceiver = new CustomBroadcastReceiver(wifiManager) - - let filter = new IntentFilter() - filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) - // @ts-ignore - filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) - // @ts-ignore - filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); - - UTSAndroid.getUniActivity()!.registerReceiver(Global.mReceiver, filter) - - /** - * activity 被销毁时,取消注册 - */ - UTSAndroid.onAppActivityDestroy(function () { - - if (Global.mReceiver != null) { - UTSAndroid.getUniActivity()!.unregisterReceiver(Global.mReceiver) - Global.mReceiver = null - - Global.scanList = [] - Global.onGetWifiListCallback = null - Global.onWifiConnectCallbackList = [] - Global.onWifiConnectWithPartialInfoCallbackList = [] - } +export function startWifiImpl(option : WifiOption) { + + // 具备了权限,继续前进 + let wifiManager : WifiManager = + UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager + // 用户没有开启wifi 总开关 + if (!wifiManager.isWifiEnabled()) { + // wifi 没开启 + let failResult = new WifiFailImpl(getErrcode(12005)); + option.fail?.(failResult); + option.complete?.(failResult); + return; + } + // 初始化wifi 状态广播监听,后续所有的api,均基于此 + if(Global.mReceiver != null){ + // 说明已经注册过了 + let result = UniWifiResult(0,UniErrorSubject,"startWifi:ok",null) + + option.success?.(result) + option.complete?.(result) + return + } + + + Global.mReceiver = new CustomBroadcastReceiver(wifiManager) + + let filter = new IntentFilter() + filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) + // @ts-ignore + filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) + // @ts-ignore + filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); + + UTSAndroid.getUniActivity()!.registerReceiver(Global.mReceiver, filter) + + /** + * activity 被销毁时,取消注册 + */ + UTSAndroid.onAppActivityDestroy(function () { + + if (Global.mReceiver != null) { + UTSAndroid.getUniActivity()!.unregisterReceiver(Global.mReceiver) + Global.mReceiver = null + + Global.scanList = [] + Global.onGetWifiListCallback = null + Global.onWifiConnectCallbackList = [] + Global.onWifiConnectWithPartialInfoCallbackList = [] + } + + }); + startWifiScaning = true + // 开始扫描 + wifiManager.startScan() + + let result = UniWifiResult(0,UniErrorSubject,"startWifi:ok",null) + option.success?.(result) + option.complete?.(result) - }); - startWifiScaning = true - // 开始扫描 - wifiManager.startScan() +} - result.errCode = 0 - result.errMsg = "startWifi:ok" +@Suppress("DEPRECATION") +export function startWifi(option : WifiOption) { - option.success?.(result) - option.complete?.(result) + /** + * 准备权限 + */ + let permissionNeed = ["android.permission.ACCESS_FINE_LOCATION"]; + UTSAndroid.requestSystemPermission(UTSAndroid.getUniActivity()!, permissionNeed, function (allRight:boolean,_grantedList:string[]) { + if (allRight) { + // 交给目前的location 引擎,真实执行 + startWifiImpl(option) + } + }, function (_doNotAskAgain:boolean,_grantedList:string[]) { + let err = new WifiFailImpl(getErrcode(12001)); + option.fail?.(err) + option.complete?.(err) + }) } @@ -460,8 +440,7 @@ export function getWifiList(option : WifiOption) { if (Global.mReceiver == null) { // 还没调用startWifi 提示报错 - let err = new UniError("uni-getWifiList",12000,"getWifiList:fail:not invoke startWifi"); - + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) @@ -471,13 +450,14 @@ export function getWifiList(option : WifiOption) { let wifiManager : WifiManager = UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager + if(option.success != null){ + Global.supendGetWifiSuccess = option.success + } + if(option.complete != null){ + Global.supendGetWifiComplete = option.complete + } - - Global.supendGetWifiSuccess = option.success - Global.supendGetWifiComplete = option.complete - wifiManager.startScan() - } @@ -486,12 +466,12 @@ export function getWifiList(option : WifiOption) { /** * wifi 链接成功的回调注册 */ -export function onWifiConnected(callback : UTSCallback) { +export function onWifiConnected(callback : UniWifiResultCallback) { Global.onWifiConnectCallbackList.push(callback) } -export function onWifiConnectedWithPartialInfo(callback : UTSCallback) { +export function onWifiConnectedWithPartialInfo(callback : UniWifiResultCallbackWithPartialInfo) { Global.onWifiConnectWithPartialInfoCallbackList.push(callback) } @@ -499,13 +479,13 @@ export function onWifiConnectedWithPartialInfo(callback : UTSCallback) { /** * wifi 链接成功的回调取消注册 */ -export function offWifiConnected(callback? : UTSCallback) { - +export function offWifiConnected(callback? : UniWifiResultCallback) { + if(callback == null){ Global.onWifiConnectCallbackList = [] return } - + let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback) if (callbackIndex >= 0) { Global.onWifiConnectCallbackList.splice(callbackIndex, 1); @@ -515,13 +495,13 @@ export function offWifiConnected(callback? : UTSCallback) { /** * 不具备详细信息的wifi 反注册 */ -export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { - +export function offWifiConnectedWithPartialInfo(callback? : UniWifiResultCallbackWithPartialInfo) { + if(callback == null){ Global.onWifiConnectWithPartialInfoCallbackList = [] return } - + let callbackIndex = Global.onWifiConnectWithPartialInfoCallbackList.indexOf(callback) if (callbackIndex >= 0) { Global.onWifiConnectWithPartialInfoCallbackList.splice(callbackIndex, 1); @@ -531,14 +511,14 @@ export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { /** * 注册Wifi列表的监听事件 */ -export function onGetWifiList(callback : UTSCallback) { +export function onGetWifiList(callback : UniGetWifiListCallback) { Global.onGetWifiListCallback = callback } /** * 取消注册Wifi列表的监听事件 */ -export function offGetWifiList(callback? : UTSCallback) { +export function offGetWifiList(callback? : UniWifiCallback) { Global.onGetWifiListCallback = null Global.supendGetWifiComplete = null Global.supendGetWifiSuccess = null @@ -549,16 +529,16 @@ export function offGetWifiList(callback? : UTSCallback) { * 真正执行wifi链接逻辑 */ function realWifiConnect(option : WifiConnectOption){ - + if (Global.mReceiver == null || Global.scanList.length < 1) { - - let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi"); + + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) - + return } - + // 执行后续的逻辑 let scanWifiInfo : AndroidUniWifiInfo | null = null for (let scanResult in Global.scanList) { @@ -566,20 +546,20 @@ function realWifiConnect(option : WifiConnectOption){ scanWifiInfo = scanResult } } - + if (scanWifiInfo == null) { // 不在扫描列表中返回错误 - let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi"); + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) return } - + let wifiConfigration = wrapWifiConfiguration(scanWifiInfo.SSID, option.password, scanWifiInfo.securityType); wifiConfigration.BSSID = scanWifiInfo.BSSID let wifiManager : WifiManager = UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager - + // 如果已经存在了指定wifi 配置,移除之 let targetExistConfig : WifiConfiguration | null = null let existingConfigs = wifiManager.getConfiguredNetworks(); @@ -591,28 +571,28 @@ function realWifiConnect(option : WifiConnectOption){ // 如果wifi已经保存了当前ssid的配置,可能是别的应用添加的。android系统要求,需要删除掉重新添加 if (targetExistConfig != null) { let removeRet = wifiManager.removeNetwork(targetExistConfig.networkId); - + if (!removeRet) { - + // add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持 if(Build.VERSION.SDK_INT > 28){ // 系统大于android 9 - let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support"); + let err = new WifiFailImpl(getErrcode(12001)); option.fail?.(err) option.complete?.(err) }else{ // 移除之前的配置失败了,返回错误,需要用户手动取消保存一下 - let err = new UniError("uni-connectWifi",12013,"connectWifi:wifi config may be expired"); + let err = new WifiFailImpl(getErrcode(12013)); option.fail?.(err) option.complete?.(err) } - - + + return } - + } - + let currentConnect = wifiManager.getConnectionInfo() if (currentConnect.networkId >= 0) { wifiManager.disableNetwork(currentConnect.networkId) @@ -620,55 +600,47 @@ function realWifiConnect(option : WifiConnectOption){ wifiManager.removeNetwork(currentConnect.networkId) } wifiManager.disconnect() - + let connected = false; try { - + let netID = wifiManager.addNetwork(wifiConfigration); // 如果-1 说明没添加上,报错即可 if (netID < 0) { - - let err = new UniError("uni-connectWifi",12002,"connectWifi:password error Wi-Fi"); - + let err = new WifiFailImpl(getErrcode(12002)); option.fail?.(err) option.complete?.(err) - + return } - - + + let enabled = wifiManager.enableNetwork(netID, true); if (!enabled) { - - let err = new UniError("uni-connectWifi",12007,"connectWifi:user denied"); - + let err = new WifiFailImpl(getErrcode(12007)); option.fail?.(err) option.complete?.(err) - + return } connected = wifiManager.reconnect(); - + } catch (e) { connected = false; console.log(e); } - + if (!connected) { // 出错了,返回错误 // 兜底的报错 - let err = new UniError("uni-connectWifi",12010,"connectWifi:fail:unknown error"); + let err = new WifiFailImpl(getErrcode(12010)); option.fail?.(err) option.complete?.(err) return } - - let result : UniWifiResult = { - errCode: 0, - errMsg: "connectWifi:ok", - errSubject: "uni-connectWifi", - } - + + let result = new UniWifiResult(0,UniErrorSubject,"getWifiList:ok",null) + wifiManager.saveConfiguration() //scanWifiInfo 根据 partialInfo 填充给返回字段 if (option.partialInfo != null && option.partialInfo == true) { @@ -679,8 +651,8 @@ function realWifiConnect(option : WifiConnectOption){ } else { result.wifi = wrapUniWifiInfoFromAndroid(scanWifiInfo) } - - + + option.success?.(result) option.complete?.(result) } @@ -707,14 +679,14 @@ export function connectWifi(option : WifiConnectOption) { return } - - + + // add since 2022-03-28 ,增加逻辑,如果正在扫描中,则可以等待5s if(startWifiScaning){ let taskCount = 0 let taskId:number = 0 taskId = setInterval(function(){ - + taskCount += 1; if(taskCount >= 5 || startWifiScaning == false){ // 超过10s了。或者扫描过程结束了 @@ -722,7 +694,7 @@ export function connectWifi(option : WifiConnectOption) { realWifiConnect(option) } },2000) - + UTSAndroid.onAppActivityDestroy(function () { clearInterval(taskId) }); @@ -730,7 +702,7 @@ export function connectWifi(option : WifiConnectOption) { realWifiConnect(option) } - + } @@ -741,7 +713,7 @@ export function connectWifi(option : WifiConnectOption) { export function stopWifi(option : WifiOption) { // 需要先开启wifi,才能使用后续的功能 if (Global.mReceiver == null) { - let err = new UniError("uni-stopWifi",12000,"stopWifi:not init"); + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) @@ -753,20 +725,15 @@ export function stopWifi(option : WifiOption) { // 多次调用 //TODO handle the exception } - + Global.onGetWifiListCallback = null Global.onWifiConnectWithPartialInfoCallbackList = [] Global.onWifiConnectCallbackList = [] Global.mReceiver = null - - let result : UniWifiResult = { - errCode: 0, - errSubject: "uni-stopWifi", - errMsg: "stopWifi:ok" - } - option.success?.(result) - option.complete?.(result) + let ret = new UniWifiResult(0,UniErrorSubject,"stopWifi:ok",null) + option.success?.(ret) + option.complete?.(ret) } @@ -780,10 +747,10 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { SSID: "" } - + if (Global.mReceiver == null) { // 还没调用startWifi 提示报错 - let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) @@ -793,14 +760,14 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { // 需要先校验权限,没有位置权限无法获取wifi if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 尚不具备权限,返回错误 - let err = new UniError("uni-getConnectedWifi",12001,"getConnectedWifi:permission loss"); + let err = new WifiFailImpl(getErrcode(12001)); option.fail?.(err) option.complete?.(err) return; } - - + + const context = UTSAndroid.getAppContext(); if (context != null) { const wm = context.getSystemService( @@ -811,12 +778,8 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { const winfo = wm.getConnectionInfo(); wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo); - - let res : UniWifiResult = { - errCode: 0, - errMsg: "getConnectedWifi:ok", - errSubject: "uni-getConnectedWifi", - } + + let res = new UniWifiResult(0,UniErrorSubject,"getConnectedWifi:ok",null) // 判断一下是否wifi 关闭了 if (option.partialInfo!= null) { @@ -826,7 +789,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { res.wifi = ret; } else { if (wifiInfo.BSSID == null || zeroCountNum(wifiInfo.BSSID) > 3) { - let err = new UniError("uni-getConnectedWifi",12005,"getConnectedWifi:fail:wifi is disable"); + let err = new WifiFailImpl(getErrcode(12005)); option.fail?.(err) option.complete?.(err) return @@ -839,8 +802,8 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { option.complete?.(res) return } - - let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); + + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) } diff --git a/uni_modules/uni-wifi/utssdk/index.d.ts b/uni_modules/uni-wifi/utssdk/index.d.ts new file mode 100644 index 0000000..da8fd21 --- /dev/null +++ b/uni_modules/uni-wifi/utssdk/index.d.ts @@ -0,0 +1,377 @@ +declare namespace UniNamespace { + + type UniWifiComplete = any + type WifiSuccessCallback = (res : UniWifiResult) => void + type WifiFailCallback = (err : UniWifiFail) => void + type WifiCompleteCallback = (res : UniWifiComplete) => void + type UniWifiCallback = () => void + + type WifiErrorCode = 1300002 + + interface UniWifiInfo { + SSID : string; + BSSID ?: string; + secure ?: boolean; + signalStrength ?: number; + frequency ?: number; + } + + interface UniWifiResult { + errCode : number, + errSubject : string, + errMsg : string, + wifi : UniWifiInfo | null + } + + interface UniWifiFail { + errCode : WifiErrorCode + } + + interface WifiConnectOption{ + SSID : string | null; + BSSID : string | null; + password : string | null; + maunal : boolean | null; + partialInfo : boolean | null; //ios不生效 + success ?: WifiSuccessCallback | null; + fail ?: WifiFailCallback | null; + complete ?: WifiCompleteCallback | null; + } + + interface GetConnectedWifiOptions { + partialInfo : boolean | null; + success ?: WifiSuccessCallback | null; + fail ?: WifiFailCallback | null; + complete ?: WifiCompleteCallback | null; + } + + + interface WifiOption { + success ?: WifiSuccessCallback | null, + fail ?: WifiFailCallback | null, + complete ?: InstallApkCompleteCallback | null + } + +} + +declare interface Uni { + + /** + * 初始化Wi-Fi模块 + * + * @param {WifiOption} option + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#startwifi + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { after: 'stopWifi' } + */ + startWifi(option : UniNamespace.WifiOption): void, + + /** + * 关闭 Wi-Fi 模块 + * + * @param {WifiOption} option + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#stopwifi + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { before: 'startWifi' } + */ + stopWifi(option : UniNamespace.WifiOption) : void, + /** + * @param {WifiConnectOption} option + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#connectWifi + * @uniPlatform { + * "app": { + * "android": { + * "osVer": ">=4.4 && <10.0", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { + generated: false, + pollution: false, + cases:[ + { + before: 'startWifi', + after: 'stopWifi', + input: [{ + maunal:false, + SSID:"Xiaomi_20D0", + password:"streamApp!2016", + }], + output:{ + callbackType: 'success', + value: { errCode: 12013 ,errMsg: "connectWifi:wifi config may be expired",errSubject: "uni-connectWifi"} + } + } + ] + } + */ + connectWifi(option : UniNamespace.WifiConnectOption) : void, + /** + * 请求获取 Wi-Fi 列表。wifiList 数据会在 onGetWifiList 注册的回调中返回。 + * @param {WifiOption} option + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#getWifiList + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { before: 'startWifi', after: 'stopWifi' } + */ + getWifiList(option : UniNamespace.WifiOption) : void, + /** + * 监听获取到 Wi-Fi 列表数据事件。 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onGetWifiList + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + * @autotest { + generated: false, + pollution: false, + expectCallback: true, + before: 'startWifi', + after: 'onGetWifiListAfter', + cases: [ + { + output: { + value: 0, + returnKey: '.wifiList.length', + jestExpectSyntax: 'toBeGreaterThan' + }, + } + ] + } + */ + onGetWifiList(callback : UniNamespace.UniWifiCallback) : void, + /** + * 移除获取到 Wi-Fi 列表数据事件的监听函数。 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offGetWifiList + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + */ + offGetWifiList(callback : UniNamespace.UniWifiCallback) : void, + /** + * 获取已连接的 Wi-Fi 信息 + * + * @param {GetConnectedWifiOptions} option + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#getConnectedWifi + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { before: 'startWifi', after: 'stopWifi' } + */ + getConnectedWifi(option : UniNamespace.GetConnectedWifiOptions) : void, + /** + * 监听连接上 Wi-Fi 的事件 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onWifiConnected + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + */ + onWifiConnected(callback : UniNamespace.UniWifiCallback) : void, + /** + * 监听连接上 Wi-Fi 的事件。 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onWifiConnectedWithPartialInfo + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + */ + onWifiConnectedWithPartialInfo(callback : UniNamespace.UniWifiCallback) : void, + /** + * 移除连接上 Wi-Fi 的事件的监听函数。 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offWifiConnected + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "4.4.4", + * "uniVer": "3.7.0", + * "unixVer": "3.9.0" + * }, + * "ios": { + * "osVer": "9.0", + * "uniVer": "3.7.7", + * "unixVer": "3.9.0" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + */ + offWifiConnected(callback ?: UniNamespace.UniWifiCallback) : void, + + /** + * 移除连接上 Wi-Fi 的事件的监听函数。 + * + * @param {UniWifiCallback} callback + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onOffWifiConnectedWithPartialInfo + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "x", + * "uniVer": "x", + * "unixVer": "x" + * }, + * "ios": { + * "osVer": "x", + * "uniVer": "x", + * "unixVer": "x" + * } + * } + * } + * @uniVersion 3.7.7 + * @uniVueVersion 2,3 //支持的vue版本 + * @autotest { expectCallback: true } + */ + onOffWifiConnectedWithPartialInfo(callback ?: UniNamespace.UniWifiCallback) : void, + /** + * SetWifiList 暂未实现 + * + * @uniPlatform { + * "app": { + * "android": { + * "osVer": "x", + * "uniVer": "x", + * "unixVer": "x" + * }, + * "ios": { + * "osVer": "x", + * "uniVer": "x", + * "unixVer": "x" + * } + * } + * } + */ + setWifiList(option : UniNamespace.WifiOption) : void, + + +} diff --git a/uni_modules/uni-wifi/utssdk/interface.uts b/uni_modules/uni-wifi/utssdk/interface.uts index 14cdbf0..3d1a9a4 100644 --- a/uni_modules/uni-wifi/utssdk/interface.uts +++ b/uni_modules/uni-wifi/utssdk/interface.uts @@ -8,7 +8,7 @@ export type WifiOption = { }; /** - * Wifi 链接参数封装 + * Wifi 链接参数封装 */ export type WifiConnectOption = { SSID ?: string; @@ -31,7 +31,7 @@ export type GetConnectedWifiOptions = { complete ?: (res : any) => void } -/* +/* * 对外暴露的wifi信息 */ export type UniWifiInfo = { @@ -42,6 +42,10 @@ export type UniWifiInfo = { frequency ?: number; } +export type UniWifiInfoWithPartialInfo = { + SSID : string; +} + export type UniWifiResult = { errCode : number, @@ -52,6 +56,12 @@ export type UniWifiResult = { export type UniWifiCallback = () => void +export type UniGetWifiListCallback = (wifiInfo:UTSJSONObject) => void + +export type UniWifiResultCallback = (wifiInfo:UniWifiResult) => void + +export type UniWifiResultCallbackWithPartialInfo = (wifiInfo:UniWifiInfoWithPartialInfo) => void + export type StartWifi = (option : WifiOption) => void @@ -77,11 +87,30 @@ export type OnOffWifiConnectedWithPartialInfo = (callback : UniWifiCallback | nu export type SetWifiList = (option : WifiOption) => void + +/** + * 错误码 + * - 12000 尚未初始化 + * - 12001 当前系统不支持相关能力 + * - 12002 密码错误 + * - 12005 Android 特有,未打开 Wi-Fi 开关 + * - 12007 用户拒绝授权链接 Wi-Fi + * - 12010 系统其他错误 + * - 12013 系统保存的 Wi-Fi 配置过期,建议忘记 Wi-Fi 后重试,仅 Android 支持 + */ +export type WifiErrorCode = 12000 |12001 | 12002 | 12005 | 12007 | 12010 | 12013; + +export interface WifiFail extends IUniError{ + errCode: WifiErrorCode +}; + + + interface Uni { /** * 初始化Wi-Fi模块 - * - * @param {WifiOption} option + * + * @param {WifiOption} option * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#startwifi * @uniPlatform { * "app": { @@ -104,8 +133,8 @@ interface Uni { startWifi(option : WifiOption): void, /** * 关闭 Wi-Fi 模块 - * - * @param {WifiOption} option + * + * @param {WifiOption} option * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#stopwifi * @uniPlatform { * "app": { @@ -127,7 +156,7 @@ interface Uni { */ stopWifi(option : WifiOption) : void, /** - * @param {WifiConnectOption} option + * @param {WifiConnectOption} option * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#connectWifi * @uniPlatform { * "app": { @@ -191,8 +220,8 @@ interface Uni { getWifiList(option : WifiOption) : void, /** * 监听获取到 Wi-Fi 列表数据事件。 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onGetWifiList * @uniPlatform { * "app": { @@ -231,8 +260,8 @@ interface Uni { onGetWifiList(callback : UniWifiCallback) : void, /** * 移除获取到 Wi-Fi 列表数据事件的监听函数。 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offGetWifiList * @uniPlatform { * "app": { @@ -255,8 +284,8 @@ interface Uni { offGetWifiList(callback : UniWifiCallback) : void, /** * 获取已连接的 Wi-Fi 信息 - * - * @param {GetConnectedWifiOptions} option + * + * @param {GetConnectedWifiOptions} option * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#getConnectedWifi * @uniPlatform { * "app": { @@ -279,8 +308,8 @@ interface Uni { getConnectedWifi(option : GetConnectedWifiOptions) : void, /** * 监听连接上 Wi-Fi 的事件 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onWifiConnected * @uniPlatform { * "app": { @@ -303,8 +332,8 @@ interface Uni { onWifiConnected(callback : UniWifiCallback) : void, /** * 监听连接上 Wi-Fi 的事件。 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onWifiConnectedWithPartialInfo * @uniPlatform { * "app": { @@ -327,8 +356,8 @@ interface Uni { onWifiConnectedWithPartialInfo(callback : UniWifiCallback) : void, /** * 移除连接上 Wi-Fi 的事件的监听函数。 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offWifiConnected * @uniPlatform { * "app": { @@ -351,8 +380,8 @@ interface Uni { offWifiConnected(callback : UniWifiCallback | null) : void, /** * 移除连接上 Wi-Fi 的事件的监听函数。 - * - * @param {UniWifiCallback} callback + * + * @param {UniWifiCallback} callback * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onOffWifiConnectedWithPartialInfo * @uniPlatform { * "app": { @@ -375,7 +404,7 @@ interface Uni { onOffWifiConnectedWithPartialInfo(callback : UniWifiCallback | null) : void, /** * SetWifiList 暂未实现 - * + * * @uniPlatform { * "app": { * "android": { @@ -392,4 +421,4 @@ interface Uni { * } */ setWifiList(option : WifiOption) : void, -} \ No newline at end of file +} diff --git a/uni_modules/uni-wifi/utssdk/unierror.uts b/uni_modules/uni-wifi/utssdk/unierror.uts new file mode 100644 index 0000000..023c2de --- /dev/null +++ b/uni_modules/uni-wifi/utssdk/unierror.uts @@ -0,0 +1,59 @@ +import { WifiErrorCode, WifiFail} from "./interface.uts" + +/** + * 错误主题 + */ +export const UniErrorSubject = 'uni-wifi'; +/** + * 错误码 + * @UniError + */ +export const WifiUniErrors : Map = new Map([ + + /** + * 未先调用 startWifi 接口 + */ + [12000, 'not init.'], + /** + * 当前系统不支持相关能力 + */ + [12001, 'system not support'], + /** + * 密码错误 + */ + [12002, 'password error Wi-Fi'], + /** + * Android 特有,未打开 Wi-Fi 开关 + */ + [12005, 'wifi not turned on'], + /** + * 用户拒绝授权链接 Wi-Fi + */ + [12007, 'user denied'], + /** + * 系统其他错误,需要在 errmsg 打印具体的错误原因 + */ + [12010, 'unknown error'], + /** + * 系统保存的 Wi-Fi 配置过期,建议忘记 Wi-Fi 后重试,仅 Android 支持 + */ + [12013, 'wifi config may be expired'], + +]); + + +export function getErrcode(errCode : number) : WifiErrorCode { + const res = WifiUniErrors[errCode]; + return res == null ? 12000 : errCode; +} + + +export class WifiFailImpl extends UniError implements WifiFail { + constructor(errCode : WifiErrorCode) { + super(); + this.errSubject = UniErrorSubject; + this.errCode = errCode; + this.errMsg = WifiUniErrors[errCode] ?? ""; + } +} + -- GitLab