直接上代码!
<button @click="connectWifi">wifi</button>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
connectWifi(){
console.log(123)
var that = this;
//检测手机型号
wx.getSystemInfo({
success: function (res) {
var system = '';
if (res.platform == 'android') system = parseInt(res.system.substr(8));
if (res.platform == 'ios') system = parseInt(res.system.substr(4));
if (res.platform == 'android' && system < 6) {
wx.showToast({
title: '手机版本不支持',
})
return
}
if (res.platform == 'ios' && system < 11.2) {
wx.showToast({
title: '手机版本不支持',
})
return
}
//2.初始化 Wi-Fi 模块
that.startWifi();
}
})
},
//初始化 Wi-Fi 模块
startWifi: function () {
var that = this
wx.startWifi({
success: function () {
//请求成功连接Wifi
that.Connected();
},
fail: function (res) {
wx.showToast({
title: '接口调用失败',
})
}
})
},
Connected: function () {
var that = this
wx.connectWifi({
SSID: '云客秀-2.4G',
BSSID: '********',
password: ''********',',
success: function (res) {
wx.showToast({
title: 'wifi连接成功',
})
},
fail: function (res) {
console.log(res)
if(res.errCode == 12005){
wx.showToast({
title: '未打开 Wi-Fi 开关',
})
return false
}
wx.showToast({
title: 'wifi连接失败',
})
}
})
},
}
}