// pages/deviceConnect3/deviceConnect3.ts const app = getApp(); let xBlufi = require("../../utils/blufi/xBlufi.js"); import route_constant from '../../utils/route_constant.js' import route_util from '../../utils/route_util.js'; let percentIn = null; let errTi = null; Page({ data: { nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '连接配网', //导航栏 中间的标题 }, scopeBluetooth: app.globalData.scopeBluetooth, ssid: app.globalData.ssid, password: app.globalData.pwdData, version: '2.0', name: '', connectedDeviceId: '', connected: true, deviceInfo: null, isInitOK: false, customData: '', percent: 0, ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败 }, onLoad: function (options) { var that = this; that.setData({ name: options.name, connectedDeviceId: options.deviceId, }) xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent); ///通知初始化获取设备的服务列表等信息 xBlufi.notifyInitBleEsp32({ deviceId: options.deviceId, }); percentIn = setInterval(() => { if (that.data.percent === 100) { clearInterval(percentIn); ///失败 that.setData({ ruterStatus: 2 }); return; }; that.data.percent++; that.setData({ percent: that.data.percent }); }, 600); }, // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB","characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}} // {"type":"4","result":false,"data":{"progress":0,"ssid":""}} // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}} funListenDeviceMsgEvent: function (options) { var that = this; switch (options.type) { // {"type":"2","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","serviceId":"0000FFFF-0000-1000-8000-00805F9B34FB", // "characteristicId":"0000FF01-0000-1000-8000-00805F9B34FB"}} case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT: wx.hideLoading(); if (options.result) { console.log('初始化成功'); that.OnClickStart(); } else { console.log('初始化失败') that.setData({ connected: false }) that.setData({ ruterStatus: 2 }); } break; // {"type":"4","result":false,"data":{"progress":0,"ssid":""}} // {"type":"4","result":true,"data":{"progress":100,"ssid":"muzen\u0000"}} ///连接结果 case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT: wx.hideLoading(); if (!options.result) errTi = setTimeout(() => { that.setData({ ruterStatus: 2 }); clearTimeout(errTi); clearInterval(percentIn); }, 5000); else { // 配网成功 if (options.data.progress == 100) { clearTimeout(errTi); clearInterval(percentIn); // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}] that.setData({ percent: 100, ruterStatus: 1, }); // 记住密码 wx.setStorage({ key: "wifiInfo", data: JSON.stringify({ "password": app.globalData.pwdData, "ssid": app.globalData.ssid }), }); var deviceId = that.data.name; var device = { connectType: 3, devName: "", state: "online", name: deviceId, deviceId: deviceId, mac: deviceId, imageUrl: "./../../img/min.png", } getCurrentPages()[0].addWifiDevice(device); } } break; ///设备连接状态回调 case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED: that.setData({ connected: options.result }); if (!options.result) { wx.showModal({ title: '很抱歉提醒你!', content: '小程序与设备异常断开', showCancel: false, //是否显示取消按钮 success: function (res) { wx.navigateBack({ delta: 1 }); }, }) } break; } }, ///校验wifi账号密码 OnClickStart: function () { if (!app.globalData.ssid) { wx.showToast({ title: 'SSID不能为空', icon: 'none' }) return } if (!app.globalData.pwdData) { wx.showToast({ title: '密码不能为空', icon: 'none' }) return; } xBlufi.notifySendRouterSsidAndPassword({ ssid: app.globalData.ssid, password: app.globalData.pwdData }); }, bindPasswordInput: function (e) { var that = this; that.setData({ password: e.detail.value }) }, bindCustomDataInput: function (e) { var that = this; that.setData({ customData: e.detail.value }) }, egen() { wx.navigateBack({ delta: 1 }) }, goIndex() { route_util.goBackRoute(route_constant.indexRoot); }, onUnload: function () { // 关闭蓝牙连接 var that = this xBlufi.notifyConnectBle({ isStart: false, deviceId: that.data.connectedDeviceId, name: that.data.name, }); xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent); clearTimeout(errTi); clearInterval(percentIn); }, })