提交 cd2a20a3 编写于 作者: lizhongyi_'s avatar lizhongyi_

删除uni-wifiinformation及其示例

上级 bccd6706
......@@ -13,7 +13,6 @@
<uni-collapse-item title="系统事件" :border="false">
<uni-list>
<uni-list-item @tap="gotoScreenListen" title="监听系统截屏" class="itemButton" :clickable="true" link/>
<uni-list-item @tap="gotoWifiInfomation" title="获取当前wifi信息" class="itemButton" :clickable="true" link/>
</uni-list>
</uni-collapse-item>
</uni-collapse>
......@@ -67,11 +66,6 @@
url:'/pages/SystemAPI/ScreenListen/screenlisten'
})
},
gotoWifiInfomation(){
uni.navigateTo({
url:'/pages/SystemAPI/WifiInfomation/wifiInfomation'
})
},
gotoAlert(){
uni.navigateTo({
url:'/pages/SystemAPI/Alert/alert'
......
<template>
<page-head title="获取当前连接wifi信息" accordion></page-head>
<view class="uni-padding-wrap uni-common-mt">
<button @tap="getLocationPromise">获取定位权限</button>
<button @tap="getWifiInfo">获取WiFi信息</button>
</view>
</template>
<script>
import { getCurrentWifiInfo, requestLocationPromise } from '@/uni_modules/uni-wifiinformation';
export default {
methods:{
getLocationPromise() {
requestLocationPromise(function(res){
console.log(res)
uni.showToast({
title: res["message"],
icon: 'none'
})
})
},
getWifiInfo() {
/* iOS12之后获取wifi信息需要获取系统定位权限,否则获取到的wifi信息为空 */
requestLocationPromise(function(res) {
if (res["success"] == true) {
getCurrentWifiInfo({
success:function(res){
console.log(res)
uni.showToast({
title: `当前wifi信息: interface: ${res.wifiInterface}, SSID: ${res.SSID}, BSSID: ${res.BSSID}` ,
icon: 'none'
})
}
})
}
})
},
}
}
</script>
<style>
</style>
\ No newline at end of file
{
"id": "uni-wifiinformation",
"displayName": "uni-wifiinformation",
"version": "1.0.0",
"description": "uni-wifiinformation",
"keywords": [
"uni-wifiinformation"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-currentwifiinformation
## 使用文档
```ts
import { getCurrentWifiInfo, requestLocationPromise } from '@/uni_modules/uni-wifiinformation';
// 获取系统定位权限
requestLocationPromise(function(res){
console.log(res)
uni.showToast({
title: res["message"],
icon: 'none'
})
})
// 获取手机当前连接的wifi信息
getCurrentWifiInfo({
success:function(res){
console.log(res)
uni.showToast({
title: `当前wifi信息: interface: ${res.wifiInterface}, SSID: ${res.SSID}, BSSID: ${res.BSSID}` ,
icon: 'none'
})
}
})
// 注意:iOS12之后获取wifi信息需要获取系统定位权限,否则获取到的wifi信息为空
// 所以建议在获取wifi信息之前,先调用获取系统定位权限的接口
// 示例如下:
requestLocationPromise(function(res) {
if (res["success"] == true) {
getCurrentWifiInfo({
success:function(res){
console.log(res)
uni.showToast({
title: `当前wifi信息: interface: ${res.wifiInterface}, SSID: ${res.SSID}, BSSID: ${res.BSSID}` ,
icon: 'none'
})
}
})
}
})
```
### 参数
#### getCurrentWifiInfo
Object object
|属性|类型|必填|说明|
|----|---|----|----|
|success|function|否|接口调用成功的回调函数|
|fail|function|否|接口调用失败的回调函数|
|complete|function|否|接口调用结束的回调函数(调用成功、失败都会执行)|
object.success 回调函数
|属性|类型|说明|
|----|---|----|
|wifiInterface|string|当前手机连接wifi的interface,如 en0|
|SSID|string|当前手机连接wifi的SSID,即wifi名称|
|BSSID|string|当前手机连接wifi的BSSID|
#### requestLocationPromise
completion UTSCallback
object 回调函数
|属性|类型|说明|
|----|---|----|
|success|bool|是否获取成功|
|message|string|描述信息|
import Context from "android.content.Context";
import { getAppContext, getUniActivity } from "io.dcloud.uts.android";
import WifiManager from "android.net.wifi.WifiManager";
import WifiInfo from "android.net.wifi.WifiInfo";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
import ScanResult from "android.net.wifi.ScanResult";
import BroadcastReceiver from "android.content.BroadcastReceiver";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import IntentFilter from "android.content.IntentFilter";
import Gson from "com.google.gson.Gson";
import JSONObject from "com.alibaba.fastjson.JSONObject";
import Intent from "android.content.Intent";
import Thread from "java.lang.Thread";
import WifiConfiguration from 'android.net.wifi.WifiConfiguration';
import AuthAlgorithm from 'android.net.wifi.WifiConfiguration.AuthAlgorithm';
import KeyMgmt from 'android.net.wifi.WifiConfiguration.KeyMgmt';
import TextUtils from 'android.text.TextUtils';
/**
* Wifi 函数通用入参封装
*/
type WifiOption = {
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
};
/**
* Wifi 链接参数封装
*/
type WifiConnectOption = {
SSID:string;
BSSID:string;
password:string;
maunal:boolean;
partialInfo:boolean;
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
}
function isHex(key:string):boolean {
for (var i = key.length - 1; i >= 0; i--) {
let c = key.charAt(i);
if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a'
&& c <= 'f')) {
return false;
}
}
return true;
}
/**
* 判断是否是wep格式的key
*/
function isHexWepKey(wepKey:string):boolean {
let len = wepKey.length;
// WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
if (len != 10 && len != 26 && len != 58) {
return false;
}
return isHex(wepKey);
}
function wrapWifiConfiguration(SSID:string ,password:string,passwordType:string):WifiConfiguration {
let config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
// nopass
if ("NONE".equals(passwordType)) {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
// // wep
if ("WEP".equals(passwordType)) {
if (!TextUtils.isEmpty(password)) {
if (isHexWepKey(password)) {
config.wepKeys[0] = password;
} else {
config.wepKeys[0] = "\"" + password + "\"";
}
}
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if ("WPA".equals(passwordType)) {
config.preSharedKey = "\"" + password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.TKIP);
// 此处需要修改否则不能自动重联
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers
.set(WifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
/**
* Wifi信息统一数据结构
*/
class UniWifiInfo {
SSID: String = "";
BSSID: String = "";
secure: boolean = false;
signalStrength: Number = 0;
frequency: Number = 0;
/*下面的字段属于扩展字段*/
securityType:string = ""
constructor(scanResult?: ScanResult) {
if (scanResult != null) {
// 如果是通过扫描列表得到的数据,进行封装
this.BSSID = scanResult.BSSID;
this.SSID = scanResult.SSID;
this.signalStrength = scanResult.level;
this.frequency = scanResult.frequency;
// 是否安全,微信的标准是是否需要密码。 来源:https://developers.weixin.qq.com/community/develop/doc/00064cf1790458db19cddf9925ac00?highLine=WifiInfo
this.secure = false;
let capabilities = scanResult.capabilities.trim();
if ((capabilities.equals(Global.WIFI_AUTH_OPEN) || capabilities.equals(Global.WIFI_AUTH_ROAM))) {
this.secure = false;
} else {
this.secure = true;
}
/*扩展字段*/
this.securityType = getSecurityType(scanResult);
}
}
/**
* 根据connectInfo 链接信息对wifi数据结构进行初始化
*/
wrapConnectInfo(connectInfo: WifiInfo): void {
console.log(connectInfo.getSSID());
console.log(connectInfo.getBSSID());
if (connectInfo.getBSSID() == null) {
return
}
if (connectInfo.getSSID() != null) {
let s = connectInfo.getSSID();
// 微信不带,这里需要去掉引号
if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') {
s = s.substring(1, s.length - 1);
}
this.SSID = s;
}
this.BSSID = connectInfo.getBSSID();
//Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
this.signalStrength = connectInfo.getRssi() + 100;
this.frequency = connectInfo.getFrequency();
}
}
/**
* 获取当前链接的wifi信息
*/
type GetConnectedWifiOptions = {
partialInfo?: boolean
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
/**
* 全局数据储存
*/
class Global {
static mReceiver?: CustomBroadcastReceiver = null;
static scanList: UniWifiInfo[] = []
static WIFI_AUTH_OPEN: String = "";
static WIFI_AUTH_ROAM: String = "[ESS]";
static getWifiListCallbackList: UTSCallback[] = []
static onWifiConnectCallbackList: UTSCallback[] = []
}
function getSecurityType(result:ScanResult):string {
if (result.capabilities.contains("WEP")) {
return "WEP";
} else if (result.capabilities.contains("PSK")) {
return "WPA";
} else if (result.capabilities.contains("EAP")) {
return "EAP";
}
return "NONE";
}
/**
* 自定义wifi变化广播监听器
*/
class CustomBroadcastReceiver extends BroadcastReceiver {
mWifiManager?: WifiManager = null;
constructor(wifiManager: WifiManager) {
super();
this.mWifiManager = wifiManager;
}
override onReceive(_context: Context, intent: Intent): void {
if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
let state =
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
if (state == WifiManager.WIFI_STATE_ENABLED) {
// 获取当前的connectInfo 并且进行数据封装
let uniWifiInfo = new UniWifiInfo(null)
//做一些异步操作
setTimeout(function() {
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
console.log(Thread.currentThread().getName())
// @ts-ignore
let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null) {
Thread.sleep(1000)
winfo = this.mWifiManager!.getConnectionInfo();
}
// 封装成数据对象
uniWifiInfo.wrapConnectInfo(winfo)
let res = {
errMsg: 'onWifiConnected:ok',
errCode: 0,
wifi: uniWifiInfo
}
// wifi状态可用了,分发当前的链接状态给已注册的监听集合
for (let perCallback in Global.onWifiConnectCallbackList) {
perCallback(res);
}
}, 100);
}
}
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
// wifi 扫描结果回调
let results = this.mWifiManager!.scanResults;
if (results != null) {
Global.scanList = []
for (let scanResult in results) {
if (scanResult.SSID == null) {
continue;
}
Global.scanList.push(new UniWifiInfo(scanResult));
}
// 挨个通知,所有的监听器
for (let perCallback in Global.getWifiListCallbackList) {
const data = new JSONObject();
let mainJsonStr = Gson().toJson(Global.scanList);
data["wifiList"] = mainJsonStr
perCallback(data);
}
}
}
}
}
/************************* 下面是对外提供的函数 *************************/
/**
* 获取wifi列表
*/
export function getWifiList(option: WifiOption) {
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
var result = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject:"uni-getWifiList"
}
option.fail?.(result)
option.complete?.(result)
return
}
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan()
let ret = {
errCode: 0,
errMsg: "getWifiList:ok"
}
option.success?.(ret)
option.complete?.(ret)
}
/**
* wifi 链接成功的回调注册
*/
export function onWifiConnected(callback: UTSCallback) {
Global.onWifiConnectCallbackList.push(callback)
}
/**
* wifi 链接成功的回调取消注册
*/
export function offWifiConnected(callback: UTSCallback) {
let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback)
if (callbackIndex > 0) {
Global.onWifiConnectCallbackList.splice(callbackIndex, 1);
}
}
/**
* 注册Wifi列表的监听事件
*/
export function onGetWifiList(callback: UTSCallback) {
Global.getWifiListCallbackList.push(callback)
}
/**
* 取消注册Wifi列表的监听事件
*/
export function offGetWifiList(callback: UTSCallback) {
let callbackIndex = Global.getWifiListCallbackList.indexOf(callback)
if (callbackIndex > 0) {
Global.getWifiListCallbackList.splice(callbackIndex, 1);
}
}
/**
* 链接指定wifi
*/
export function connectWifi(option: WifiConnectOption) {
var result = {
errCode: 12000,
errMsg: "connectWifi:fail:not invoke startWifi",
errSubject:"uni-connectWifi"
}
if (Global.mReceiver == null || Global.scanList.length < 1) {
// 还没调用startWifi 提示报错
option.fail?.(result)
option.complete?.(result)
return
}
if(option.maunal == true){
// 指定了手动模式
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
getUniActivity()!!.startActivity(manunalIntent);
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
return
}
// 执行后续的逻辑
let scanWifiInfo:UniWifiInfo|null = null
for (let scanResult in Global.scanList) {
if (scanResult.SSID.equals(option.SSID)) {
scanWifiInfo = scanResult
}
}
if(scanWifiInfo == null){
// 不在扫描列表中返回错误
option.fail?.(result)
option.complete?.(result)
return
}
console.log(JSON.stringify(scanWifiInfo.securityType));
let wifiConfigration = wrapWifiConfiguration(option.SSID,option.password,scanWifiInfo.securityType);
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
// 如果已经存在了指定wifi 配置,移除之
let targetExistConfig:WifiConfiguration|null = null
let existingConfigs = wifiManager.getConfiguredNetworks();
for (let existingConfig in existingConfigs) {
if (existingConfig.SSID.equals("\"" + option.SSID + "\"")) {
targetExistConfig = existingConfig
}
}
if (targetExistConfig != null) {
wifiManager.removeNetwork(targetExistConfig.networkId);
}
try {
let netID = wifiManager.addNetwork(wifiConfigration);
let enabled = wifiManager.enableNetwork(netID, true);
let connected = wifiManager.reconnect();
console.log(connected);
} catch (e) {
// TODO: handle exception
console.log(e);
// e.printStackTrace();
}
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
}
/**
* 开启wifi
*/
export function startWifi(option: WifiOption) {
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
var result = {
errCode: 12001,
errMsg: "startWifi:premission loss"
}
// 检查权限
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getUniActivity()!, permissionWifi, requestCode)
// 尚不具备权限,返回错误
option.fail?.(result)
option.complete?.(result)
return;
}
// 具备了权限,继续前进
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (Global.mReceiver == null) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
let filter = new IntentFilter()
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
// @ts-ignore
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
// @ts-ignore
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
getUniActivity()!.registerReceiver(Global.mReceiver, filter)
wifiManager.startScan()
result.errCode = 0
result.errMsg = "startWifi:ok"
option.success?.(result)
option.complete?.(result)
}
/**
* 关闭wifi
*/
export function stopWifi(option: WifiOption) {
// 需要先开启wifi,才能使用后续的功能
if (Global.mReceiver == null) {
var result = {
errNo: 12000,
errCode: 12000,
errMsg: "stopWifi:not init"
}
option.fail?.(result)
option.complete?.(result)
return
}
getUniActivity()!.unregisterReceiver(Global.mReceiver)
var result = {
errNo: 0,
errCode: 0,
errMsg: "stopWifi:ok"
}
option.success?.(result)
option.complete?.(result)
}
/**
* 获取当前连接中的wifi信息
*/
export function getConnectedWifi(option: GetConnectedWifiOptions) {
let wifiInfo = new UniWifiInfo(null)
var res = {
errCode: 12000,
errMsg: "getConnectedWifi:fail:not invoke startWifi",
wifi:wifiInfo
}
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
option.fail?.(res)
option.complete?.(res)
return
}
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// 尚不具备权限,返回错误
res.errCode = 12001
res.errMsg = "getConnectedWifi:permission loss"
option.fail?.(res)
option.complete?.(res)
return;
}
// TODO 应该try catch一下,把系统的错误码给返回来,然后和微信的错误码拉齐
// 需要先校验权限,没有位置权限无法获取wifi
const context = getAppContext();
if (context != null) {
const wm = context.getSystemService(
Context.WIFI_SERVICE
) as WifiManager;
// 测试android 12上可以使用
//@ts-ignore
const winfo = wm.getConnectionInfo();
// 封装成数据对象
wifiInfo.wrapConnectInfo(winfo);
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
res.wifi = wifiInfo;
option.success?.(res)
option.complete?.(res)
return
}
option.fail?.(res)
option.complete?.(res)
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>使用期间获取位置权限</string>
</dict>
</plist>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.wifi-info</key>
<true/>
</dict>
</plist>
{
"frameworks": [
"CoreLocation",
"SystemConfiguration"
],
"deploymentTarget": "9.0",
"validArchitectures": [
"arm64", "armv7" ]
}
\ No newline at end of file
import { CLLocationManager, CLAuthorizationStatus, CLLocationManagerDelegate } from 'CoreLocation'
import { CaptiveNetwork, kCNNetworkInfoKeySSID, kCNNetworkInfoKeyBSSID } from 'SystemConfiguration.CaptiveNetwork';
import { NSArray, NSDictionary } from 'Foundation';
import { CFString } from 'CoreFoundation';
/**
* 定义 接口参数
*/
type GetWifiInfoOptions = {
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
};
class CurrentWifiInfo {
wifiInterface: string = ""
SSID: string = ""
BSSID: string = ""
constructor(wifiInterface: string, ssid: string, bssid: string) {
this.wifiInterface = wifiInterface
this.SSID = ssid
this.BSSID = bssid
}
}
class LocationPromiseService implements CLLocationManagerDelegate {
static promiseCompletionHandler: UTSCallback[] = []
manager?: CLLocationManager
constructor(manager?: CLLocationManager) {
this.manager = manager
}
initlizeManager(): boolean {
if (this.manager == null) {
this.manager = new CLLocationManager()
this.manager!.delegate = this
}
return true
}
locationManager(manager: CLLocationManager, status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedAlways || status == CLAuthorizationStatus.authorizedWhenInUse) {
LocationPromiseService.promiseCompletionHandler.forEach((handler): void => {
handler({success : true, message: "authorized success"})
})
} else if (status == CLAuthorizationStatus.notDetermined) {
manager.requestWhenInUseAuthorization()
} else if (status == CLAuthorizationStatus.denied) {
LocationPromiseService.promiseCompletionHandler.forEach((handler): void => {
handler({success : false, message: "user denied"})
})
}
}
locationManagerDidChangeAuthorization(manager: CLLocationManager) {
}
locationManagerDidPauseLocationUpdates(manager: CLLocationManager) {
}
locationManagerDidResumeLocationUpdates(manager: CLLocationManager) {
}
locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager): boolean {
return true
}
requestPromise(completion: UTSCallback) {
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({success : true, message: "authorized success"})
} 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)
export function requestLocationPromise (completion: UTSCallback) {
locationPromiseService.requestPromise(completion)
}
export function getCurrentWifiInfo (option: GetWifiInfoOptions) {
let arr = CNCopySupportedInterfaces()
if (arr != null) {
let list = arr! as NSArray
let index = 0
let wifiInfo = new CurrentWifiInfo("", "", "")
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.wifiInterface = interfaceName
wifiInfo.SSID = ssid
wifiInfo.BSSID = bssid
break;
}
}
index++
}
if (wifiInfo.BSSID.length > 0 && wifiInfo.SSID.length > 0) {
let res = {
wifiInterface: wifiInfo.wifiInterface,
SSID: wifiInfo.SSID,
BSSID: wifiInfo.BSSID
}
option.success?.(res)
option.complete?.(res)
}else {
option.fail?.({message: "current wifi is null"})
option.complete?.({message: "current wifi is null"})
}
}else {
option.fail?.({message: "current wifi is null"})
option.complete?.({message: "current wifi is null"})
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册