diff --git a/pages/SystemAPI/WifiInfomation/wifiInfomation.vue b/pages/SystemAPI/WifiInfomation/wifiInfomation.vue index fb97f086c200e3c1856ed5c055b838f6ee8ed45e..9d338ae7332420eb69d9f10c41b44730482fe743 100644 --- a/pages/SystemAPI/WifiInfomation/wifiInfomation.vue +++ b/pages/SystemAPI/WifiInfomation/wifiInfomation.vue @@ -27,14 +27,15 @@ /* iOS12之后获取wifi信息需要获取系统定位权限,否则获取到的wifi信息为空 */ requestLocationPromise(function(res) { if (res["success"] == true) { - let wifiInfo = getCurrentWifiInfo() - if (wifiInfo != null) { - console.log(wifiInfo) - uni.showToast({ - title: "当前wifi信息获取成功", - icon: 'none' - }) - } + getCurrentWifiInfo({ + success:function(res){ + console.log(res) + uni.showToast({ + title: `当前wifi信息: interface: ${res.wifiInterface}, SSID: ${res.SSID}, BSSID: ${res.BSSID}` , + icon: 'none' + }) + } + }) } }) }, diff --git a/uni_modules/uni-wifiinformation/readme.md b/uni_modules/uni-wifiinformation/readme.md index b364844ee9c4fd64ff003ddb289038501017d994..95863ce34140b0d3fcc274ef711b966a76ea25c1 100644 --- a/uni_modules/uni-wifiinformation/readme.md +++ b/uni_modules/uni-wifiinformation/readme.md @@ -1,5 +1,83 @@ # uni-currentwifiinformation -### 开发文档 -[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html) -[UTS 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html) -[Hello UTS](https://gitcode.net/dcloud/hello-uts/-/tree/dev) \ No newline at end of file + +## 使用文档 + +```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|描述信息| diff --git a/uni_modules/uni-wifiinformation/utssdk/app-ios/index.uts b/uni_modules/uni-wifiinformation/utssdk/app-ios/index.uts index 6f9bf6c6e59739e18fa4b57ba0f60fbcd5225574..b24ff6b71078e97259423602dbb2f9e4bcfb0cc6 100644 --- a/uni_modules/uni-wifiinformation/utssdk/app-ios/index.uts +++ b/uni_modules/uni-wifiinformation/utssdk/app-ios/index.uts @@ -3,6 +3,16 @@ import { CaptiveNetwork, kCNNetworkInfoKeySSID, kCNNetworkInfoKeyBSSID } from 'S 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 = "" @@ -89,13 +99,12 @@ export function requestLocationPromise (completion: UTSCallback) { locationPromiseService.requestPromise(completion) } -export function getCurrentWifiInfo (): CurrentWifiInfo | null { +export function getCurrentWifiInfo (option: GetWifiInfoOptions) { let arr = CNCopySupportedInterfaces() if (arr != null) { let list = arr! as NSArray - let wifiInfo = new CurrentWifiInfo("", "", "") let index = 0 - let array = [1, 2, 3] + let wifiInfo = new CurrentWifiInfo("", "", "") while (index < list.count) { let item = list[index] let interfaceName = item as string @@ -118,9 +127,20 @@ export function getCurrentWifiInfo (): CurrentWifiInfo | null { } if (wifiInfo.BSSID.length > 0 && wifiInfo.SSID.length > 0) { - return wifiInfo + 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"}) } - return null }