Wi-Fi功能模块
App平台由 uni ext api 实现,需下载插件:uni-WiFi 链接:https://ext.dcloud.net.cn/plugin?id=10337
uni ext api 需 HBuilderX 3.6.8+
iOS平台获取Wi-Fi信息需要开启“Access WiFi information”能力登录苹果开发者网站,在“Certificates, Identifiers & Profiles”页面选择“Identifiers”中选择对应的App ID,确保开启“Access WiFi information”,生存后重新生成profile文件。
iOS平台iOS13及以上系统,获取当前毗连的Wi-Fi信息需要先获取系统定位权限,因此在iOS13及以上系统使用此接口时,会触发定位权限申请的弹窗。
uni.startWifi(OBJECT)
初始化Wi-Fi模块

uni.stopWifi(OBJECT)
关闭 Wi-Fi 模块
OBJECT 参数说明

uni.getConnectedWifi(OBJECT)
获取已毗连的 Wi-Fi 信息
OBJECT 参数说明

- <template>
- <view class="page">
- <view class="gc-box">
- <button @click="getConnectedWifi">获取当前连接wifi</button>
- </view>
- <view class="gc-box">
- <button @click="getWifiList">获取周围 WiFi</button>
- </view>
- <view class="gc-box">已连接:{{ connectedWifi.SSID }} </view>
- <view class="gc-box">默认连接密码:<input v-model="password" /> </view>
- <view class="gc-box">
- <view v-for="item in wifiList" :key="item.BSSID" class="item">
- <text class="title">{{ item.SSID }}</text>
- <button class="btn" @click="connectWifi(item)">连接</button>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- wifiList: [], // 存储WiFi列表
- connectedWifi: { SSID: '' }, // 存储当前已连接wifi
- password: '', // 密码
- }
- },
- onLoad() {
- this.onWifiConnected()
- this.onGetWifiList()
- },
- methods: {
- /** 启动wifi */
- startWifi() {
- return new Promise((resolve, reject) => {
- uni.startWifi({
- success: (res) => {
- console.log('启动wifi 成功', res)
- resolve(true)
- },
- fail: (err) => {
- console.error('启动wifi 失败', err)
- uni.showModal({ content: err.errMsg, showCancel: false })
- reject(new Error(err))
- },
- })
- })
- },
- /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
- async getWifiList() {
- const hasStart = await this.startWifi()
- if (hasStart !== true) return
- uni.getWifiList({
- success: (res1) => {
- console.log('获取wifi列表命令发送 成功', res1)
- },
- fail: (err) => {
- console.error('获取wifi列表 失败', err)
- uni.showModal({ content: err.errMsg, showCancel: false })
- },
- })
- },
- /** 监听获取wifi列表 */
- onGetWifiList() {
- uni.onGetWifiList((res) => {
- console.log('监听获取wifi列表', res)
- const { wifiList } = res
- // 过滤同名WiFi信号
- const filterList = wifiList.reduce((result, item) => {
- const index = result.findIndex((v) => {
- return v.SSID === item.SSID
- })
- if (index < 0) {
- result.push(item)
- } else if (item.signalStrength > result[index].signalStrength) {
- result[index] = item
- }
- return result
- }, [])
- console.log('过滤同名后', filterList)
- this.wifiList = filterList
- })
- },
- /** 连接某个 Wi-Fi */
- connectWifi(wifi) {
- console.log('选中的wifi:', wifi)
- this.connectedWifi = { SSID: '' }
- uni.connectWifi({
- SSID: wifi.SSID,
- password: this.password,
- success: (res) => {
- console.log('wifi连接命令发送 成功:', res)
- },
- fail: (err) => {
- console.error('wifi连接 失败:', err)
- uni.showModal({ content: err.errMsg, showCancel: false })
- },
- })
- },
- /** 监听wifi连接状态 */
- onWifiConnected() {
- uni.onWifiConnected((res) => {
- console.log('监听wifi连接状态', res)
- this.connectedWifi = res.wifi
- })
- },
- /** 获取当前连接的wifi */
- async getConnectedWifi() {
- this.connectedWifi = { SSID: '' }
- const hasStart = await this.startWifi()
- if (hasStart !== true) return
- uni.getConnectedWifi({
- success: (res) => {
- console.log('获取当前连接的wifi 成功', res)
- this.connectedWifi = res.wifi // 当前连接的wifi的信息
- // this.connectedWifiSSID = res.wifi.SSID
- },
- fail: (err) => {
- console.error('获取当前连接的wifi 失败:', err)
- uni.showModal({ content: err.errMsg, showCancel: false })
- },
- })
- },
- },
- }
- </script>
- <style>
- .item {
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: space-between;
- border-bottom: 2rpx solid #ddd;
- padding: 16rpx 0;
- }
- .item .title {
- flex: 1;
- }
- input {
- border-bottom: 2rpx solid #ddd;
- }
- </style>
复制代码
作者介绍
一个热爱编程,无背景最底层的步伐员。没人领路遇到过很多坑,希望能分享一下经验,让后续的小同伴们少走弯路!希望大家可以多多支持关注!您的肯定是我最大的动力。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |