123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // pages/setWifi/setWifi.js
- const { BtHelper } = require("../../devices/bt_helper");
- import EventManager from '../../utils/event_bus'
- import { EnumCmdEvent, CmdEvent } from '../../devices/cmd_key_event';
- import store from '../../utils/store';
- import { BtCmd } from '../../devices/bluetooth/bt_cmd';
- Page({
- data: {
- wifiName: '',
- wifiPassword: '',
- _otaUrl: "",
- },
- getConnectedWifi: function () {
- const that = this;
- let wifiDic = store.getStore("wifiInfo")
- let wifiName = wifiDic.wifiName
- let pwd = wifiDic.wifiPassword
- if (wifiName && pwd) {
- console.log("获取Wi-Fi信息", wifiName, pwd);
- that.setData({
- wifiName: wifiName,
- wifiPassword: pwd
- });
- return;
- }
- 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
- });
- },
- 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 = BtCmd.sendWiFiInfo(wifiName, pwd)
- // _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;
- let otaCmd = event.otaCmd
- 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'
- })
- if (name === EnumCmdEvent.otaWifi && value === 1) {
- store.setStore("wifiInfo", {
- wifiName: _this.data.wifiName,
- wifiPassword: _this.data.wifiPassword
- })
- }
- }, _this)
- },
- onLoad: function (options) {
- let param = options.param ?? "{}";
- let url = JSON.parse(param).url ?? "";
- this.data._otaUrl = url;
- this.getConnectedWifi();
- this.addNotification()
- },
- onUnload() {
- EventManager.removeNotification(CmdEvent.eventName)
- },
- });
|