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设置中连接网络

使用方法:

相关推荐
小恒恒17 小时前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松18 小时前
uniapp使用uni-im
uni-app
2501_9160074721 小时前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063221 小时前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
一颗小青松1 天前
uniapp vue3中app端使用腾讯云点播上传
uni-app·云计算·腾讯云
玄尺_0071 天前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
2501_915106321 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS1 天前
供求求购供应发布VIP会员抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·供求求购供应发布
毕设源码-朱学姐1 天前
【开题答辩全过程】以 基于微信小程序旅游疫情防控管理系统为例,包含答辩的问题和答案
微信小程序·小程序·旅游
weixin_lynhgworld1 天前
陪诊小程序系统开发:开启就医陪伴新体验 [特殊字符]
小程序