uniapp 小程序获取WiFi列表

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

使用方法:

相关推荐

  1. Uniapp 和Vue3 程序 获取页面dom 方法

    2024-04-12 06:14:09       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 06:14:09       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 06:14:09       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 06:14:09       82 阅读
  4. Python语言-面向对象

    2024-04-12 06:14:09       91 阅读

热门阅读

  1. 数据结构(二)——链表的介绍以及单链表的实现

    2024-04-12 06:14:09       47 阅读
  2. Redis数据持久化的方式

    2024-04-12 06:14:09       71 阅读
  3. 代码随想录训练营-15day:二叉树4

    2024-04-12 06:14:09       34 阅读
  4. Css 和OpenCv.js 多种方式实现图像叠加 / 图像融合

    2024-04-12 06:14:09       34 阅读
  5. vue-指令v-for

    2024-04-12 06:14:09       137 阅读
  6. HTTP状态码大全:常见状态码一网打尽

    2024-04-12 06:14:09       37 阅读
  7. 构建CICD

    2024-04-12 06:14:09       27 阅读
  8. Binary Tree Mock

    2024-04-12 06:14:09       31 阅读
  9. 微服务支持平台--限流算法

    2024-04-12 06:14:09       32 阅读
  10. vue3 问递归算法中解决ajax访问题

    2024-04-12 06:14:09       37 阅读
  11. Bash将输出同时重定向到标准输出stdout和文件

    2024-04-12 06:14:09       38 阅读