收藏
回答

微信小程序调用API 连接上wifi后,虽然连上但是无法上网

问题类型 API/组件名称 微信版本 基础库版本
Bug wifi 8.0.71 3.16.0

微信小程序调用API 连接上wifi后,虽然连上但是无法上网

回答关注问题邀请回答
收藏

2 个回答

  • 张美军
    张美军
    2小时前

    test(){


      wx.getSetting({


        success(res) {


          console.log('11-')


          if (!res.authSetting['scope.userLocation']) {


            console.log('22-')


            wx.authorize({


              scope: 'scope.userLocation',


              fail() {


                wx.showToast({ title: '需要授权位置权限才能连接WiFi', icon: 'none' });


              }


            })


          }


        }


      });




      wx.startWifi({


        success: () => {


          console.log("startWifi 成功");




        },


        fail: (err) => {


          console.log(err);


          wx.showToast({ title: '请开启WiFi和定位权限'+err, icon: 'none' });


        }


      });




           // 2. 【关键】必须先获取WiFi列表,才能连接


           wx.getWifiList({


            success: () => {


              // 3. 监听获取到列表(必须写,否则iOS会报错)


              wx.onGetWifiList(() => {


                // 4. 现在再连接,绝对不会报 not invoke


               // this.doConnect(ssid, pwd, enc);


               


              });


            },


            fail: (err) => {


              console.log(err);


              wx.showToast({ title: '请开启WiFi和定位权限'+err, icon: 'none' });


            }


          });




      wx.scanCode({


      success: (res) => {


        // 二维码内容:WIFI:S:4017;T:WPA;P:12348765;;


        const wifiStr = res.result;


        if (!wifiStr.startsWith('WIFI:')) {


          wx.showToast({ title: '非WiFi二维码', icon: 'none' });


          return;


        }


        // 解析SSID、加密类型、密码


        const ssid = wifiStr.match(/S:(.*?);/)[1];


        const type = wifiStr.match(/T:(.*?);/)[1];


        const password = wifiStr.match(/P:(.*?);/)[1] || '';






         // 调用连接WiFi方法


         this.connectWifi(ssid, password, type);


      }


    });




    console.log(11111111111111111111111111)


    },


    connectWifi(ssid, password, type) {


      wx.connectWifi({


         SSID: ssid,


         password: password,


         encryption:type, 


         suggestion:true,


        success(res){


          wx.showLoading({ title: '正在连接...' });


          // 监听连接成功


          wx.onWifiConnected((res) => {


            wx.hideLoading();


            wx.showToast({ title: '连接成功' });


          });


        },


        fail: (err) => {


          wx.showToast({ title: '连接失败:' + err.errMsg, icon: 'none' });


        }


      });


    }


    }

    2小时前
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    2小时前
    有用
    回复
登录 后发表内容