123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- // pages/deviceConnect3/deviceConnect3.ts
- const app = getApp();
- let xBlufi = require("../../utils/blufi/xBlufi.js");
- import strings from '../../utils/strings.js';
- import route_util from '../../utils/route_util.js';
- import route_constant from '../../utils/route_constant.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失败
- imageUrl: "./../../img/g.png",
- },
- onLoad: function (options) {
- var that = this;
- var param = options.param;
- if (!strings.isEmpty(param)) {
- param = JSON.parse(param);
- that.setData({
- imageUrl: param.img,
- });
- }
- // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
- 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;
- console.log("gadfqwerqwerqr==1111==" + options.type + "===" + JSON.stringify(options));
- 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:
- console.log("gadfqwerqwerqr==222==" + JSON.stringify(options.result));
- 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:
- console.log("gadfqwerqwerqr==2222==" + JSON.stringify(options.result));
- if (!options.result) {
- errTi = setTimeout(() => {
- that.setData({
- ruterStatus: 2
- });
- clearTimeout(errTi);
- clearInterval(percentIn);
- }, 5000);
- } else {
- // 配网成功
- if (options.data.progress == 100) {
- var deviceId = that.data.name;
- var device = {
- connectType: 3,
- devName: "",
- state: "online",
- name: deviceId,
- deviceId: deviceId,
- mac: deviceId,
- imageUrl: that.data.imageUrl,
- // imageUrl: "./../../img/min.png",
- }
- getCurrentPages()[0].addWifiDevice(device);
- // [{"deviceId":"BLUFI_7cdfa1fcbb24","name":"BLUFI_7cdfa1fcbb24","state":"online"}]
- // 记住密码
- wx.setStorage({
- key: "wifiInfo",
- data: JSON.stringify({
- "password": app.globalData.pwdData,
- "ssid": app.globalData.ssid
- }),
- });
- setTimeout(() => {
- clearTimeout(errTi);
- clearInterval(percentIn);
- that.setData({
- percent: 100,
- ruterStatus: 1,
- });
- }, 500);
- }
- }
- break;
- ///设备连接状态回调
- case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
- console.log("gadfqwerqwerqr==4444==" + JSON.stringify(options.result));
- 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);
- },
- })
|