wifi贴 原理和代码

一、原理

利用微信小程序,连接无线局域网 (Wi-Fi)。

无线局域网 (Wi-Fi) | 微信开放文档

二、代码

以下是代码:                

<button bindtap="scan">扫码</button>

js的代码

scan(){
    let that = this
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
        
        function extractCredentials(inputString) {  
          // 使用正则表达式匹配S和P后面的字符串  
          const sMatch = inputString.match(/S:(.*?);/);  
          const pMatch = inputString.match(/P:(.*?);/);  
        
          // 提取账号和密码  
          const account = sMatch ? sMatch[1] : '';  
          const password = pMatch ? pMatch[1] : '';  
        
          // 返回一个JSON对象  
          return {  
              "acc": account,  
              "pwd": password  
          };  
      }  
        
      // 测试字符串  
      const credentials = extractCredentials(res.result);  
      console.log(credentials)
        that.wifi(credentials.acc, credentials.pwd)

      }
    })
  },

  wifi(name, pwd){
    // let name = '123'
    // let pwd = '1234567890'
    wx.startWifi({
      success (res) {
        console.log(res, '1')

        wx.connectWifi({
          SSID: name,
          password: pwd,
          success (res1) {
            console.log(res1, '2')
          },
          fail(err1){
            console.log(err1, '1-err')
            wx.showModal({
              title: '错误',
              content: err1.errMsg,
              complete: (res) => {
                if (res.cancel) {
                  
                }
            
                if (res.confirm) {
                  
                }
              }
            })
          }
        })
      },
      fail(err){
        console.log(err, '1-err')
        wx.showModal({
          title: '错误',
          content: err.errMsg,
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
      }
    })

  },

暂时只有安卓的,ios的还没有做。需要整体兼容,后面再完善。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值