小程序
小游戏
企业微信
微信支付
扫描小程序码分享
微信小程序调用API 连接上wifi后,虽然连上但是无法上网
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({
// 3. 监听获取到列表(必须写,否则iOS会报错)
wx.onGetWifiList(() => {
// 4. 现在再连接,绝对不会报 not invoke
// this.doConnect(ssid, pwd, enc);
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: '连接成功' });
wx.showToast({ title: '连接失败:' + err.errMsg, icon: 'none' });
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
本回答由AI生成,可能已过期、失效或不适用于当前情形,请谨慎参考
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
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' });
}
});
}
}