uniapp 小程序获取WiFi列表

javascript 复制代码
<template>
  <view >
    <button @click="getWifiList">获取WiFi列表</button>
    <scroll-view
      :scroll-top="scrollTop"
      scroll-y
      class="content-pop"
    >
      <view
        class="itemInfo"
        v-for="(item, index) in wifiList"
        :key="index"
      >
        <text>{{ item.SSID }}</text>
      </view>
    </scroll-view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        wifiList: [],
      };
    },
    methods: {
      getWifiList() {
        // 获取手机型号
        const sys = uni.getSystemInfoSync();
        if (sys.platform == "android" && sys.parseInt(sys.system.substr(8) < 6) {
          this.$tip.error("手机版本不支持");
          return;
        }
        if (sys.platform == "ios" && sys.parseInt(sys.system.substr(4)) < 11.2) {
          this.$tip.error("手机版本不支持");
          return;
        }
        //初始wifi模块
        this.start_wifi();
      },
      start_wifi() {
        uni.startWifi({
          success: (res) => {
              this.getWifi();
          },
          fail: (err) => {
            this.$tip.toast("WIFI启动失败");
          },
        });
      },
      getWifi() {
        let that = this;
        uni.getWifiList({
          //成功后,就可以获取列表了
          success(res) {
            //列表获取成功后,要到事件里提取
            uni.onGetWifiList((res) => {
              that.onGetWifiListFun(res);
            });
          },
          fail(err) {
            console.log(err, "err");
          },
        });
      },
      // 监听获取到的WiFi列表函数 进行处理
      onGetWifiListFun(info) {
        this.wifiList = [];
        let wifiList = [];
        // 过滤掉没有SSID的 还有我个人对信号的处理,用来渲染对应的样式的
        wifiList = info.wifiList.filter((item) => {
          if (item.signalStrength >= -55) {
            item["signalDefine"] = [true, true, true];
          } else if (item.signalStrength >= -66) {
            item["signalDefine"] = [false, true, true];
          } else if (item.signalStrength >= -88) {
            item["signalDefine"] = [false, false, true];
          } else if (item.signalStrength >= -100) {
            item["signalDefine"] = [false, false, false];
          }
          return item.SSID;
        });
        // 按信号强度排序
        wifiList = wifiList.sort((a, b) => {
          return b.signalStrength - a.signalStrength;
        });
        // 取当前连接的WiFi的信号
        const connectResIndex = wifiList.findIndex(
          (item) => item.SSID === this.connectWifi
        );
        const connectRes = wifiList[connectResIndex];
        if (connectRes) {
          this.connectWifiSignal = connectRes["signalDefine"];
          this.connectRes = connectRes;
        }
        wifiList.splice(connectResIndex, 1);

        this.wifiList = this.uniqueByField(wifiList, "SSID");
        this.$tip.loaded();
      },
      uniqueByField(list, field) {
        const seen = new Set();
        const result = [];

        for (let item of list) {
          const value = item[field];
          if (!seen.has(value)) {
            seen.add(value);
            result.push(item);
          }
        }

        return result;
      },
    },
  };
</script>

<style scoped>
  .itemInfo {
    height: 96rpx;
    line-height: 96rpx;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f4f4f4;
    text-align: left;
  } 
  .content-pop {
    width: 100vw;
    max-height: 55vh;
    padding: 0 30rpx;
  }
</style>

提示:iphone刷新会跳转至手机wifi设置中连接网络

使用方法:

相关推荐
jingling5551 小时前
uniapp | 快速上手ThorUI组件
前端·笔记·前端框架·uni-app
老华带你飞3 小时前
畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·畅阅读系统小程序
百思可瑞教育8 小时前
uni-app 根据用户不同身份显示不同的tabBar
vue.js·uni-app·北京百思可瑞教育·北京百思教育
老华带你飞10 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
项目題供诗10 小时前
微信小程序开发教程(八)
微信小程序·小程序
Q_Q196328847514 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php
百思可瑞教育16 小时前
使用UniApp实现一个AI对话页面
javascript·vue.js·人工智能·uni-app·xcode·北京百思可瑞教育·百思可瑞教育
不想吃饭e17 小时前
在uniapp/vue项目中全局挂载component
前端·vue.js·uni-app
00后程序员张17 小时前
iOS App 混淆与资源保护:iOS配置文件加密、ipa文件安全、代码与多媒体资源防护全流程指南
android·安全·ios·小程序·uni-app·cocoa·iphone
低代码布道师17 小时前
少儿舞蹈小程序(12)作品列表查询搭建
低代码·小程序