提交 4ce655e2 编写于 作者: 杜庆泉's avatar 杜庆泉

wifi 适配 uni error.

上级 dcfa32bb
import Context from "android.content.Context"; import Context from "android.content.Context";
import { UTSAndroid } from "io.dcloud.uts";
import WifiManager from "android.net.wifi.WifiManager"; import WifiManager from "android.net.wifi.WifiManager";
import WifiInfo from "android.net.wifi.WifiInfo"; import WifiInfo from "android.net.wifi.WifiInfo";
import Manifest from "android.Manifest"; import Manifest from "android.Manifest";
...@@ -8,9 +7,7 @@ import ScanResult from "android.net.wifi.ScanResult"; ...@@ -8,9 +7,7 @@ import ScanResult from "android.net.wifi.ScanResult";
import BroadcastReceiver from "android.content.BroadcastReceiver"; import BroadcastReceiver from "android.content.BroadcastReceiver";
import ActivityCompat from "androidx.core.app.ActivityCompat"; import ActivityCompat from "androidx.core.app.ActivityCompat";
import IntentFilter from "android.content.IntentFilter"; import IntentFilter from "android.content.IntentFilter";
import JSONObject from "com.alibaba.fastjson.JSONObject";
import Intent from "android.content.Intent"; import Intent from "android.content.Intent";
import Thread from "java.lang.Thread";
import WifiConfiguration from 'android.net.wifi.WifiConfiguration'; import WifiConfiguration from 'android.net.wifi.WifiConfiguration';
import AuthAlgorithm from 'android.net.wifi.WifiConfiguration.AuthAlgorithm'; import AuthAlgorithm from 'android.net.wifi.WifiConfiguration.AuthAlgorithm';
...@@ -18,8 +15,8 @@ import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt'; ...@@ -18,8 +15,8 @@ import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt';
import TextUtils from 'android.text.TextUtils'; import TextUtils from 'android.text.TextUtils';
import Build from 'android.os.Build'; 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 { ...@@ -32,12 +29,12 @@ class Global {
// 扫描wifi结果 // 扫描wifi结果
static scanList : AndroidUniWifiInfo[] = [] static scanList : AndroidUniWifiInfo[] = []
// 获取wifi列表监听 // 获取wifi列表监听
static onGetWifiListCallback : UTSCallback | null = null static onGetWifiListCallback : UniGetWifiListCallback|null = null
static supendGetWifiSuccess : ((res: UniWifiResult) => void) | null = null static supendGetWifiSuccess : UniWifiResultCallback|null = null
static supendGetWifiComplete : ((res: UniWifiResult) => void) | null = null static supendGetWifiComplete :UniWifiResultCallback|null = null
// wifi链接监听 // wifi链接监听
static onWifiConnectCallbackList : UTSCallback[] = [] static onWifiConnectCallbackList : UniWifiResultCallback[] = []
static onWifiConnectWithPartialInfoCallbackList : UTSCallback[] = [] static onWifiConnectWithPartialInfoCallbackList : UniWifiResultCallbackWithPartialInfo[] = []
} }
...@@ -280,19 +277,16 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -280,19 +277,16 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
// 封装成数据对象 // 封装成数据对象
uniWifiInfo = wrapUniWifiInfoFromConnectInfo(winfo) uniWifiInfo = wrapUniWifiInfoFromConnectInfo(winfo)
let res = {
errMsg: 'onWifiConnected:ok', let res = new UniWifiResult(0,UniErrorSubject,'onWifiConnected:ok',uniWifiInfo)
errCode: 0,
wifi: uniWifiInfo
}
// wifi状态可用了,分发当前的链接状态给已注册的监听集合 // wifi状态可用了,分发当前的链接状态给已注册的监听集合
for (let perCallback in Global.onWifiConnectCallbackList) { for (let perCallback in Global.onWifiConnectCallbackList) {
perCallback(res); perCallback(res);
} }
// 封装仅SSID 数据对象 // 封装仅SSID 数据对象
var connectedWithPartialInfo = { let connectedWithPartialInfo = new UniWifiInfoWithPartialInfo(uniWifiInfo.SSID)
SSID: uniWifiInfo.SSID
}
for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) { for (let perCallback in Global.onWifiConnectWithPartialInfoCallbackList) {
perCallback(connectedWithPartialInfo); perCallback(connectedWithPartialInfo);
} }
...@@ -305,14 +299,9 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -305,14 +299,9 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) { if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
startWifiScaning = false; startWifiScaning = false;
// wifi 扫描结果回调 // wifi 扫描结果回调
let results = this.mWifiManager!.scanResults; let results = this.mWifiManager!.scanResults;
if (results != null) { if (results != null) {
Global.scanList = [] Global.scanList = []
for (let scanResult in results) { for (let scanResult in results) {
...@@ -324,7 +313,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -324,7 +313,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
// 挨个通知,所有的监听器 // 挨个通知,所有的监听器
if(Global.onGetWifiListCallback != null){ if(Global.onGetWifiListCallback != null){
const data = new JSONObject(); const data = new UTSJSONObject();
data["wifiList"] = Global.scanList data["wifiList"] = Global.scanList
Global.onGetWifiListCallback?.(data); Global.onGetWifiListCallback?.(data);
/** /**
...@@ -335,11 +324,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver { ...@@ -335,11 +324,7 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
} }
let ret : UniWifiResult = { let ret = new UniWifiResult(0,UniErrorSubject,"getWifiList:ok",null)
errCode: 0,
errSubject: "uni-getWifiList",
errMsg: "getWifiList:ok"
}
if(Global.supendGetWifiSuccess != null){ if(Global.supendGetWifiSuccess != null){
Global.supendGetWifiSuccess?.(ret) Global.supendGetWifiSuccess?.(ret)
Global.supendGetWifiSuccess = null Global.supendGetWifiSuccess = null
...@@ -365,29 +350,7 @@ var startWifiScaning = false ...@@ -365,29 +350,7 @@ var startWifiScaning = false
* 开启wifi * 开启wifi
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
export function startWifi(option : WifiOption) { export function startWifiImpl(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 = let wifiManager : WifiManager =
...@@ -395,16 +358,15 @@ export function startWifi(option : WifiOption) { ...@@ -395,16 +358,15 @@ export function startWifi(option : WifiOption) {
// 用户没有开启wifi 总开关 // 用户没有开启wifi 总开关
if (!wifiManager.isWifiEnabled()) { if (!wifiManager.isWifiEnabled()) {
// wifi 没开启 // wifi 没开启
let err = new UniError("uni-startWifi",12005,"wifi not turned on"); let failResult = new WifiFailImpl(getErrcode(12005));
option.fail?.(err); option.fail?.(failResult);
option.complete?.(err); option.complete?.(failResult);
return; return;
} }
// 初始化wifi 状态广播监听,后续所有的api,均基于此 // 初始化wifi 状态广播监听,后续所有的api,均基于此
if(Global.mReceiver != null){ if(Global.mReceiver != null){
// 说明已经注册过了 // 说明已经注册过了
result.errCode = 0 let result = UniWifiResult(0,UniErrorSubject,"startWifi:ok",null)
result.errMsg = "startWifi:ok"
option.success?.(result) option.success?.(result)
option.complete?.(result) option.complete?.(result)
...@@ -443,14 +405,32 @@ export function startWifi(option : WifiOption) { ...@@ -443,14 +405,32 @@ export function startWifi(option : WifiOption) {
// 开始扫描 // 开始扫描
wifiManager.startScan() wifiManager.startScan()
result.errCode = 0 let result = UniWifiResult(0,UniErrorSubject,"startWifi:ok",null)
result.errMsg = "startWifi:ok"
option.success?.(result) option.success?.(result)
option.complete?.(result) option.complete?.(result)
} }
@Suppress("DEPRECATION")
export function startWifi(option : WifiOption) {
/**
* 准备权限
*/
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)
})
}
/** /**
* 获取wifi列表 * 获取wifi列表
*/ */
...@@ -460,8 +440,7 @@ export function getWifiList(option : WifiOption) { ...@@ -460,8 +440,7 @@ export function getWifiList(option : WifiOption) {
if (Global.mReceiver == null) { if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错 // 还没调用startWifi 提示报错
let err = new UniError("uni-getWifiList",12000,"getWifiList:fail:not invoke startWifi"); let err = new WifiFailImpl(getErrcode(12000));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -471,14 +450,15 @@ export function getWifiList(option : WifiOption) { ...@@ -471,14 +450,15 @@ export function getWifiList(option : WifiOption) {
let wifiManager : WifiManager = let wifiManager : WifiManager =
UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager UTSAndroid.getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
if(option.success != null){
Global.supendGetWifiSuccess = option.success Global.supendGetWifiSuccess = option.success
}
if(option.complete != null){
Global.supendGetWifiComplete = option.complete Global.supendGetWifiComplete = option.complete
}
wifiManager.startScan() wifiManager.startScan()
} }
...@@ -486,12 +466,12 @@ export function getWifiList(option : WifiOption) { ...@@ -486,12 +466,12 @@ export function getWifiList(option : WifiOption) {
/** /**
* wifi 链接成功的回调注册 * wifi 链接成功的回调注册
*/ */
export function onWifiConnected(callback : UTSCallback) { export function onWifiConnected(callback : UniWifiResultCallback) {
Global.onWifiConnectCallbackList.push(callback) Global.onWifiConnectCallbackList.push(callback)
} }
export function onWifiConnectedWithPartialInfo(callback : UTSCallback) { export function onWifiConnectedWithPartialInfo(callback : UniWifiResultCallbackWithPartialInfo) {
Global.onWifiConnectWithPartialInfoCallbackList.push(callback) Global.onWifiConnectWithPartialInfoCallbackList.push(callback)
} }
...@@ -499,7 +479,7 @@ export function onWifiConnectedWithPartialInfo(callback : UTSCallback) { ...@@ -499,7 +479,7 @@ export function onWifiConnectedWithPartialInfo(callback : UTSCallback) {
/** /**
* wifi 链接成功的回调取消注册 * wifi 链接成功的回调取消注册
*/ */
export function offWifiConnected(callback? : UTSCallback) { export function offWifiConnected(callback? : UniWifiResultCallback) {
if(callback == null){ if(callback == null){
Global.onWifiConnectCallbackList = [] Global.onWifiConnectCallbackList = []
...@@ -515,7 +495,7 @@ export function offWifiConnected(callback? : UTSCallback) { ...@@ -515,7 +495,7 @@ export function offWifiConnected(callback? : UTSCallback) {
/** /**
* 不具备详细信息的wifi 反注册 * 不具备详细信息的wifi 反注册
*/ */
export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { export function offWifiConnectedWithPartialInfo(callback? : UniWifiResultCallbackWithPartialInfo) {
if(callback == null){ if(callback == null){
Global.onWifiConnectWithPartialInfoCallbackList = [] Global.onWifiConnectWithPartialInfoCallbackList = []
...@@ -531,14 +511,14 @@ export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) { ...@@ -531,14 +511,14 @@ export function offWifiConnectedWithPartialInfo(callback? : UTSCallback) {
/** /**
* 注册Wifi列表的监听事件 * 注册Wifi列表的监听事件
*/ */
export function onGetWifiList(callback : UTSCallback) { export function onGetWifiList(callback : UniGetWifiListCallback) {
Global.onGetWifiListCallback = callback Global.onGetWifiListCallback = callback
} }
/** /**
* 取消注册Wifi列表的监听事件 * 取消注册Wifi列表的监听事件
*/ */
export function offGetWifiList(callback? : UTSCallback) { export function offGetWifiList(callback? : UniWifiCallback) {
Global.onGetWifiListCallback = null Global.onGetWifiListCallback = null
Global.supendGetWifiComplete = null Global.supendGetWifiComplete = null
Global.supendGetWifiSuccess = null Global.supendGetWifiSuccess = null
...@@ -552,7 +532,7 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -552,7 +532,7 @@ function realWifiConnect(option : WifiConnectOption){
if (Global.mReceiver == null || Global.scanList.length < 1) { 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.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -569,7 +549,7 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -569,7 +549,7 @@ function realWifiConnect(option : WifiConnectOption){
if (scanWifiInfo == null) { 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.fail?.(err)
option.complete?.(err) option.complete?.(err)
return return
...@@ -597,12 +577,12 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -597,12 +577,12 @@ function realWifiConnect(option : WifiConnectOption){
// add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持 // add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持
if(Build.VERSION.SDK_INT > 28){ if(Build.VERSION.SDK_INT > 28){
// 系统大于android 9 // 系统大于android 9
let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support"); let err = new WifiFailImpl(getErrcode(12001));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
}else{ }else{
// 移除之前的配置失败了,返回错误,需要用户手动取消保存一下 // 移除之前的配置失败了,返回错误,需要用户手动取消保存一下
let err = new UniError("uni-connectWifi",12013,"connectWifi:wifi config may be expired"); let err = new WifiFailImpl(getErrcode(12013));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
} }
...@@ -627,9 +607,7 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -627,9 +607,7 @@ function realWifiConnect(option : WifiConnectOption){
let netID = wifiManager.addNetwork(wifiConfigration); let netID = wifiManager.addNetwork(wifiConfigration);
// 如果-1 说明没添加上,报错即可 // 如果-1 说明没添加上,报错即可
if (netID < 0) { if (netID < 0) {
let err = new WifiFailImpl(getErrcode(12002));
let err = new UniError("uni-connectWifi",12002,"connectWifi:password error Wi-Fi");
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -639,9 +617,7 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -639,9 +617,7 @@ function realWifiConnect(option : WifiConnectOption){
let enabled = wifiManager.enableNetwork(netID, true); let enabled = wifiManager.enableNetwork(netID, true);
if (!enabled) { if (!enabled) {
let err = new WifiFailImpl(getErrcode(12007));
let err = new UniError("uni-connectWifi",12007,"connectWifi:user denied");
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -657,17 +633,13 @@ function realWifiConnect(option : WifiConnectOption){ ...@@ -657,17 +633,13 @@ function realWifiConnect(option : WifiConnectOption){
if (!connected) { if (!connected) {
// 出错了,返回错误 // 出错了,返回错误
// 兜底的报错 // 兜底的报错
let err = new UniError("uni-connectWifi",12010,"connectWifi:fail:unknown error"); let err = new WifiFailImpl(getErrcode(12010));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
return return
} }
let result : UniWifiResult = { let result = new UniWifiResult(0,UniErrorSubject,"getWifiList:ok",null)
errCode: 0,
errMsg: "connectWifi:ok",
errSubject: "uni-connectWifi",
}
wifiManager.saveConfiguration() wifiManager.saveConfiguration()
//scanWifiInfo 根据 partialInfo 填充给返回字段 //scanWifiInfo 根据 partialInfo 填充给返回字段
...@@ -741,7 +713,7 @@ export function connectWifi(option : WifiConnectOption) { ...@@ -741,7 +713,7 @@ export function connectWifi(option : WifiConnectOption) {
export function stopWifi(option : WifiOption) { export function stopWifi(option : WifiOption) {
// 需要先开启wifi,才能使用后续的功能 // 需要先开启wifi,才能使用后续的功能
if (Global.mReceiver == null) { if (Global.mReceiver == null) {
let err = new UniError("uni-stopWifi",12000,"stopWifi:not init"); let err = new WifiFailImpl(getErrcode(12000));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -759,14 +731,9 @@ export function stopWifi(option : WifiOption) { ...@@ -759,14 +731,9 @@ export function stopWifi(option : WifiOption) {
Global.onWifiConnectCallbackList = [] Global.onWifiConnectCallbackList = []
Global.mReceiver = null Global.mReceiver = null
let result : UniWifiResult = { let ret = new UniWifiResult(0,UniErrorSubject,"stopWifi:ok",null)
errCode: 0, option.success?.(ret)
errSubject: "uni-stopWifi", option.complete?.(ret)
errMsg: "stopWifi:ok"
}
option.success?.(result)
option.complete?.(result)
} }
...@@ -783,7 +750,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { ...@@ -783,7 +750,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
if (Global.mReceiver == null) { if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错 // 还没调用startWifi 提示报错
let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); let err = new WifiFailImpl(getErrcode(12000));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
...@@ -793,7 +760,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { ...@@ -793,7 +760,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
// 需要先校验权限,没有位置权限无法获取wifi // 需要先校验权限,没有位置权限无法获取wifi
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 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.fail?.(err)
option.complete?.(err) option.complete?.(err)
return; return;
...@@ -812,11 +779,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { ...@@ -812,11 +779,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo); wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo);
let res : UniWifiResult = { let res = new UniWifiResult(0,UniErrorSubject,"getConnectedWifi:ok",null)
errCode: 0,
errMsg: "getConnectedWifi:ok",
errSubject: "uni-getConnectedWifi",
}
// 判断一下是否wifi 关闭了 // 判断一下是否wifi 关闭了
if (option.partialInfo!= null) { if (option.partialInfo!= null) {
...@@ -826,7 +789,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { ...@@ -826,7 +789,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
res.wifi = ret; res.wifi = ret;
} else { } else {
if (wifiInfo.BSSID == null || zeroCountNum(wifiInfo.BSSID) > 3) { 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.fail?.(err)
option.complete?.(err) option.complete?.(err)
return return
...@@ -840,7 +803,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) { ...@@ -840,7 +803,7 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
return return
} }
let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi"); let err = new WifiFailImpl(getErrcode(12000));
option.fail?.(err) option.fail?.(err)
option.complete?.(err) option.complete?.(err)
} }
......
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,
}
...@@ -42,6 +42,10 @@ export type UniWifiInfo = { ...@@ -42,6 +42,10 @@ export type UniWifiInfo = {
frequency ?: number; frequency ?: number;
} }
export type UniWifiInfoWithPartialInfo = {
SSID : string;
}
export type UniWifiResult = { export type UniWifiResult = {
errCode : number, errCode : number,
...@@ -52,6 +56,12 @@ export type UniWifiResult = { ...@@ -52,6 +56,12 @@ export type UniWifiResult = {
export type UniWifiCallback = () => void 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 export type StartWifi = (option : WifiOption) => void
...@@ -77,6 +87,25 @@ export type OnOffWifiConnectedWithPartialInfo = (callback : UniWifiCallback | nu ...@@ -77,6 +87,25 @@ export type OnOffWifiConnectedWithPartialInfo = (callback : UniWifiCallback | nu
export type SetWifiList = (option : WifiOption) => void 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 { interface Uni {
/** /**
* 初始化Wi-Fi模块 * 初始化Wi-Fi模块
......
import { WifiErrorCode, WifiFail} from "./interface.uts"
/**
* 错误主题
*/
export const UniErrorSubject = 'uni-wifi';
/**
* 错误码
* @UniError
*/
export const WifiUniErrors : Map<WifiErrorCode, string> = 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] ?? "";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册