使用uniapp实现小步伐获取wifi并毗连

打印 上一主题 下一主题

主题 553|帖子 553|积分 1659

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 参数说明

  1. <template>
  2.   <view class="page">
  3.     <view class="gc-box">
  4.       <button @click="getConnectedWifi">获取当前连接wifi</button>
  5.     </view>
  6.     <view class="gc-box">
  7.       <button @click="getWifiList">获取周围 WiFi</button>
  8.     </view>
  9.     <view class="gc-box">已连接:{{ connectedWifi.SSID }} </view>
  10.     <view class="gc-box">默认连接密码:<input v-model="password" /> </view>
  11.     <view class="gc-box">
  12.       <view v-for="item in wifiList" :key="item.BSSID" class="item">
  13.         <text class="title">{{ item.SSID }}</text>
  14.         <button class="btn" @click="connectWifi(item)">连接</button>
  15.       </view>
  16.     </view>
  17.   </view>
  18. </template>
  19. <script>
  20. export default {
  21.   data() {
  22.     return {
  23.       wifiList: [], // 存储WiFi列表
  24.       connectedWifi: { SSID: '' }, // 存储当前已连接wifi
  25.       password: '', // 密码
  26.     }
  27.   },
  28.   onLoad() {
  29.     this.onWifiConnected()
  30.     this.onGetWifiList()
  31.   },
  32.   methods: {
  33.     /** 启动wifi */
  34.     startWifi() {
  35.       return new Promise((resolve, reject) => {
  36.         uni.startWifi({
  37.           success: (res) => {
  38.             console.log('启动wifi 成功', res)
  39.             resolve(true)
  40.           },
  41.           fail: (err) => {
  42.             console.error('启动wifi 失败', err)
  43.             uni.showModal({ content: err.errMsg, showCancel: false })
  44.             reject(new Error(err))
  45.           },
  46.         })
  47.       })
  48.     },
  49.     /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
  50.     async getWifiList() {
  51.       const hasStart = await this.startWifi()
  52.       if (hasStart !== true) return
  53.       uni.getWifiList({
  54.         success: (res1) => {
  55.           console.log('获取wifi列表命令发送 成功', res1)
  56.         },
  57.         fail: (err) => {
  58.           console.error('获取wifi列表 失败', err)
  59.           uni.showModal({ content: err.errMsg, showCancel: false })
  60.         },
  61.       })
  62.     },
  63.     /** 监听获取wifi列表 */
  64.     onGetWifiList() {
  65.       uni.onGetWifiList((res) => {
  66.         console.log('监听获取wifi列表', res)
  67.         const { wifiList } = res
  68.         // 过滤同名WiFi信号
  69.         const filterList = wifiList.reduce((result, item) => {
  70.           const index = result.findIndex((v) => {
  71.             return v.SSID === item.SSID
  72.           })
  73.           if (index < 0) {
  74.             result.push(item)
  75.           } else if (item.signalStrength > result[index].signalStrength) {
  76.             result[index] = item
  77.           }
  78.           return result
  79.         }, [])
  80.         console.log('过滤同名后', filterList)
  81.         this.wifiList = filterList
  82.       })
  83.     },
  84.     /** 连接某个 Wi-Fi */
  85.     connectWifi(wifi) {
  86.       console.log('选中的wifi:', wifi)
  87.       this.connectedWifi = { SSID: '' }
  88.       uni.connectWifi({
  89.         SSID: wifi.SSID,
  90.         password: this.password,
  91.         success: (res) => {
  92.           console.log('wifi连接命令发送 成功:', res)
  93.         },
  94.         fail: (err) => {
  95.           console.error('wifi连接 失败:', err)
  96.           uni.showModal({ content: err.errMsg, showCancel: false })
  97.         },
  98.       })
  99.     },
  100.     /**  监听wifi连接状态 */
  101.     onWifiConnected() {
  102.       uni.onWifiConnected((res) => {
  103.         console.log('监听wifi连接状态', res)
  104.         this.connectedWifi = res.wifi
  105.       })
  106.     },
  107.     /** 获取当前连接的wifi */
  108.     async getConnectedWifi() {
  109.       this.connectedWifi = { SSID: '' }
  110.       const hasStart = await this.startWifi()
  111.       if (hasStart !== true) return
  112.       uni.getConnectedWifi({
  113.         success: (res) => {
  114.           console.log('获取当前连接的wifi 成功', res)
  115.           this.connectedWifi = res.wifi // 当前连接的wifi的信息
  116.           // this.connectedWifiSSID = res.wifi.SSID
  117.         },
  118.         fail: (err) => {
  119.           console.error('获取当前连接的wifi 失败:', err)
  120.           uni.showModal({ content: err.errMsg, showCancel: false })
  121.         },
  122.       })
  123.     },
  124.   },
  125. }
  126. </script>
  127. <style>
  128. .item {
  129.   display: flex;
  130.   flex-direction: row;
  131.   align-items: center;
  132.   justify-content: space-between;
  133.   border-bottom: 2rpx solid #ddd;
  134.   padding: 16rpx 0;
  135. }
  136. .item .title {
  137.   flex: 1;
  138. }
  139. input {
  140.   border-bottom: 2rpx solid #ddd;
  141. }
  142. </style>
复制代码

作者介绍
一个热爱编程,无背景最底层的步伐员。没人领路遇到过很多坑,希望能分享一下经验,让后续的小同伴们少走弯路!希望大家可以多多支持关注!您的肯定是我最大的动力。


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

尚未崩坏

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表