直接上代碼!
<button @click="connectWifi">wifi</button>
export default {
data() {
return {
title: 'Hello'
}
},
onLoad() {
},
methods: {
connectWifi(){
console.log(123)
var that = this;
//檢測手機(jī)型號
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: '手機(jī)版本不支持',
})
return
}
if (res.platform == 'ios' && system < 11.2) {
wx.showToast({
title: '手機(jī)版本不支持',
})
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: '接口調(diào)用失敗',
})
}
})
},
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 開關(guān)',
})
return false
}
wx.showToast({
title: 'wifi連接失敗',
})
}
})
},
}
}