当前位置:   article > 正文

uniapp 小程序获取WiFi列表

uniapp 小程序获取WiFi列表
  1. <template>
  2. <view >
  3. <button @click="getWifiList">获取WiFi列表</button>
  4. <scroll-view
  5. :scroll-top="scrollTop"
  6. scroll-y
  7. class="content-pop"
  8. >
  9. <view
  10. class="itemInfo"
  11. v-for="(item, index) in wifiList"
  12. :key="index"
  13. >
  14. <text>{{ item.SSID }}</text>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. wifiList: [],
  24. };
  25. },
  26. methods: {
  27. getWifiList() {
  28. // 获取手机型号
  29. const sys = uni.getSystemInfoSync();
  30. if (sys.platform == "android" && sys.parseInt(sys.system.substr(8) < 6) {
  31. this.$tip.error("手机版本不支持");
  32. return;
  33. }
  34. if (sys.platform == "ios" && sys.parseInt(sys.system.substr(4)) < 11.2) {
  35. this.$tip.error("手机版本不支持");
  36. return;
  37. }
  38. //初始wifi模块
  39. this.start_wifi();
  40. },
  41. start_wifi() {
  42. uni.startWifi({
  43. success: (res) => {
  44. this.getWifi();
  45. },
  46. fail: (err) => {
  47. this.$tip.toast("WIFI启动失败");
  48. },
  49. });
  50. },
  51. getWifi() {
  52. let that = this;
  53. uni.getWifiList({
  54. //成功后,就可以获取列表了
  55. success(res) {
  56. //列表获取成功后,要到事件里提取
  57. uni.onGetWifiList((res) => {
  58. that.onGetWifiListFun(res);
  59. });
  60. },
  61. fail(err) {
  62. console.log(err, "err");
  63. },
  64. });
  65. },
  66. // 监听获取到的WiFi列表函数 进行处理
  67. onGetWifiListFun(info) {
  68. this.wifiList = [];
  69. let wifiList = [];
  70. // 过滤掉没有SSID的 还有我个人对信号的处理,用来渲染对应的样式的
  71. wifiList = info.wifiList.filter((item) => {
  72. if (item.signalStrength >= -55) {
  73. item["signalDefine"] = [true, true, true];
  74. } else if (item.signalStrength >= -66) {
  75. item["signalDefine"] = [false, true, true];
  76. } else if (item.signalStrength >= -88) {
  77. item["signalDefine"] = [false, false, true];
  78. } else if (item.signalStrength >= -100) {
  79. item["signalDefine"] = [false, false, false];
  80. }
  81. return item.SSID;
  82. });
  83. // 按信号强度排序
  84. wifiList = wifiList.sort((a, b) => {
  85. return b.signalStrength - a.signalStrength;
  86. });
  87. // 取当前连接的WiFi的信号
  88. const connectResIndex = wifiList.findIndex(
  89. (item) => item.SSID === this.connectWifi
  90. );
  91. const connectRes = wifiList[connectResIndex];
  92. if (connectRes) {
  93. this.connectWifiSignal = connectRes["signalDefine"];
  94. this.connectRes = connectRes;
  95. }
  96. wifiList.splice(connectResIndex, 1);
  97. this.wifiList = this.uniqueByField(wifiList, "SSID");
  98. this.$tip.loaded();
  99. },
  100. uniqueByField(list, field) {
  101. const seen = new Set();
  102. const result = [];
  103. for (let item of list) {
  104. const value = item[field];
  105. if (!seen.has(value)) {
  106. seen.add(value);
  107. result.push(item);
  108. }
  109. }
  110. return result;
  111. },
  112. },
  113. };
  114. </script>
  115. <style scoped>
  116. .itemInfo {
  117. height: 96rpx;
  118. line-height: 96rpx;
  119. font-size: 28rpx;
  120. border-bottom: 1rpx solid #f4f4f4;
  121. text-align: left;
  122. }
  123. .content-pop {
  124. width: 100vw;
  125. max-height: 55vh;
  126. padding: 0 30rpx;
  127. }
  128. </style>

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

使用方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/413727
推荐阅读
相关标签
  

闽ICP备14008679号