Bläddra i källkod

feature: 整理wifi配网

Damon 7 månader sedan
förälder
incheckning
5cbb2ac8a5

+ 8 - 8
app.js

@@ -234,21 +234,21 @@ App({
 
   // 获取蓝牙权限
   getBluetoothStatus() {
-    const _this = this;
+    var that = this;
     wx.getSetting({
       success(res) {
         if (res.authSetting["scope.bluetooth"]) {
-          _this.globalData.scopeBluetooth = true;
+          that.globalData.scopeBluetooth = true;
         } else if (res.authSetting["scope.bluetooth"] === undefined) {
-          _this.globalData.scopeBluetooth = false;
+          that.globalData.scopeBluetooth = false;
           wx.authorize({
             scope: "scope.bluetooth",
             complete() {
-              _this.getBluetoothStatus();
+              that.getBluetoothStatus();
             }
           });
         } else {
-          _this.globalData.scopeBluetooth = false;
+          that.globalData.scopeBluetooth = false;
           wx.showModal({
             title: '请打开系统蓝牙进行配网',
             content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
@@ -257,7 +257,7 @@ App({
                 console.log('用户点击确定')
                 wx.openSetting({
                   complete() {
-                    // _this.getBluetoothStatus();
+                    // that.getBluetoothStatus();
                   }
                 })
               } else if (res.cancel) {
@@ -266,8 +266,9 @@ App({
             }
           })
         };
+
         if (getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck) {
-          getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(_this.globalData.scopeBluetooth);
+          getCurrentPages()[getCurrentPages().length - 1].getBluetoothStatusCallck(that.globalData.scopeBluetooth);
         }
         // res.authSetting = {
         //   "scope.userInfo": true,
@@ -276,7 +277,6 @@ App({
       }
     })
   },
-
 })
 
 

+ 44 - 90
pages/deviceConnect0/deviceConnect0.js

@@ -1,37 +1,25 @@
-// pages/deviceConnect0/deviceConnect0.ts
 const app = getApp();
 import route_constant from '../../utils/route_constant.js'
 import route_util from '../../utils/route_util.js';
 
 Page({
-
-  /**
-   * 页面的初始数据
-   */
   data: {
-    scopeBluetooth: false,
-    userFuzzyLocation: true,
     nvabarData: {
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
       title: '连接配网', //导航栏 中间的标题
-      callback() {
-        wx.navigateBack({
-          delta: 1,
-        })
-      }
     },
+    scopeBluetooth: false,
+    userFuzzyLocation: true,
     isNotruter: false
   },
-  onShow() {},
-  /**
-   * 生命周期函数--监听页面加载
-   */
+
   onLoad(options) {
-    wx.offAppShow(this.getSetting);
-    wx.onAppShow(this.getSetting);
-    this.getSetting();
+    var that = this;
+    wx.offAppShow(that.getSetting);
+    wx.onAppShow(that.getSetting);
+    that.getSetting();
     if (options.isLognlink) {
-      this.setData({
+      that.setData({
         isNotruter: true,
         nvabarData: {
           showCapsule: 1,
@@ -44,57 +32,60 @@ Page({
     }
   },
 
-  getBluetoothStatusCallck(v) {
-    this.setData({
-      scopeBluetooth: v
-    })
-  },
+  ///获取定位服务
   getSetting() {
-    const _this = this;
+    var that = this;
     wx.getSetting({
       success(res) {
         if (res.authSetting["scope.userFuzzyLocation"]) {
-          // 成功
           app.getBluetoothStatus();
-          _this.setData({
+          that.setData({
             userFuzzyLocation: true,
           });
         } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
-
+          ///请求定位权限
           wx.authorize({
             scope: "scope.userFuzzyLocation",
             success() {
-              _this.getSetting();
+              that.getSetting();
             }
           });
         } else {
-          _this.setData({
+          that.setData({
             userFuzzyLocation: false,
           });
           wx.showModal({
             title: '请打开系统位置获取',
             success(res) {
               if (res.confirm) {
-                console.log('用户点击确定')
                 wx.openSetting({
                   complete() {
-                    // _this.getSetting();
+                    // that.getSetting();
                   }
                 })
-              } else if (res.cancel) {
-                console.log('用户点击取消');
-              }
+              } else if (res.cancel) {}
             }
-          })
+          });
         }
       }
     })
   },
+
+  ///检测蓝牙权限回调 true:false
+  getBluetoothStatusCallck(v) {
+    var that = this;
+    that.setData({
+      scopeBluetooth: v
+    });
+  },
+
+  ///下一步
   next() {
-    const _this = this;
+    var that = this;
+    ///初始化蓝牙
     wx.openBluetoothAdapter({
       success(res) {
-        if (_this.data.scopeBluetooth && _this.data.userFuzzyLocation) {
+        if (that.data.scopeBluetooth && that.data.userFuzzyLocation) {
           wx.navigateTo({
             url: './../deviceConnect1/deviceConnect1',
           });
@@ -111,71 +102,34 @@ Page({
       }
     });
   },
-  setBluetooth() {
+
+  ///手动开启定位权限
+  setUserFuzzyLocation() {
+    const that = this;
     wx.openSetting({
       complete() {
-        // app.getBluetoothStatus();
+        // that.getSetting();
       }
     })
   },
-  setUserFuzzyLocation() {
-    const _this = this;
+
+  //手动开启蓝牙
+  setBluetooth() {
     wx.openSetting({
       complete() {
-        // _this.getSetting();
+        // app.getBluetoothStatus();
       }
     })
   },
+
+  ///返回到首页
   notRoter() {
-    getCurrentPages()[0].onLoad();
     route_util.goBackRoute(route_constant.indexRoot);
   },
-  /**
-   * 生命周期函数--监听页面初次渲染完成
-   */
-  onReady() {
-
-  },
-
-  /**
-   * 生命周期函数--监听页面显示
-   */
-  onShow() {
-
-  },
 
-  /**
-   * 生命周期函数--监听页面隐藏
-   */
-  onHide() {
-
-  },
-
-  /**
-   * 生命周期函数--监听页面卸载
-   */
   onUnload() {
-    wx.offAppShow(this.getSetting);
-  },
-
-  /**
-   * 页面相关事件处理函数--监听用户下拉动作
-   */
-  onPullDownRefresh() {
-
+    var that = this;
+    ///传入的监听函数。不传此参数则移除所有监听函数
+    wx.offAppShow(that.getSetting);
   },
-
-  /**
-   * 页面上拉触底事件的处理函数
-   */
-  onReachBottom() {
-
-  },
-
-  /**
-   * 用户点击右上角分享
-   */
-  onShareAppMessage() {
-
-  }
 })

+ 31 - 38
pages/deviceConnect1/deviceConnect1.js

@@ -1,54 +1,53 @@
-// pages/deviceConnect0/deviceConnect0.ts
 const app = getApp();
 Page({
-
-  /**
-   * 页面的初始数据
-   */
   data: {
+    nvabarData: {
+      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
+      title: '设置网络', //导航栏 中间的标题
+    },
     scopeBluetooth: app.globalData.scopeBluetooth,
     isShowPwd: false,
     is5GWifi: false,
     ssid: "",
     pwdData: "",
     pwdData1: "",
-    nvabarData: {
-      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
-      title: '设置网络', //导航栏 中间的标题
-    },
   },
 
-  /**
-   * 生命周期函数--监听页面加载
-   */
   onLoad() {
 
   },
 
+  onShow() {
+    var that = this;
+    that.getWifiStatus();
+  },
+
   getBluetoothStatus() {
     app.getBluetoothStatus();
   },
 
   setPwd: function (e) {
+    var that = this;
     app.globalData.pwdData = e.detail.value;
-    this.setData({
+    that.setData({
       pwdData: e.detail.value,
     });
   },
 
   setSsid: function (e) {
+    var that = this;
     let str = false;
     if (e.detail.value.indexOf("5G") !== -1) {
       str = true;
     };
-    this.setData({
+    that.setData({
       ssid: e.detail.value,
       is5GWifi: str
     });
   },
 
   getWifiStatus() {
-    const _this = this;
+    var that = this;
     wx.getNetworkType({
       success(res) {
         if (res.networkType === "wifi") {
@@ -59,15 +58,15 @@ Page({
                 success: function (res) {
                   // 5Gwifi
                   if (res.wifi.SSID.indexOf("5G") !== -1) {
-                    _this.setData({
+                    that.setData({
                       is5GWifi: true
                     });
                   } else {
-                    _this.setData({
+                    that.setData({
                       is5GWifi: false
                     });
                   };
-                  _this.setData({
+                  that.setData({
                     ssid: res.wifi.SSID,
                   });
                   app.globalData.ssid = res.wifi.SSID;
@@ -76,7 +75,7 @@ Page({
                     success(wifiInfo) {
                       const resData = JSON.parse(wifiInfo.data);
                       if (resData.ssid === res.wifi.SSID) {
-                        _this.setData({
+                        that.setData({
                           pwdData: resData.password,
                         });
                       }
@@ -87,12 +86,13 @@ Page({
             }
           })
         }
-
       }
     })
   },
+
   next() {
-    if (this.data.ssid === "") {
+    var that = this;
+    if (that.data.ssid === "") {
       wx.showToast({
         title: '请输入WIFI名称',
         icon: 'none',
@@ -100,7 +100,8 @@ Page({
       })
       return;
     };
-    if (this.data.pwdData === "") {
+
+    if (that.data.pwdData === "") {
       wx.showToast({
         title: '请输入WIFI密码',
         icon: 'none',
@@ -108,33 +109,25 @@ Page({
       })
       return;
     };
-    app.globalData.ssid = this.data.ssid;
-    app.globalData.pwdData = this.data.pwdData;
+
+    app.globalData.ssid = that.data.ssid;
+    app.globalData.pwdData = that.data.pwdData;
     wx.navigateTo({
       url: './../deviceConnect2/deviceConnect2',
     })
   },
+
   notRoter() {
     wx.navigateBack({
       delta: 1
     });
   },
+
   showPwd() {
-    this.setData({
-      isShowPwd: !this.data.isShowPwd
+    var that = this;
+    that.setData({
+      isShowPwd: !that.data.isShowPwd
     })
   },
-  /**
-   * 生命周期函数--监听页面初次渲染完成
-   */
-  onReady() {
-
-  },
 
-  /**
-   * 生命周期函数--监听页面显示
-   */
-  onShow() {
-    this.getWifiStatus();
-  },
 })

+ 3 - 4
pages/deviceConnect1/deviceConnect1.wxml

@@ -10,11 +10,11 @@
   </view>
   <view class="wifiInfo">
     <view class="ssid">
-      <input placeholder="请输入WIFI名称" bindinput="setSsid" type="text" value="{{ssid}}" maxlength="30"/>
+      <input placeholder="请输入WIFI名称" bindinput="setSsid" type="text" value="{{ssid}}" maxlength="30" />
     </view>
     <view class="ssid pwd" maxlength="30">
       <input wx:if="{{!isShowPwd}}" class="notShow" bindinput="setPwd" password="true" placeholder="请输入WiFi密码" value="{{pwdData}}" />
-      <input wx:else class="isShow" bindinput="setPwd" placeholder="请输入WiFi密码" value="{{pwdData}}"/>
+      <input wx:else class="isShow" bindinput="setPwd" placeholder="请输入WiFi密码" value="{{pwdData}}" />
       <view class="yz" bindtap="showPwd">
         <image mode="widthFix" src="./../../img/yj{{isShowPwd ? '1' : '0'}}.png"></image>
       </view>
@@ -29,5 +29,4 @@
       下一步
     </view>
   </view>
-</view>
-
+</view>

+ 36 - 31
pages/deviceConnect2/deviceConnect2.js

@@ -1,10 +1,7 @@
-// pages/deviceConnect0/deviceConnect0.ts
 const app = getApp();
 let xBlufi = require("../../utils/blufi/xBlufi.js");
 Page({
-  /**
-   * 页面的初始数据
-   */
+
   data: {
     nvabarData: {
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
@@ -24,39 +21,46 @@ Page({
     islanya: false,
     isOnBluetooth: false,
   },
-  onShow() {
-    if (!this.data.isOne) {
-      this.Search();
-    }
-  },
-  /**
-   * 生命周期函数--监听页面加载
-   */
+
   onLoad() {
-    const _this = this;
+    const that = this;
     const isOne = setTimeout(() => {
       clearTimeout(isOne);
-      if (_this.data.isOne) {
-        _this.setData({
+      if (that.data.isOne) {
+        that.setData({
           isOne: false,
         });
       }
     }, 2000);
+
     if (app.globalData.oneInitBluetooth) {
       app.globalData.oneInitBluetooth = false;
       xBlufi.initXBlufi(1);
     };
-    xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
-    this.Search();
+
+    xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
+    that.Search();
+  },
+
+  onShow() {
+    var that = this;
+    if (!that.data.isOne) {
+      thithats.Search();
+    }
   },
+
+  //   {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}}
+  // deviceConnect2.js:50 gadsfasdfqwerqwerqwerqer==={"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
+  // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}},{"deviceId":"42:E0:22:C2:6E:25","name":"","RSSI":-92,"connectable":true,"advertisData":"4c001007221fa8d7a84ba8","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]}
   funListenDeviceMsgEvent: function (options) {
-    const _this = this;
+    console.log("gadsfasdfqwerqwerqwerqer===" + JSON.stringify(options));
+    const that = this;
     switch (options.type) {
       case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
         // 搜索到蓝牙
         if (options.result) {
           if (options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1).length > 0) {
-            _this.setData({
+            that.setData({
               islanya: true,
               devicesList: options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1)
             });
@@ -68,14 +72,14 @@ Page({
         // console.log("连接回调:" + JSON.stringify(options))
         if (options.result) {
           wx.hideLoading();
-          if (_this.data.isOnBluetooth) {
+          if (that.data.isOnBluetooth) {
             return;
           };
-          _this.setData({
+          that.setData({
             isOnBluetooth: true,
           });
           setTimeout(() => {
-            _this.setData({
+            that.setData({
               isSubmit: false,
               islanya: false,
               devicesList: [],
@@ -105,7 +109,7 @@ Page({
           })
         } else {
           //蓝牙搜索开始
-          _this.setData({
+          that.setData({
             searching: true
           });
         }
@@ -119,13 +123,13 @@ Page({
           //蓝牙停止搜索失败
           console.log('蓝牙停止搜索失败')
         }
-        _this.setData({
+        that.setData({
           searching: false
         });
         break;
-
     }
   },
+
   Search: function () {
     xBlufi.notifyStartDiscoverBle({
       'isStart': true,
@@ -136,9 +140,9 @@ Page({
     // if(!this.data.islanya) {
     //   return;
     // };
-
     // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}]
-    const deviceIdData = this.data.devicesList;
+    var that = this;
+    const deviceIdData = that.data.devicesList;
     if (deviceIdData.length < 1) {
       wx.showToast({
         title: '请短按设备配对键',
@@ -148,14 +152,14 @@ Page({
       return;
     };
 
-    if (this.data.isSubmit) {
+    if (that.data.isSubmit) {
       return;
     };
-    this.setData({
+
+    that.setData({
       isSubmit: true
     });
 
-    // await getCurrentPages()[0].cancelCurrent();
     // 停止搜索
     xBlufi.notifyStartDiscoverBle({
       'isStart': false,
@@ -173,7 +177,8 @@ Page({
     });
   },
   onUnload: function () {
-    xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
+    var that = that
+    xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
   },
   getBluetoothStatus() {
     app.getBluetoothStatus();

+ 26 - 26
pages/deviceConnect3/deviceConnect3.js

@@ -30,50 +30,55 @@ Page({
     ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
   },
 
-  onShow: function (options) {},
-
   onLoad: function (options) {
-    _this = this;
-    _this.setData({
+    var that = this;
+    that.setData({
       name: options.name,
       connectedDeviceId: options.deviceId,
     })
+
     xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
     xBlufi.notifyInitBleEsp32({
       deviceId: options.deviceId,
     });
+
     percentIn = setInterval(() => {
-      if (_this.data.percent === 100) {
+      if (that.data.percent === 100) {
         clearInterval(percentIn);
-        _this.setData({
+        that.setData({
           ruterStatus: 2
         });
         return;
       };
-      _this.data.percent++;
-      _this.setData({
-        percent: _this.data.percent
+      that.data.percent++;
+      that.setData({
+        percent: that.data.percent
       });
     }, 600);
   },
+
   onUnload: function () {
     // 关闭蓝牙连接
-    let _this = this
+    var that = this
     xBlufi.notifyConnectBle({
       isStart: false,
-      deviceId: _this.data.connectedDeviceId,
-      name: _this.data.name,
+      deviceId: that.data.connectedDeviceId,
+      name: that.data.name,
     });
+
     xBlufi.listenDeviceMsgEvent(false, this.funListenDeviceMsgEvent);
     clearTimeout(errTi);
     clearInterval(percentIn);
   },
+
+  // {"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) {
-    let _this = this;
+    var that = this;
     switch (options.type) {
-
       case xBlufi.XBLUFI_TYPE.TYPE_STATUS_CONNECTED:
-        _this.setData({
+        that.setData({
           connected: options.result
         });
         if (!options.result) {
@@ -89,31 +94,26 @@ Page({
           })
         }
         break;
-
-
       case xBlufi.XBLUFI_TYPE.TYPE_CONNECT_ROUTER_RESULT:
         wx.hideLoading();
         if (!options.result)
           errTi = setTimeout(() => {
-            _this.setData({
+            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"}]
             getCurrentPages()[0].addConnectWifiDevice({
-              deviceId: _this.data.name,
+              deviceId: that.data.name,
             });
-            _this.setData({
+            that.setData({
               percent: 100,
               ruterStatus: 1
             });
@@ -133,13 +133,13 @@ Page({
         // console.log("初始化结果:", JSON.stringify(options))
         if (options.result) {
           console.log('初始化成功');
-          _this.OnClickStart();
+          that.OnClickStart();
         } else {
           console.log('初始化失败')
-          _this.setData({
+          that.setData({
             connected: false
           })
-          _this.setData({
+          that.setData({
             ruterStatus: 2
           });
         }

+ 4 - 1
pages/index/index.js

@@ -79,13 +79,17 @@ Page({
     if (!strings.isEmpty(userInfo)) {
       // 获取设备本地数据
       if (app.globalData.newDeviceId) {
+        console.log("gadfadfqwwerqeqr==00===");
         that.addNewDeviceId();
       } else if (app.globalData.client == null) {
+        console.log("gadfadfqwwerqeqr==11===");
         app.connect();
       } else if (that.data.deviceListIndex == null && that.data.deviceList.length > 0) {
+        console.log("gadfadfqwwerqeqr==22===");
         that.actionDevice(0)
       }
     } else {
+      console.log("gadfadfqwwerqeqr==33===");
       if (app.globalData.client == null) {
         app.connect();
       }
@@ -268,7 +272,6 @@ Page({
             return
           };
           var deviceId = deviceList[deviceListIndex].deviceId;
-
           // 接收设备当前播放状态
           const obj = {
             DstDeviceName: that.getThisDeviceID(deviceId)

+ 1 - 0
pages/index/index.wxml

@@ -159,6 +159,7 @@
       </view>
     </view>
   </view>
+
   <view style="height: 98rpx;">
     <view class="line"></view>
     <view style="height: 97rpx; text-align: center; display: flex; flex: 1; align-items: center;">