Przeglądaj źródła

feature: 处理小王子OTA-2A设备连接

Damon 7 miesięcy temu
rodzic
commit
7d17c0fb3d

+ 1 - 1
app.js

@@ -18,7 +18,6 @@ App({
     pwdData: "",
     userData: null,
     userInfo: null,
-    newDeviceId: null,
     is_debug: 2, // 1 测试环境 // 2正式环境
     client: null,
     oneInitBluetooth: true,
@@ -64,6 +63,7 @@ App({
         that.connect();
       };
     });
+
     that.getBluetoothStatus();
     //自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
     wx.getSystemInfo({

+ 1 - 0
pages/deviceConnect0/deviceConnect0.js

@@ -13,6 +13,7 @@ Page({
     isNotruter: false
   },
 
+  ///定位,蓝牙权限,蓝牙适配器
   onLoad(options) {
     var that = this;
     wx.offAppShow(that.getSetting);

+ 19 - 18
pages/deviceConnect1/deviceConnect1.js

@@ -13,10 +13,6 @@ Page({
     pwdData1: "",
   },
 
-  onLoad() {
-
-  },
-
   onShow() {
     var that = this;
     that.getWifiStatus();
@@ -26,14 +22,7 @@ Page({
     app.getBluetoothStatus();
   },
 
-  setPwd: function (e) {
-    var that = this;
-    app.globalData.pwdData = e.detail.value;
-    that.setData({
-      pwdData: e.detail.value,
-    });
-  },
-
+  ///Wifi名称
   setSsid: function (e) {
     var that = this;
     let str = false;
@@ -46,6 +35,16 @@ Page({
     });
   },
 
+  ///Wifi名称
+  setPwd: function (e) {
+    var that = this;
+    app.globalData.pwdData = e.detail.value;
+    that.setData({
+      pwdData: e.detail.value,
+    });
+  },
+
+  ///获取网络状态
   getWifiStatus() {
     var that = this;
     wx.getNetworkType({
@@ -56,20 +55,21 @@ Page({
               //获取当前已连接wifi名
               wx.getConnectedWifi({
                 success: function (res) {
+                  var ssid = res.wifi.SSID;
                   // 5Gwifi
                   if (res.wifi.SSID.indexOf("5G") !== -1) {
                     that.setData({
-                      is5GWifi: true
+                      is5GWifi: true,
+                      ssid: ssid,
                     });
                   } else {
                     that.setData({
-                      is5GWifi: false
+                      is5GWifi: false,
+                      ssid: ssid,
                     });
                   };
-                  that.setData({
-                    ssid: res.wifi.SSID,
-                  });
-                  app.globalData.ssid = res.wifi.SSID;
+
+                  app.globalData.ssid = ssid;
                   wx.getStorage({
                     key: 'wifiInfo',
                     success(wifiInfo) {
@@ -90,6 +90,7 @@ Page({
     })
   },
 
+  ///下一步
   next() {
     var that = this;
     if (that.data.ssid === "") {

+ 50 - 42
pages/deviceConnect2/deviceConnect2.js

@@ -45,21 +45,46 @@ Page({
   onShow() {
     var that = this;
     if (!that.data.isOne) {
-      thithats.Search();
+      that.Search();
     }
   },
 
+  ///开始或结束,也可以过滤名称
+  Search: function () {
+    xBlufi.notifyStartDiscoverBle({
+      'isStart': true,
+    });
+  },
+
   //   {"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":{}}]}
   // {"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) {
-    console.log("gadsfasdfqwerqwerqwerqer===" + JSON.stringify(options));
     const that = this;
     switch (options.type) {
+      //发现设备列表回调开始
+      case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
+        if (!options.result) {
+          console.log("蓝牙未开启 fail =》", options)
+          wx.showToast({
+            title: '蓝牙未开启',
+            icon: 'none'
+          })
+        } else {
+          //蓝牙搜索开始
+          that.setData({
+            searching: true
+          });
+        }
+        break;
+
+        // [{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0","RSSI":-62,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fd3af0","serviceData":{}}]
+        ///发现设备列表回调
       case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS:
         // 搜索到蓝牙
         if (options.result) {
           if (options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1).length > 0) {
+            ///过滤乐鑫设备
             that.setData({
               islanya: true,
               devicesList: options.data.filter((v) => v.name.indexOf("BLUFI_") !== -1)
@@ -68,16 +93,20 @@ Page({
         }
         break;
 
+        // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}}
+        // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}}
+        ///主动去连接连接
       case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED:
-        // console.log("连接回调:" + JSON.stringify(options))
+        wx.hideLoading();
         if (options.result) {
-          wx.hideLoading();
           if (that.data.isOnBluetooth) {
             return;
           };
+
           that.setData({
             isOnBluetooth: true,
           });
+
           setTimeout(() => {
             that.setData({
               isSubmit: false,
@@ -88,10 +117,8 @@ Page({
             wx.navigateTo({
               url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name,
             });
-          }, 500);
-
+          }, 200);
         } else {
-          wx.hideLoading()
           wx.showModal({
             title: '提示',
             content: '连接失败',
@@ -100,21 +127,7 @@ Page({
         }
         break;
 
-      case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START:
-        if (!options.result) {
-          console.log("蓝牙未开启 fail =》", options)
-          wx.showToast({
-            title: '蓝牙未开启',
-            icon: 'none'
-          })
-        } else {
-          //蓝牙搜索开始
-          that.setData({
-            searching: true
-          });
-        }
-        break;
-
+        ///停止发现设备列表回调
       case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP:
         if (options.result) {
           //蓝牙停止搜索ok
@@ -130,20 +143,11 @@ Page({
     }
   },
 
-  Search: function () {
-    xBlufi.notifyStartDiscoverBle({
-      'isStart': true,
-    });
-  },
-
+  // [{"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":{}}]
   async Connect(e) {
-    // 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":{}}]
     var that = this;
-    const deviceIdData = that.data.devicesList;
-    if (deviceIdData.length < 1) {
+    const devicesList = that.data.devicesList;
+    if (devicesList.length < 1) {
       wx.showToast({
         title: '请短按设备配对键',
         icon: 'none',
@@ -164,28 +168,32 @@ Page({
     xBlufi.notifyStartDiscoverBle({
       'isStart': false,
     });
+
     //开始连接
-    let name = deviceIdData[0].name;
-    // console.log('点击了,蓝牙准备连接的deviceId:' + deviceIdData[0].deviceId)
+    let name = devicesList[0].name;
+    // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId)
     xBlufi.notifyConnectBle({
       isStart: true,
-      deviceId: deviceIdData[0].deviceId,
+      deviceId: devicesList[0].deviceId,
       name
     });
     wx.showLoading({
       title: '请稍后',
     });
   },
-  onUnload: function () {
-    var that = that
-    xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
-  },
+
   getBluetoothStatus() {
     app.getBluetoothStatus();
   },
+
   notRoter() {
     wx.navigateBack({
       delta: 1
     });
   },
+
+  onUnload: function () {
+    var that = this;
+    xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent);
+  },
 })

+ 60 - 52
pages/deviceConnect3/deviceConnect3.js

@@ -4,13 +4,8 @@ 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 _this = null;
 let errTi = null;
 Page({
-
-  /**
-   * 页面的初始数据
-   */
   data: {
     nvabarData: {
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
@@ -37,7 +32,8 @@ Page({
       connectedDeviceId: options.deviceId,
     })
 
-    xBlufi.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent);
+    xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent);
+    ///通知初始化获取设备的服务列表等信息
     xBlufi.notifyInitBleEsp32({
       deviceId: options.deviceId,
     });
@@ -45,6 +41,7 @@ Page({
     percentIn = setInterval(() => {
       if (that.data.percent === 100) {
         clearInterval(percentIn);
+        ///失败
         that.setData({
           ruterStatus: 2
         });
@@ -57,43 +54,33 @@ Page({
     }, 600);
   },
 
-  onUnload: function () {
-    // 关闭蓝牙连接
-    var that = this
-    xBlufi.notifyConnectBle({
-      isStart: false,
-      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) {
     var that = this;
     switch (options.type) {
-      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
-              });
-            },
+      // {"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)
@@ -128,25 +115,29 @@ Page({
           }
         }
         break;
-      case xBlufi.XBLUFI_TYPE.TYPE_INIT_ESP32_RESULT:
-        wx.hideLoading();
-        // console.log("初始化结果:", JSON.stringify(options))
-        if (options.result) {
-          console.log('初始化成功');
-          that.OnClickStart();
-        } else {
-          console.log('初始化失败')
-          that.setData({
-            connected: false
+
+        ///设备连接状态回调
+      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
+              });
+            },
           })
-          that.setData({
-            ruterStatus: 2
-          });
         }
         break;
     }
   },
 
+  ///校验wifi账号密码
   OnClickStart: function () {
     if (!app.globalData.ssid) {
       wx.showToast({
@@ -163,18 +154,23 @@ Page({
       })
       return;
     }
+
     xBlufi.notifySendRouterSsidAndPassword({
       ssid: app.globalData.ssid,
       password: app.globalData.pwdData
     });
   },
+
   bindPasswordInput: function (e) {
-    this.setData({
+    var that = this;
+    that.setData({
       password: e.detail.value
     })
   },
+
   bindCustomDataInput: function (e) {
-    this.setData({
+    var that = this;
+    that.setData({
       customData: e.detail.value
     })
   },
@@ -185,8 +181,20 @@ Page({
   },
 
   goIndex() {
-    //  BLUFI_7cdfa1fd3cfc
-    app.globalData.newDeviceId = this.data.name;
     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);
+  },
 })

+ 38 - 52
pages/index/index.js

@@ -78,18 +78,12 @@ Page({
     var userInfo = wx.getStorageSync("userInfo") || "";
     if (!strings.isEmpty(userInfo)) {
       // 获取设备本地数据
-      if (app.globalData.newDeviceId) {
-        console.log("gadfadfqwwerqeqr==00===");
-        that.addNewDeviceId();
-      } else if (app.globalData.client == null) {
-        console.log("gadfadfqwwerqeqr==11===");
+      if (app.globalData.client == null) {
         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();
       }
@@ -193,44 +187,6 @@ Page({
     }
   },
 
-  addNewDeviceId() {
-    // 连接mqtt
-    var that = this;
-    if (app.globalData.client === null) {
-      app.connect();
-    } else if (app.globalData.newDeviceId) {
-      //监听
-      var deviceList = that.data.deviceList;
-      if (deviceList.length == 0) {
-        return;
-      }
-
-      ///连接新添加的设备
-      var deviceListIndex = that.data.deviceListIndex;
-      if (deviceListIndex != null) {
-        if (deviceList.length > deviceListIndex) {
-          if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
-            that.setData({
-              actionIndex: null,
-              deviceListIndex: null,
-            });
-          }
-        } else {
-          that.setData({
-            actionIndex: null,
-            deviceListIndex: null,
-          });
-        }
-      }
-      var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
-      app.subscribe(topic);
-      const Timeout = setTimeout(() => {
-        clearTimeout(Timeout);
-        that.actionDevice(0);
-      }, 500);
-    }
-  },
-
   // 回调
   mqttCallback(type, option) {
     // console.log("gadsfadsfadsfa==888===" + type);
@@ -539,14 +495,7 @@ Page({
         type: "get_position",
         ...obj
       });
-
       wx.setStorageSync("channelData", res);
-
-      // 有新设备
-      if (app.globalData.newDeviceId) {
-        app.globalData.newDeviceId = null;
-        return;
-      }
     })
   },
 
@@ -1131,6 +1080,43 @@ Page({
   },
 })
 
+// addNewDeviceId() {
+//   // 连接mqtt
+//   var that = this;
+//   if (app.globalData.client === null) {
+//     app.connect();
+//   } else if (app.globalData.newDeviceId) {
+//     //监听
+//     var deviceList = that.data.deviceList;
+//     if (deviceList.length == 0) {
+//       return;
+//     }
+
+//     ///连接新添加的设备
+//     var deviceListIndex = that.data.deviceListIndex;
+//     if (deviceListIndex != null) {
+//       if (deviceList.length > deviceListIndex) {
+//         if (deviceList[deviceListIndex].deviceId != deviceList[0].deviceId) {
+//           that.setData({
+//             actionIndex: null,
+//             deviceListIndex: null,
+//           });
+//         }
+//       } else {
+//         that.setData({
+//           actionIndex: null,
+//           deviceListIndex: null,
+//         });
+//       }
+//     }
+//     var topic = `/AIrSMArT_${deviceList[0].deviceId.split("BLUFI_")[1]}/status/onoffline`;
+//     app.subscribe(topic);
+//     const Timeout = setTimeout(() => {
+//       clearTimeout(Timeout);
+//       that.actionDevice(0);
+//     }, 500);
+//   }
+// },
 
 // tryConnectBle() {
 //   var hasBle = false;

+ 4 - 7
utils/blufi/xBlufi.js

@@ -7,17 +7,14 @@ let XMQTT_SYSTEM = {
   WeChat: 1,
 };
 
-
 let XBLUFI_TYPE = {
   TYPE_STATUS_CONNECTED: '-2', /// 设备连接状态回调
   TYPE_CLOSE_CONNECTED: '-1', ///主动关闭连接
-  TYPE_CONNECTED: '0',  //主动连接
+  TYPE_CONNECTED: '0', //主动连接
   TYPE_GET_DEVICE_LISTS: '1', //发现设备列表回调
   TYPE_INIT_ESP32_RESULT: '2',
   TYPE_RECIEVE_CUSTON_DATA: '3', //接收到自定义数据
   TYPE_CONNECT_ROUTER_RESULT: '4',
-
-
   TYPE_GET_DEVICE_LISTS_START: ' 41', //发现设备列表回调开始
   TYPE_GET_DEVICE_LISTS_STOP: '42', //停止发现设备列表回调
 
@@ -43,8 +40,10 @@ let OnFireEvent = {
  */
 function initXBlufi(type) {
   switch (type) {
+    ///Alis
     case XMQTT_SYSTEM.Alis:
       break;
+      ///WeChat
     case XMQTT_SYSTEM.WeChat:
       $wxBlufiImpl.init();
       break;
@@ -97,7 +96,7 @@ function notifyConnectBle(options) {
  * @param options 连接参数 {"isStart":true} 是否开始发现设备
  */
 function listenConnectBle(isSetListener, funtion) {
-  
+
   if (isSetListener) {
     mOnFire.on(OnFireEvent.EVENT_CONNECT_DISCONNECT, funtion)
   } else {
@@ -124,8 +123,6 @@ function listenInitBleEsp32(isSetListener, funtion) {
   }
 }
 
-
-
 /**
  * 发送要连接的路由器的ssid和密码
  * @param options 连接参数 {"deviceId":"设备的设备id","serverId":"服务id","characterId":"通道","ssid":"路由器名字","password":"密码"}