From 6afd901e960cb4882478e3ef0c34c875c28cdab1 Mon Sep 17 00:00:00 2001 From: lizhongyi Date: Fri, 10 Nov 2023 18:11:33 +0800 Subject: [PATCH] =?UTF-8?q?uni-wifi=20iOS=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-wifi/utssdk/app-ios/index.uts | 105 +++++++++--------- uni_modules/uni-wifi/utssdk/index.d.ts | 39 ++++--- uni_modules/uni-wifi/utssdk/interface.uts | 6 +- 3 files changed, 81 insertions(+), 69 deletions(-) diff --git a/uni_modules/uni-wifi/utssdk/app-ios/index.uts b/uni_modules/uni-wifi/utssdk/app-ios/index.uts index a2ccfe4..f2bdb50 100644 --- a/uni_modules/uni-wifi/utssdk/app-ios/index.uts +++ b/uni_modules/uni-wifi/utssdk/app-ios/index.uts @@ -3,20 +3,21 @@ import { CaptiveNetwork, kCNNetworkInfoKeySSID, kCNNetworkInfoKeyBSSID } from 'S import { NSArray, NSDictionary } from 'Foundation'; import { CFString } from 'CoreFoundation'; import { UIDevice } from 'UIKit'; -import { WifiOption, WifiConnectOption, GetConnectedWifiOptions, UniWifiInfo, UniWifiResult, UniWifiCallback, StartWifi, StopWifi, GetWifiList, OnGetWifiList, OffGetWifiList, GetConnectedWifi, ConnectWifi, OnWifiConnected, OnWifiConnectedWithPartialInfo, OffWifiConnected, OnOffWifiConnectedWithPartialInfo, SetWifiList } from "../interface.uts" +import { UniWifiResultCallbackWithPartialInfo, UniGetWifiListCallback, UniWifiResultCallback, WifiOption, WifiConnectOption, GetConnectedWifiOptions, UniWifiInfo, UniWifiResult, UniWifiCallback, StartWifi, StopWifi, GetWifiList, OnGetWifiList, OffGetWifiList, GetConnectedWifi, ConnectWifi, OnWifiConnected, OnWifiConnectedWithPartialInfo, OffWifiConnected, OffWifiConnectedWithPartialInfo, SetWifiList } from "../interface.uts" +import { WifiFailImpl, getErrcode } from '../unierror'; -/* +/* * 系统定位权限获取类 */ class LocationPromiseService implements CLLocationManagerDelegate { static promiseCompletionHandler: ((res: boolean)=>void)[] = [] - + manager?: CLLocationManager - + constructor(manager?: CLLocationManager) { this.manager = manager } - + initlizeManager(): boolean { if (this.manager == null) { this.manager = new CLLocationManager() @@ -24,20 +25,20 @@ class LocationPromiseService implements CLLocationManagerDelegate { } return true } - + locationManager(manager: CLLocationManager, @argumentLabel("didChangeAuthorization") status: CLAuthorizationStatus) { if (status == CLAuthorizationStatus.authorizedAlways || status == CLAuthorizationStatus.authorizedWhenInUse) { LocationPromiseService.promiseCompletionHandler.forEach((handler): void => { handler(true) }) - } else if (status == CLAuthorizationStatus.notDetermined) { + } else if (status == CLAuthorizationStatus.notDetermined) { manager.requestWhenInUseAuthorization() } else if (status == CLAuthorizationStatus.denied) { LocationPromiseService.promiseCompletionHandler.forEach((handler): void => { handler(false) }) } - } + } requestPromise(@escaping completion: (res: boolean)=>void) { let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus() if (status == CLAuthorizationStatus.notDetermined) { @@ -52,20 +53,20 @@ class LocationPromiseService implements CLLocationManagerDelegate { this.manager!.requestWhenInUseAuthorization() LocationPromiseService.promiseCompletionHandler.push(completion) } - } + } } } const locationPromiseService: LocationPromiseService = new LocationPromiseService(null) -/* - * 获取系统定位权限 +/* + * 获取系统定位权限 */ function requestLocationPromise(@escaping completion: (res: boolean)=>void) { locationPromiseService.requestPromise(completion) } -/* +/* * 获取当前连接的wifi信息(通过定位权限) */ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) { @@ -76,20 +77,20 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) secure: false, signalStrength: 0, frequency: 0 - } - + } + if (arr != null) { let list = arr! as NSArray let index: Int = 0 - while (index < list.count) { - let item = list[index] + while (index < list.count) { + let item = list[index] let interfaceName = item as string let dic = CNCopyCurrentNetworkInfo(interfaceName as CFString) if (dic != null) { let dict = dic! as NSDictionary - let SSID = dict[kCNNetworkInfoKeySSID as string] + let SSID = dict[kCNNetworkInfoKeySSID as string] let BSSID = dict[kCNNetworkInfoKeyBSSID as string] - + if (SSID != null && BSSID != null) { let ssid = SSID! as string let bssid = BSSID! as string @@ -103,7 +104,7 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) } index++ } - + if (wifiInfo.BSSID!.length > 0 && wifiInfo.SSID.length > 0) { let res: UniWifiResult = { errSubject: "uni-getConnectedWifi", @@ -114,12 +115,12 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) option.success?.(res) option.complete?.(res) }else { - let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error"); + let err = new WifiFailImpl(getErrcode(12010)); option.fail?.(err) option.complete?.(err) } }else { - let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error"); + let err = new WifiFailImpl(getErrcode(12010)); option.fail?.(err) option.complete?.(err) } @@ -127,7 +128,7 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) -/* +/* * 保存全局数据信息 */ class UniWiFiModuleGloabInfo { @@ -138,14 +139,14 @@ class UniWiFiModuleGloabInfo { -/* - * 初始化wifi模块 +/* + * 初始化wifi模块 */ export const startWifi: StartWifi = function (option: WifiOption) { UniWiFiModuleGloabInfo.alreadyStartWifi = true let res: UniWifiResult = { errSubject: "uni-startWifi", - errCode: 0, + errCode: 0, errMsg: "startWifi:ok", wifi: null } @@ -153,15 +154,15 @@ export const startWifi: StartWifi = function (option: WifiOption) { option.complete?.(res) } -/* - * 停止wifi模块 +/* + * 停止wifi模块 */ export const stopWifi: StopWifi = function (option: WifiOption) { UniWiFiModuleGloabInfo.alreadyStartWifi = false - LocationPromiseService.promiseCompletionHandler = [] + LocationPromiseService.promiseCompletionHandler = [] let res: UniWifiResult = { errSubject: "uni-stopWifi", - errCode: 0, + errCode: 0, errMsg: "stopWifi:ok", wifi: null } @@ -169,11 +170,11 @@ export const stopWifi: StopWifi = function (option: WifiOption) { option.complete?.(res) } -/* +/* * 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调 */ export const getWifiList: GetWifiList = function (option: WifiOption) { - let err = new UniError("uni-getWifiList",12001,"getWifiList:system not support"); + let err = new WifiFailImpl(getErrcode(12001)); option.fail?.(err) option.complete?.(err) } @@ -181,11 +182,11 @@ export const getWifiList: GetWifiList = function (option: WifiOption) { /* 获取wifi列表的回调 * note: 请在getWifiList方法的回调里调用该方法 */ -export const onGetWifiList: OnGetWifiList = function (callback: UniWifiCallback) { - +export const onGetWifiList: OnGetWifiList = function (callback: UniGetWifiListCallback) { + } -/* +/* * 注销获取wifi列表的回调 */ export const offGetWifiList: OffGetWifiList = function (callback: UniWifiCallback) { @@ -193,12 +194,12 @@ export const offGetWifiList: OffGetWifiList = function (callback: UniWifiCallbac } -/* +/* * 获取当前连接的wifi信息 */ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnectedWifiOptions) { if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) { - let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:not init"); + let err = new WifiFailImpl(getErrcode(12000)); option.fail?.(err) option.complete?.(err) } else{ @@ -207,7 +208,7 @@ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnected if (success == true) { fetchConnectedWifiWithLocationPromise(option) }else { - let err = new UniError("uni-getConnectedWifi",12007,"getConnectedWifi:user denied"); + let err = new WifiFailImpl(getErrcode(12007)); option.fail?.(err) option.complete?.(err) } @@ -218,50 +219,50 @@ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnected } } -/* +/* * 连接wifi */ export const connectWifi: ConnectWifi = function (option: WifiConnectOption) { - - let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support"); + + let err = new WifiFailImpl(getErrcode(12001)); option.fail?.(err) option.complete?.(err) } -/* +/* * 连上wifi事件的监听函数 */ -export const onWifiConnected: OnWifiConnected = function (callback: UniWifiCallback) { - +export const onWifiConnected: OnWifiConnected = function (callback: UniWifiResultCallback) { + } -/* +/* * 连上wifi事件的监听函数, wifiInfo仅包含ssid */ -export const onWifiConnectedWithPartialInfo: OnWifiConnectedWithPartialInfo = function (callback: UniWifiCallback) { - +export const onWifiConnectedWithPartialInfo: OnWifiConnectedWithPartialInfo = function (callback: UniWifiResultCallbackWithPartialInfo) { + } -/* +/* * 移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。 */ export const offWifiConnected: OffWifiConnected = function (callback: UniWifiCallback | null) { } -/* +/* * 移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。 */ -export const onOffWifiConnectedWithPartialInfo: OnOffWifiConnectedWithPartialInfo = function (callback: UniWifiCallback | null) { +export const offWifiConnectedWithPartialInfo: OffWifiConnectedWithPartialInfo = function (callback: UniWifiResultCallbackWithPartialInfo | null) { } -/* +/* * 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。 */ export const setWifiList: SetWifiList = function (option: WifiOption) { - let err = new UniError("uni-setWifiList",12001,"setWifiList:system not support"); + let err = new WifiFailImpl(getErrcode(12001)); option.fail?.(err) option.complete?.(err) -} \ No newline at end of file +} diff --git a/uni_modules/uni-wifi/utssdk/index.d.ts b/uni_modules/uni-wifi/utssdk/index.d.ts index da8fd21..64f0829 100644 --- a/uni_modules/uni-wifi/utssdk/index.d.ts +++ b/uni_modules/uni-wifi/utssdk/index.d.ts @@ -6,6 +6,7 @@ declare namespace UniNamespace { type WifiCompleteCallback = (res : UniWifiComplete) => void type UniWifiCallback = () => void + type WifiErrorCode = 1300002 interface UniWifiInfo { @@ -27,31 +28,41 @@ declare namespace UniNamespace { errCode : WifiErrorCode } - interface WifiConnectOption{ + 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; + success : WifiSuccessCallback | null; + fail : WifiFailCallback | null; + complete : WifiCompleteCallback | null; } interface GetConnectedWifiOptions { partialInfo : boolean | null; - success ?: WifiSuccessCallback | null; - fail ?: WifiFailCallback | null; + success : WifiSuccessCallback | null; + fail : WifiFailCallback | null; complete ?: WifiCompleteCallback | null; } interface WifiOption { - success ?: WifiSuccessCallback | null, - fail ?: WifiFailCallback | null, - complete ?: InstallApkCompleteCallback | null + success : WifiSuccessCallback | null, + fail : WifiFailCallback | null, + complete : InstallApkCompleteCallback | null + } + + interface UniWifiInfoWithPartialInfo { + SSID : string; } + type UniGetWifiListCallback = (wifiInfo:UTSJSONObject) => void + + type UniWifiResultCallback = (wifiInfo:UniWifiResult) => void + + type UniWifiResultCallbackWithPartialInfo = (wifiInfo:UniWifiInfoWithPartialInfo) => void + } declare interface Uni { @@ -207,7 +218,7 @@ declare interface Uni { ] } */ - onGetWifiList(callback : UniNamespace.UniWifiCallback) : void, + onGetWifiList(callback : UniNamespace.UniGetWifiListCallback) : void, /** * 移除获取到 Wi-Fi 列表数据事件的监听函数。 * @@ -279,7 +290,7 @@ declare interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - onWifiConnected(callback : UniNamespace.UniWifiCallback) : void, + onWifiConnected(callback : UniNamespace.UniWifiResultCallback) : void, /** * 监听连接上 Wi-Fi 的事件。 * @@ -303,7 +314,7 @@ declare interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - onWifiConnectedWithPartialInfo(callback : UniNamespace.UniWifiCallback) : void, + onWifiConnectedWithPartialInfo(callback : UniNamespace.UniWifiResultCallbackWithPartialInfo) : void, /** * 移除连接上 Wi-Fi 的事件的监听函数。 * @@ -333,7 +344,7 @@ declare interface Uni { * 移除连接上 Wi-Fi 的事件的监听函数。 * * @param {UniWifiCallback} callback - * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onOffWifiConnectedWithPartialInfo + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offWifiConnectedWithPartialInfo * @uniPlatform { * "app": { * "android": { @@ -352,7 +363,7 @@ declare interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - onOffWifiConnectedWithPartialInfo(callback ?: UniNamespace.UniWifiCallback) : void, + offWifiConnectedWithPartialInfo(callback ?: UniNamespace.UniWifiResultCallbackWithPartialInfo) : void, /** * SetWifiList 暂未实现 * diff --git a/uni_modules/uni-wifi/utssdk/interface.uts b/uni_modules/uni-wifi/utssdk/interface.uts index b568d02..feb68d9 100644 --- a/uni_modules/uni-wifi/utssdk/interface.uts +++ b/uni_modules/uni-wifi/utssdk/interface.uts @@ -83,7 +83,7 @@ export type OnWifiConnectedWithPartialInfo = (callback : UniWifiResultCallbackWi export type OffWifiConnected = (callback : UniWifiCallback | null) => void -export type OnOffWifiConnectedWithPartialInfo = (callback : UniWifiCallback | null) => void +export type OffWifiConnectedWithPartialInfo = (callback : UniWifiResultCallbackWithPartialInfo | null) => void export type SetWifiList = (option : WifiOption) => void @@ -382,7 +382,7 @@ interface Uni { * 移除连接上 Wi-Fi 的事件的监听函数。 * * @param {UniWifiCallback} callback - * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#onOffWifiConnectedWithPartialInfo + * @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#offWifiConnectedWithPartialInfo * @uniPlatform { * "app": { * "android": { @@ -401,7 +401,7 @@ interface Uni { * @uniVueVersion 2,3 //支持的vue版本 * @autotest { expectCallback: true } */ - onOffWifiConnectedWithPartialInfo(callback : UniWifiCallback | null) : void, + offWifiConnectedWithPartialInfo(callback : UniWifiResultCallbackWithPartialInfo | null) : void, /** * SetWifiList 暂未实现 * -- GitLab