// pages/setWifi/setWifi.js const { BtHelper } = require("../../devices/bt_helper"); import EventManager from '../../utils/event_bus' import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event'; Page({ data: { wifiName: '', wifiPassword: '', _otaUrl: "", }, getConnectedWifi: function () { const that = this; wx.startWifi({ success(res) { console.log(res.errMsg) 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", text, code) return code }, _int2Hex(num) { let value = num ?? 0; let hexStr = value.toString(16).toUpperCase(); hexStr = hexStr.padStart(2, '0'); let hexNum = parseInt(hexStr, 16); return hexNum; }, 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); console.log("wifiList", wifiList, "pwdList", pwdList) // 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().otaSetWifi(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("设置wifi0:", event) let kind = event.heiJiaoKind; console.log("设置wifi:", name, otaCmd, kind) let toast = "设置wifi:" + name + " value," + otaCmd + "kind ," + kind wx.showToast({ title: toast, icon: 'none' }) }, _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) }, });