index.uts 7.7 KB
Newer Older
1 2
import { CLLocationManager, CLAuthorizationStatus, CLLocationManagerDelegate } from 'CoreLocation'
import { CaptiveNetwork, kCNNetworkInfoKeySSID, kCNNetworkInfoKeyBSSID } from 'SystemConfiguration.CaptiveNetwork';
lizhongyi_'s avatar
lizhongyi_ 已提交
3
import { NSArray, NSDictionary } from 'Foundation';
4 5
import { CFString } from 'CoreFoundation';
import { UIDevice } from 'UIKit';
lizhongyi_'s avatar
lizhongyi_ 已提交
6 7
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';
8

lizhongyi_'s avatar
lizhongyi_ 已提交
9
/*
lizhongyi_'s avatar
lizhongyi_ 已提交
10
 * 系统定位权限获取类
11 12 13
 */
class LocationPromiseService implements CLLocationManagerDelegate  {
	static promiseCompletionHandler: ((res: boolean)=>void)[] = []
lizhongyi_'s avatar
lizhongyi_ 已提交
14

15
	manager?: CLLocationManager
lizhongyi_'s avatar
lizhongyi_ 已提交
16

17 18 19
	constructor(manager?: CLLocationManager) {
		this.manager = manager
	}
lizhongyi_'s avatar
lizhongyi_ 已提交
20

21 22 23 24 25 26 27
	initlizeManager(): boolean {
		if (this.manager == null) {
			this.manager = new CLLocationManager()
			this.manager!.delegate = this
		}
		return true
	}
lizhongyi_'s avatar
lizhongyi_ 已提交
28

29
	locationManager(manager: CLLocationManager, @argumentLabel("didChangeAuthorization") status: CLAuthorizationStatus) {
30 31 32 33
		if (status == CLAuthorizationStatus.authorizedAlways || status == CLAuthorizationStatus.authorizedWhenInUse) {
			LocationPromiseService.promiseCompletionHandler.forEach((handler): void => {
				handler(true)
			})
lizhongyi_'s avatar
lizhongyi_ 已提交
34
		} else if (status == CLAuthorizationStatus.notDetermined) {
35 36 37 38 39 40
			manager.requestWhenInUseAuthorization()
		} else if (status == CLAuthorizationStatus.denied) {
			LocationPromiseService.promiseCompletionHandler.forEach((handler): void => {
				handler(false)
			})
		}
lizhongyi_'s avatar
lizhongyi_ 已提交
41
	}
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	requestPromise(@escaping completion: (res: boolean)=>void) {
		let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
		if (status == CLAuthorizationStatus.notDetermined) {
			if (this.initlizeManager() == true) {
				this.manager!.requestWhenInUseAuthorization()
				LocationPromiseService.promiseCompletionHandler.push(completion)
			}
		} else if (status == CLAuthorizationStatus.authorizedAlways || status == CLAuthorizationStatus.authorizedWhenInUse) {
			completion(true)
		} else if (status == CLAuthorizationStatus.denied) {
			if (CLLocationManager.locationServicesEnabled() == false && this.initlizeManager() == true) {
				this.manager!.requestWhenInUseAuthorization()
				LocationPromiseService.promiseCompletionHandler.push(completion)
			}
lizhongyi_'s avatar
lizhongyi_ 已提交
56
		}
57 58 59 60 61
	}
}

const locationPromiseService: LocationPromiseService = new LocationPromiseService(null)

lizhongyi_'s avatar
lizhongyi_ 已提交
62 63
/*
 * 获取系统定位权限
64 65 66 67 68
 */
function requestLocationPromise(@escaping completion: (res: boolean)=>void) {
	locationPromiseService.requestPromise(completion)
}

lizhongyi_'s avatar
lizhongyi_ 已提交
69
/*
70 71 72 73
 * 获取当前连接的wifi信息(通过定位权限)
 */
function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) {
	let arr = CNCopySupportedInterfaces()
lizhongyi_'s avatar
lizhongyi_ 已提交
74 75 76 77 78 79
	let wifiInfo: UniWifiInfo = {
		BSSID: "",
		SSID: "",
		secure: false,
		signalStrength: 0,
		frequency: 0
lizhongyi_'s avatar
lizhongyi_ 已提交
80 81
	}

82 83
	if (arr != null) {
		let list = arr! as NSArray
lizhongyi_'s avatar
lizhongyi_ 已提交
84
		let index: Int = 0
lizhongyi_'s avatar
lizhongyi_ 已提交
85 86
		while (index < list.count) {
			let item = list[index]
87 88 89 90
			let interfaceName = item as string
			let dic = CNCopyCurrentNetworkInfo(interfaceName as CFString)
			if (dic != null) {
				let dict = dic! as NSDictionary
lizhongyi_'s avatar
lizhongyi_ 已提交
91
				let SSID = dict[kCNNetworkInfoKeySSID as string]
92
				let BSSID = dict[kCNNetworkInfoKeyBSSID as string]
lizhongyi_'s avatar
lizhongyi_ 已提交
93

94 95 96 97 98
				if (SSID != null && BSSID != null) {
					let ssid = SSID! as string
					let bssid = BSSID! as string
					wifiInfo.SSID = ssid
					wifiInfo.BSSID = bssid
99
					wifiInfo.secure = false
100 101 102 103 104 105 106
					wifiInfo.signalStrength = 0
					wifiInfo.frequency = 0
					break;
				}
			}
			index++
		}
lizhongyi_'s avatar
lizhongyi_ 已提交
107

lizhongyi_'s avatar
lizhongyi_ 已提交
108
		if (wifiInfo.BSSID!.length > 0 && wifiInfo.SSID.length > 0) {
lizhongyi_'s avatar
lizhongyi_ 已提交
109
			let res: UniWifiResult = {
110
				errSubject: "uni-getConnectedWifi",
111
				errCode: 0,
112
				errMsg: "getConnectedWifi:ok",
113 114 115 116 117
				wifi: wifiInfo,
			}
			option.success?.(res)
			option.complete?.(res)
		}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
118
      let err = new WifiFailImpl(getErrcode(12010));
杜庆泉's avatar
杜庆泉 已提交
119 120
			option.fail?.(err)
			option.complete?.(err)
121 122
		}
	}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
123
    let err = new WifiFailImpl(getErrcode(12010));
杜庆泉's avatar
杜庆泉 已提交
124 125
		option.fail?.(err)
		option.complete?.(err)
126 127 128 129 130
	}
}



lizhongyi_'s avatar
lizhongyi_ 已提交
131
/*
132 133
 * 保存全局数据信息
 */
134
class UniWiFiModuleGloabInfo {
lizhongyi_'s avatar
lizhongyi_ 已提交
135
	static alreadyStartWifi: boolean = false
136
}
137 138 139 140 141

/* =================================== 对外暴露的接口 ==============================================*/



lizhongyi_'s avatar
lizhongyi_ 已提交
142 143
/*
 * 初始化wifi模块
144
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
145
export const startWifi: StartWifi = function (option: WifiOption) {
146
	UniWiFiModuleGloabInfo.alreadyStartWifi = true
lizhongyi_'s avatar
lizhongyi_ 已提交
147
	let res: UniWifiResult = {
148
		errSubject: "uni-startWifi",
lizhongyi_'s avatar
lizhongyi_ 已提交
149
		errCode: 0,
lizhongyi_'s avatar
lizhongyi_ 已提交
150 151
		errMsg: "startWifi:ok",
		wifi: null
152
	}
153
	option.success?.(res)
154 155 156
	option.complete?.(res)
}

lizhongyi_'s avatar
lizhongyi_ 已提交
157 158
/*
 * 停止wifi模块
159
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
160
export const stopWifi: StopWifi = function (option: WifiOption) {
161
	UniWiFiModuleGloabInfo.alreadyStartWifi = false
lizhongyi_'s avatar
lizhongyi_ 已提交
162
	LocationPromiseService.promiseCompletionHandler = []
lizhongyi_'s avatar
lizhongyi_ 已提交
163
	let res: UniWifiResult = {
164
		errSubject: "uni-stopWifi",
lizhongyi_'s avatar
lizhongyi_ 已提交
165
		errCode: 0,
lizhongyi_'s avatar
lizhongyi_ 已提交
166 167
		errMsg: "stopWifi:ok",
		wifi: null
杜庆泉's avatar
杜庆泉 已提交
168
	}
169 170
	option.success?.(res)
	option.complete?.(res)
171 172
}

lizhongyi_'s avatar
lizhongyi_ 已提交
173
/*
174 175
 * 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
176
export const getWifiList: GetWifiList = function (option: WifiOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
177
  let err = new WifiFailImpl(getErrcode(12001));
杜庆泉's avatar
杜庆泉 已提交
178 179
	option.fail?.(err)
	option.complete?.(err)
180 181 182 183 184
}

/* 获取wifi列表的回调
 * note: 请在getWifiList方法的回调里调用该方法
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
185 186
export const onGetWifiList: OnGetWifiList = function (callback: UniGetWifiListCallback) {

187 188
}

lizhongyi_'s avatar
lizhongyi_ 已提交
189
/*
190 191
 *	注销获取wifi列表的回调
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
192
export const offGetWifiList: OffGetWifiList = function (callback: UniWifiCallback) {
193

194 195 196
}


lizhongyi_'s avatar
lizhongyi_ 已提交
197
/*
198 199
 * 获取当前连接的wifi信息
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
200
export const getConnectedWifi: GetConnectedWifi = function (option: GetConnectedWifiOptions) {
201
	if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) {
lizhongyi_'s avatar
lizhongyi_ 已提交
202
    let err = new WifiFailImpl(getErrcode(12000));
杜庆泉's avatar
杜庆泉 已提交
203 204
		option.fail?.(err)
		option.complete?.(err)
205
	} else{
206 207 208 209 210
		if (UIDevice.current.systemVersion >= "13.0") {
			requestLocationPromise((success) => {
				if (success == true) {
					fetchConnectedWifiWithLocationPromise(option)
				}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
211
          let err = new WifiFailImpl(getErrcode(12007));
杜庆泉's avatar
杜庆泉 已提交
212 213
					option.fail?.(err)
					option.complete?.(err)
214 215 216 217 218
				}
			})
		} else{
			fetchConnectedWifiWithLocationPromise(option)
		}
219 220 221
	}
}

lizhongyi_'s avatar
lizhongyi_ 已提交
222
/*
223 224
 * 连接wifi
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
225
export const connectWifi: ConnectWifi = function (option: WifiConnectOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
226 227

  let err = new WifiFailImpl(getErrcode(12001));
杜庆泉's avatar
杜庆泉 已提交
228 229
	option.fail?.(err)
	option.complete?.(err)
230 231 232
}


lizhongyi_'s avatar
lizhongyi_ 已提交
233
/*
234 235
 * 连上wifi事件的监听函数
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
236 237
export const onWifiConnected: OnWifiConnected = function (callback: UniWifiResultCallback) {

238 239
}

lizhongyi_'s avatar
lizhongyi_ 已提交
240
/*
241 242
 * 连上wifi事件的监听函数, wifiInfo仅包含ssid
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
243 244
export const onWifiConnectedWithPartialInfo: OnWifiConnectedWithPartialInfo = function (callback: UniWifiResultCallbackWithPartialInfo) {

245 246
}

lizhongyi_'s avatar
lizhongyi_ 已提交
247
/*
248 249
 *	移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
250
export const offWifiConnected: OffWifiConnected = function (callback: UniWifiCallback | null) {
251

252
}
lizhongyi_'s avatar
lizhongyi_ 已提交
253

lizhongyi_'s avatar
lizhongyi_ 已提交
254
/*
255 256
 *	移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
257
export const offWifiConnectedWithPartialInfo: OffWifiConnectedWithPartialInfo = function (callback: UniWifiResultCallbackWithPartialInfo | null) {
258

259 260
}

lizhongyi_'s avatar
lizhongyi_ 已提交
261
/*
262 263
 * 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
264
export const setWifiList: SetWifiList = function (option: WifiOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
265
  let err = new WifiFailImpl(getErrcode(12001));
杜庆泉's avatar
杜庆泉 已提交
266 267
	option.fail?.(err)
	option.complete?.(err)
lizhongyi_'s avatar
lizhongyi_ 已提交
268
}