当前位置:网站首页>Use of WiFi module based on wechat applet

Use of WiFi module based on wechat applet

2022-04-23 14:04:00 one billion twenty-nine million one hundred and seventy-nine th

This article is mainly written based on wechat applet wifi Module USES , Mainly write 4 Features ,wifi The initialization 、 Get the surrounding wifi list 、 Connect wifi And get a connection wifi Basic information of .
1、wifi The initialization ,wifi Before using the module , Must be initialized .
2、 Get the surrounding wifi list , Get... Near you wifi Basic information of the list ,wifi name ,mac Address , Basic information such as signal strength , Through these, we can locate .
3、 Connect wifi, know wifi Account and password can connect .
4、 Get the connection wifi Basic information of , You don't need an account and password to get , And the connection above wifi It doesn't matter. . What you get is that the mobile phone is connected wifi.
Here is the specific code
(1)wxml


<view class="scan">
    <view class="time-section">
        <view class="time">
            <view class="hourminuts">
                <text>{
   {hours}}:{
   {minutes}}</text>
            </view>
            <view class="seconds">
                <text>{
   {seconds}}</text>
            </view>
        </view>
        <view class="date">
            <text>{
   {month}} month {
   {day}} Japan   {
   {week}}</text>
        </view>
        
    </view>
    <view class="footer">
        <button class="to-clock" hover-class="hover-to-clock" bindtap="toClock"> initialization wifi</button>
        <button class="to-clock" hover-class="hover-to-clock" bindtap="toClock1">WiFi The list of data </button>
        <button class="to-clock" hover-class="hover-to-clock" bindtap="toClock2"> Connect wifi</button>
        <button class="to-clock" hover-class="hover-to-clock" bindtap="toClock3"> Get connected wifi Information </button>
    </view>

     <view class="date">
            <text>{
   {count4}}</text>
        </view>
</view>

(2)css

/* pages/scan/scan.wxss */

page {
  background-color: #fafafa;
}




.name {
  margin-top: 20rpx; 
  margin-bottom: 40rpx;
line-height: 32px;
font-size: 17.25pt;
color: #000000;
}

.time-section {
  margin-top: 110rpx;

}

.time {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.hourminuts {
  line-height: 154rpx;
  font-size: 41.25pt;
  color: rgba(0, 0, 0, 0.87);

}

.seconds {
  line-height: 72rpx;
  font-size: 18.75pt;
  color: #f5a623;
  margin: auto 0rpx 20rpx 0rpx;
}

.date {
  margin-top: 12rpx;
  text-align: center;
  font-weight: 500;
  line-height: 22px;
  font-size: 12pt;
  color: rgba(0, 0, 0, 0.54);
  margin-bottom: 120rpx; 
}

.footer {
  /*display: flex;
  flex-direction: column;
  justify-content: center;*/
  /*align-items: center;*/
  margin: 0 auto;
  width: 80%;
}

.to-clock {
  background-color: #22a1e0;
  color: #ffffff;
  margin-bottom: 40rpx; 
}

.hover-to-clock {
  opacity: 0.7;
}

.to-list {
  color: rgba(0, 0, 0, 0.54);
  margin-bottom: 40rpx;
}

.hover-to-list {
  opacity: 0.7;
}





(3)js Code
1)wifi initialization

  // initialization wifi
  toClock(){
    wx.startWifi({
      success (res) {
        console.log(" initialization wifi Module success "+res.errMsg)
      }
    })
  },

2) Get the surrounding wifi list

 //wifi Data list    Get the surrounding wifi list 
  toClock1(){
    wx.getWifiList({
      success:function(res){
      //   console.log(" To obtain a list of "+res);
      //   console.log(" To obtain a list of "+res.wifi);
        wx.onGetWifiList(function (res) {
            var len=res.wifiList.length
            for(var i=0;i<len;i++){
                console.log(res.wifiList[i])
            }
          console.log(res.wifiList[0])// Other business codes 
      })
      },
      complete: (res) => {
        
      },
    })
  },

3) Connect wifi

 // Connect wifi
  toClock2(){
    var pass="Xys147258369."
    var zh="XYS1"
      wx.connectWifi({
        SSID:zh,
        password:pass,
        success (res) {
          console.log(" Connect wifi success ")
        },
        fail(res){
          console.log(" The connection fails "+res.errCode+"  "+res.errMsg);
        }
      })
  },

4) Get the connection wifi essential information ( account number 、 Password and signal strength )

  // Get connected wifi Information 
  toClock3(){
    var that = this;
    wx.getConnectedWifi({
      success(res){
        console.log(" Successfully get connected wifi Information ")
        console.log(res.wifi.SSID+" "+res.wifi.BSSID+" "+res.wifi.secure+" "+res.wifi.signalStrength);
        var wifi = res.wifi;
        that.setData({
          mySSID:wifi.SSID,
          myBSSID:wifi.BSSID,
          mysecure:wifi.secure,
          mysignalStrength:wifi.signalStrength,
        })
      },
      complete: (res) => {
        console.log(" Getting information ")
      },
    })
  },

Search for WeChat official account : The small white XBIT
Reply key :wifi
Other keywords :ibeacon、 QR code access control
 Insert picture description here

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401298212.html