index.uts 8.6 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 6 7 8 9 10
import { CFString } from 'CoreFoundation';
import { UIDevice } from 'UIKit';

/**
 * Wifi 函数通用入参封装
 */
type WifiOption = {
lizhongyi_'s avatar
lizhongyi_ 已提交
11 12 13
	success?: (res: UniWifiResult) => void;
	fail?: (res: UniWifiResult) => void;
	complete?: (res: UniWifiResult) => void;
14 15 16
};

/**
17
 * Wifi 链接参数封装 
18 19
 */
type WifiConnectOption = {
lizhongyi_'s avatar
lizhongyi_ 已提交
20 21 22 23 24 25 26 27
	SSID?: string;
	BSSID?: string;
	password?: string;
	maunal?: boolean;
	partialInfo?: boolean; //ios不生效
	success?: (res: UniWifiResult) => void;
	fail?: (res: UniWifiResult) => void;
	complete?: (res: UniWifiResult) => void;
28 29 30 31 32 33 34
}

/**
 * 获取当前链接的wifi信息
 */
type GetConnectedWifiOptions = {
	partialInfo?: boolean
lizhongyi_'s avatar
lizhongyi_ 已提交
35 36 37
	success?: (res: UniWifiResult) => void
	fail?: (res: UniWifiResult) => void
	complete?: (res: UniWifiResult) => void
38 39
}

lizhongyi_'s avatar
lizhongyi_ 已提交
40 41 42
/* 
 * 对外暴露的wifi信息
 */
43 44 45 46 47 48 49 50
type UniWifiInfo = {
	SSID: string;
	BSSID: string;
	secure: boolean;
	signalStrength: number;
	frequency: number;
}

lizhongyi_'s avatar
lizhongyi_ 已提交
51 52 53 54 55 56 57 58 59
type UniWifiResult = {
	errCode : number,
	errSubject : string,
	errMsg : string,
	wifi: UniWifiInfo | null
}

type UniWifiCallback = () => void

60 61

/* 
lizhongyi_'s avatar
lizhongyi_ 已提交
62
 * 系统定位权限获取类
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
 */
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
	}
	
81
	locationManager(manager: CLLocationManager, @argumentLabel("didChangeAuthorization") status: CLAuthorizationStatus) {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		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_ 已提交
126 127 128 129 130 131 132
	let wifiInfo: UniWifiInfo = {
		BSSID: "",
		SSID: "",
		secure: false,
		signalStrength: 0,
		frequency: 0
	} 
133
	
134 135
	if (arr != null) {
		let list = arr! as NSArray
lizhongyi_'s avatar
lizhongyi_ 已提交
136
		let index: Int = 0
137 138 139 140 141 142 143 144 145 146 147 148 149 150
		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
151
					wifiInfo.secure = false
152 153 154 155 156 157 158 159 160
					wifiInfo.signalStrength = 0
					wifiInfo.frequency = 0
					break;
				}
			}
			index++
		}
		
		if (wifiInfo.BSSID.length > 0 && wifiInfo.SSID.length > 0) {
lizhongyi_'s avatar
lizhongyi_ 已提交
161
			let res: UniWifiResult = {
162
				errSubject: "uni-getConnectedWifi",
163
				errCode: 0,
164
				errMsg: "getConnectedWifi:ok",
165 166 167 168 169
				wifi: wifiInfo,
			}
			option.success?.(res)
			option.complete?.(res)
		}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
170
			const res: UniWifiResult = { 
171 172
				errSubject: "uni-getConnectedWifi", 
				errCode: 12010, 
lizhongyi_'s avatar
lizhongyi_ 已提交
173 174
				errMsg: "getConnectedWifi:system internal error",
				wifi: null
175 176 177
			}
			option.fail?.(res)
			option.complete?.(res)
178 179
		}
	}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
180
		const res: UniWifiResult = {
181 182
			errSubject: "uni-getConnectedWifi", 
			errCode: 12010, 
lizhongyi_'s avatar
lizhongyi_ 已提交
183 184
			errMsg: "getConnectedWifi:system internal error",
			wifi: null
185 186 187
		}
		option.fail?.(res)
		option.complete?.(res)
188 189 190 191 192
	}
}



193 194 195
/* 
 * 保存全局数据信息
 */
196
class UniWiFiModuleGloabInfo {
lizhongyi_'s avatar
lizhongyi_ 已提交
197
	static alreadyStartWifi: boolean = false
198
}
199 200 201 202 203 204 205 206 207

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



/* 
 * 初始化wifi模块 
 */
export function startWifi(option: WifiOption) {
208
	UniWiFiModuleGloabInfo.alreadyStartWifi = true
lizhongyi_'s avatar
lizhongyi_ 已提交
209
	let res: UniWifiResult = {
210
		errSubject: "uni-startWifi",
211
		errCode: 0, 
lizhongyi_'s avatar
lizhongyi_ 已提交
212 213
		errMsg: "startWifi:ok",
		wifi: null
214
	}
215
	option.success?.(res)
216 217 218 219 220 221
	option.complete?.(res)
}

/* 
 * 停止wifi模块 
 */
222
export function stopWifi(option: WifiOption) {
223
	UniWiFiModuleGloabInfo.alreadyStartWifi = false
224
	LocationPromiseService.promiseCompletionHandler = []	
lizhongyi_'s avatar
lizhongyi_ 已提交
225
	let res: UniWifiResult = {
226 227
		errSubject: "uni-stopWifi",
		errCode: 0, 
lizhongyi_'s avatar
lizhongyi_ 已提交
228 229
		errMsg: "stopWifi:ok",
		wifi: null
230 231 232
	}
	option.success?.(res)
	option.complete?.(res)
233 234 235 236 237 238
}

/* 
 * 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调
 */
export function getWifiList(option: WifiOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
239
	let res: UniWifiResult = {
240
		errSubject: "uni-getWifiList",
241
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
242 243
		errMsg: "getWifiList:system not support",
		wifi: null
244 245 246 247 248 249 250 251
	}
	option.fail?.(res)
	option.complete?.(res)
}

/* 获取wifi列表的回调
 * note: 请在getWifiList方法的回调里调用该方法
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
252
export function onGetWifiList(callback: UniWifiCallback) {
253 254 255 256 257 258
	
}

/* 
 *	注销获取wifi列表的回调
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
259
export function offGetWifiList(callback: UniWifiCallback) {
260 261 262 263 264 265 266 267

}


/* 
 * 获取当前连接的wifi信息
 */
export function getConnectedWifi(option: GetConnectedWifiOptions) {
268
	if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) {
lizhongyi_'s avatar
lizhongyi_ 已提交
269
		let res: UniWifiResult = {
270
			errSubject: "uni-getConnectedWifi",
271
			errCode: 12000, 
272
			errMsg: "getConnectedWifi:not init",
lizhongyi_'s avatar
lizhongyi_ 已提交
273
			wifi: null
274 275 276
		}
		option.fail?.(res)
		option.complete?.(res)
277
	} else{
278 279 280 281 282
		if (UIDevice.current.systemVersion >= "13.0") {
			requestLocationPromise((success) => {
				if (success == true) {
					fetchConnectedWifiWithLocationPromise(option)
				}else {
lizhongyi_'s avatar
lizhongyi_ 已提交
283
					let res: UniWifiResult = {
284 285 286
						errSubject: "uni-getConnectedWifi",
						errCode: 12007, 
						errMsg: "getConnectedWifi:user denied",
lizhongyi_'s avatar
lizhongyi_ 已提交
287
						wifi: null
288 289 290 291 292 293 294 295
					}
					option.fail?.(res)
					option.complete?.(res)
				}
			})
		} else{
			fetchConnectedWifiWithLocationPromise(option)
		}
296 297 298 299 300 301 302
	}
}

/* 
 * 连接wifi
 */
export function connectWifi(option: WifiConnectOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
303
	let res: UniWifiResult = {
304
		errSubject: "uni-connectWifi",
305
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
306 307
		errMsg: "connectWifi:system not support",
		wifi: null
308 309 310 311 312 313 314 315 316
	}
	option.fail?.(res)
	option.complete?.(res)
}


/* 
 * 连上wifi事件的监听函数
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
317
export function onWifiConnected(callback: UniWifiCallback) {
318 319 320 321 322 323
	
}

/* 
 * 连上wifi事件的监听函数, wifiInfo仅包含ssid
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
324
export function onWifiConnectedWithPartialInfo(callback: UniWifiCallback) {
325 326 327 328 329 330
	
}

/* 
 *	移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
331
export function offWifiConnected(callback: UniWifiCallback | null) {
332 333 334 335 336 337

}

/* 
 *	移除连接上wifi的事件的监听函数,不传此参数则移除所有监听函数。
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
338
export function onOffWifiConnectedWithPartialInfo(callback: UniWifiCallback | null) {
339 340 341 342 343 344 345

}

/* 
 * 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。
 */
export function setWifiList(option: WifiOption) {
lizhongyi_'s avatar
lizhongyi_ 已提交
346
	let res: UniWifiResult = {
347
		errSubject: "uni-setWifiList",
348
		errCode: 12001, 
lizhongyi_'s avatar
lizhongyi_ 已提交
349 350
		errMsg: "setWifiList:system not support",
		wifi: null
351 352 353 354
	}
	option.fail?.(res)
	option.complete?.(res)
}