// pages/setWifi/setWifi.js const { BtHelper } = require("../../devices/bt_helper"); Page({ data: { wifiName: '', wifiPassword: '', _otaUrl: "", }, getConnectedWifi: function () { const that = this; wx.getConnectedWifi({ success: function (res) { const wifiName = res.wifi.SSID; that.setData({ wifiName: wifiName }); }, fail: function (err) { console.error('获取Wi-Fi信息失败', err); // wx.showToast({ // title: '获取Wi-Fi信息失败', // icon: 'none' // }); } }); }, onWifiNameInput: function (e) { this.setData({ wifiName: e.detail.value }); }, onWifiPasswordInput: function (e) { this.setData({ wifiPassword: e.detail.value }); }, // string转换为List string2ListInt(text) { let code = Array.from(text).map(char => char.charCodeAt(0)); console.log("string转换为List", code) return code }, sendWiFiInfo(wifiName, pwd) { // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)]; if (!wifiName || !pwd) { wx.showToast({ title: '请输入正确的账号密码', }) wx.hideLoading() return; } let result = []; // 字母*6 + let wifiList = this.string2ListInt(wifiName); // 数字*3 + let pwdList = this.string2ListInt(pwd); // 16进制 result.push(0x22); result.push(this.int2Hex(wifiList.length + pwdList.length + 6)); // 账号 result.push(0x33); result.push(this.int2Hex(wifiList.length)); let p = result[3] + 4; let j = 0; for (let i = 4; i < p; i++) { result.splice(i, 0, wifiList[j++]); } // 密码 result.splice(p, 0, 0x44); result.splice(++p, 0, this.int2Hex(pwdList.length)); p++; j = 0; // for (let i = p; i < p + pwdList.length; i++) { // result.splice(i, 0, pwdList[j++]); // } result.push(...pwdList) console.log("发送wifi账号密码:", result.toString()); // _ble.send({ cmd: result }); BtHelper.getInstance().send(result) }, onConfirm: function () { const { wifiName, wifiPassword } = this.data; if (!wifiName || !wifiPassword) { wx.showToast({ title: '请输入完整的Wi-Fi信息', icon: 'none' }); return; } // 这里可以添加进一步的逻辑,比如连接Wi-Fi或保存信息 this.sendWiFiInfo(wifiName, wifiPassword); }, addNotification() { let _this = this; EventManager.addNotification(CmdEvent.eventName, function (event) { let name = event.cmdEvent; console.log("OTA页0:", event) let kind = event.heiJiaoKind; switch (name) { case EnumCmdEvent.otaCmd: let otaCmd = event.otaCmd; console.log("OTA页:", otaCmd, kind) if (otaCmd === 1 && kind == 1) { wx.hideLoading(); // 设备收到开启OTA的回复,发送url _this.sendUrlData() } else if (otaCmd === 2 && kind == 1) { wx.hideLoading(); // 去设置wifi界面 _this.goToWifi() } else if (otaCmd === 0 && kind == 1) { // 设备回收到url,OTA结束了 // _this.sendOtaCmd(0) wx.hideLoading() wx.showModal({ title: '等待设备升级中', showCancel: false }) } else if (kind == 0) { wx.hideLoading() wx.showModal({ title: 'WIFI连接失败了', showCancel: false }) } break; case EnumCmdEvent.otaUrl: let otaUrl = event.otaUrl; if (otaUrl === 1) { // 开始发送url BtHelper.getInstance().otaUrl(_this.string2ListInt(_this.data.otaData.url)) } else { wx.hideLoading() // wifi失败 wx.showModal({ title: 'OTA升级失败了', showCancel: false }) } break; } }, _this) }, onLoad: function (options) { let param = options.param; let url = JSON.parse(param).url ?? ""; this.data._otaUrl = url; this.getConnectedWifi(); this.addNotification() }, onUnload: function () { EventManager.removeNotification(CmdEvent.eventName) }, });