index.uts 8.2 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
import { UniWifiResult, GetConnectedWifiOptions, WifiConnectOption, WifiOption, UniWifiInfo, UniWifiCallback, StartWifi, StopWifi, GetWifiList, GetConnectedWifi, OnGetWifiList, OnWifiConnectedWithPartialInfo, OnOffWifiConnectedWithPartialInfo, OnWifiConnected, ConnectWifi, OffGetWifiList, OffWifiConnected, SetWifiList } from "../interface.uts"
7 8 9 10



/* 
lizhongyi_'s avatar
lizhongyi_ 已提交
11
 * 系统定位权限获取类
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 */
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()
			this.manager!.delegate = this
		}
		return true
	}
	
30
	locationManager(manager: CLLocationManager, @argumentLabel("didChangeAuthorization") status: CLAuthorizationStatus) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
		if (status == CLAuthorizationStatus.authorizedAlways || status == CLAuthorizationStatus.authorizedWhenInUse) {
			LocationPromiseService.promiseCompletionHandler.forEach((handler): void => {
				handler(true)
			})
		} 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) {
			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)
			}
		} 
	}
}

const locationPromiseService: LocationPromiseService = new LocationPromiseService(null)

/* 
 * 获取系统定位权限 
 */
function requestLocationPromise(@escaping completion: (res: boolean)=>void) {
	locationPromiseService.requestPromise(completion)
}

/* 
 * 获取当前连接的wifi信息(通过定位权限)
 */
function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions) {
	let arr = CNCopySupportedInterfaces()
lizhongyi_'s avatar
lizhongyi_ 已提交
75 76 77 78 79 80 81
	let wifiInfo: UniWifiInfo = {
		BSSID: "",
		SSID: "",
		secure: false,
		signalStrength: 0,
		frequency: 0
	} 
82
	
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if (arr != null) {
		let list = arr! as NSArray
		let index = 0
		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 BSSID = dict[kCNNetworkInfoKeyBSSID as string]
				
				if (SSID != null && BSSID != null) {
					let ssid = SSID! as string
					let bssid = BSSID! as string
					wifiInfo.SSID = ssid
					wifiInfo.BSSID = bssid
100
					wifiInfo.secure = false
101 102 103 104 105 106 107 108 109
					wifiInfo.signalStrength = 0
					wifiInfo.frequency = 0
					break;
				}
			}
			index++
		}
		
		if (wifiInfo.BSSID.length > 0 && wifiInfo.SSID.length > 0) {
lizhongyi_'s avatar
lizhongyi_ 已提交
110
			let res: UniWifiResult = {
111
				errSubject: "uni-getConnectedWifi",
112
				errCode: 0,
113
				errMsg: "getConnectedWifi:ok",
114 115 116 117 118
				wifi: wifiInfo,
			}
			option.success?.(res)
			option.complete?.(res)
		}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
119
			const res: UniWifiResult = { 
120 121
				errSubject: "uni-getConnectedWifi", 
				errCode: 12010, 
lizhongyi_'s avatar
lizhongyi_ 已提交
122 123
				errMsg: "getConnectedWifi:system internal error",
				wifi: null
124 125 126
			}
			option.fail?.(res)
			option.complete?.(res)
127 128
		}
	}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
129
		const res: UniWifiResult = {
130 131
			errSubject: "uni-getConnectedWifi", 
			errCode: 12010, 
lizhongyi_'s avatar
lizhongyi_ 已提交
132 133
			errMsg: "getConnectedWifi:system internal error",
			wifi: null
134 135 136
		}
		option.fail?.(res)
		option.complete?.(res)
137 138 139 140 141
	}
}



142 143 144
/* 
 * 保存全局数据信息
 */
145
class UniWiFiModuleGloabInfo {
lizhongyi_'s avatar
lizhongyi_ 已提交
146
	static alreadyStartWifi: boolean = false
147
}
148 149 150 151 152 153 154 155

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



/* 
 * 初始化wifi模块 
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
156 157

export const startWifi : StartWifi = function (option : WifiOption) {
158
	UniWiFiModuleGloabInfo.alreadyStartWifi = true
lizhongyi_'s avatar
lizhongyi_ 已提交
159
	let res: UniWifiResult = {
160
		errSubject: "uni-startWifi",
161
		errCode: 0, 
lizhongyi_'s avatar
lizhongyi_ 已提交
162 163
		errMsg: "startWifi:ok",
		wifi: null
164
	}
165
	option.success?.(res)
166 167 168 169 170 171
	option.complete?.(res)
}

/* 
 * 停止wifi模块 
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
172
export const stopWifi : StopWifi = function (option : WifiOption) {
173
	UniWiFiModuleGloabInfo.alreadyStartWifi = false
174
	LocationPromiseService.promiseCompletionHandler = []	
lizhongyi_'s avatar
lizhongyi_ 已提交
175
	let res: UniWifiResult = {
176 177
		errSubject: "uni-stopWifi",
		errCode: 0, 
lizhongyi_'s avatar
lizhongyi_ 已提交
178 179
		errMsg: "stopWifi:ok",
		wifi: null
180 181 182
	}
	option.success?.(res)
	option.complete?.(res)
183 184 185 186 187
}

/* 
 * 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
188 189
export const getWifiList : GetWifiList = function (option : WifiOption) {
	let res: UniWifiResult = {
190
		errSubject: "uni-getWifiList",
191
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
192 193
		errMsg: "getWifiList:system not support",
		wifi: null
194 195 196 197 198 199 200 201
	}
	option.fail?.(res)
	option.complete?.(res)
}

/* 获取wifi列表的回调
 * note: 请在getWifiList方法的回调里调用该方法
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
202
export const onGetWifiList : OnGetWifiList = function (callback: UniWifiCallback) {
203 204 205 206 207 208
	
}

/* 
 *	注销获取wifi列表的回调
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
209 210
export const offGetWifiList : OffGetWifiList = function (callback: UniWifiCallback) {
	
211 212 213 214 215 216
}


/* 
 * 获取当前连接的wifi信息
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
217
export const getConnectedWifi : GetConnectedWifi = function (option: GetConnectedWifiOptions) {
218
	if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) {
lizhongyi_'s avatar
lizhongyi_ 已提交
219
		let res: UniWifiResult = {
220
			errSubject: "uni-getConnectedWifi",
221
			errCode: 12000, 
222
			errMsg: "getConnectedWifi:not init",
lizhongyi_'s avatar
lizhongyi_ 已提交
223
			wifi: null
224 225 226
		}
		option.fail?.(res)
		option.complete?.(res)
227
	} else{
228 229 230 231 232
		if (UIDevice.current.systemVersion >= "13.0") {
			requestLocationPromise((success) => {
				if (success == true) {
					fetchConnectedWifiWithLocationPromise(option)
				}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
233
					let res: UniWifiResult = {
234 235 236
						errSubject: "uni-getConnectedWifi",
						errCode: 12007, 
						errMsg: "getConnectedWifi:user denied",
lizhongyi_'s avatar
lizhongyi_ 已提交
237
						wifi: null
238 239 240 241 242 243 244 245
					}
					option.fail?.(res)
					option.complete?.(res)
				}
			})
		} else{
			fetchConnectedWifiWithLocationPromise(option)
		}
246 247 248 249 250 251
	}
}

/* 
 * 连接wifi
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
252 253
export const connectWifi : ConnectWifi = function (option: WifiConnectOption) {
	let res: UniWifiResult = {
254
		errSubject: "uni-connectWifi",
255
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
256 257
		errMsg: "connectWifi:system not support",
		wifi: null
258 259 260 261 262 263 264 265 266
	}
	option.fail?.(res)
	option.complete?.(res)
}


/* 
 * 连上wifi事件的监听函数
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
267
export const onWifiConnected : OnWifiConnected = function (callback: UniWifiCallback) {
268 269 270 271 272 273
	
}

/* 
 * 连上wifi事件的监听函数, wifiInfo仅包含ssid
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
274
export const onWifiConnectedWithPartialInfo : OnWifiConnectedWithPartialInfo = function (callback: UniWifiCallback) {
275 276 277 278 279 280
	
}

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

lizhongyi_'s avatar
lizhongyi_ 已提交
285

286 287 288
/* 
 *	移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
289 290
export const onOffWifiConnectedWithPartialInfo : OnOffWifiConnectedWithPartialInfo = function (callback: UniWifiCallback | null) {
	
291 292 293 294 295
}

/* 
 * 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
296 297
export const setWifiList : SetWifiList = function (option: WifiOption) {
	let res: UniWifiResult = {
298
		errSubject: "uni-setWifiList",
299
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
300 301
		errMsg: "setWifiList:system not support",
		wifi: null
302 303 304 305
	}
	option.fail?.(res)
	option.complete?.(res)
}