一、原理
利用微信小程序,连接无线局域网 (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的还没有做。需要整体兼容,后面再完善。