Ver código fonte

feature: 处理wifi和蓝牙排序问题

Damon 7 meses atrás
pai
commit
4153ad9b4b
3 arquivos alterados com 138 adições e 77 exclusões
  1. 40 21
      devices/ble_manager.js
  2. 4 0
      devices/bt_helper.js
  3. 94 56
      pages/index/index.js

+ 40 - 21
devices/ble_manager.js

@@ -13,6 +13,7 @@ class bleManager {
     that.requestBlueTime = 0;
     ///正在执行扫描中
     that.doStartScaning = false;
+    that.dissmissDevice = [];
   }
 
   ///获取比较的数据
@@ -20,6 +21,10 @@ class bleManager {
     return this.compareList;
   }
 
+  getDissmissDevice() {
+    return this.dissmissDevice;
+  }
+
   setConnectWillDevice(connectWillDevice) {
     this.connectWillDevice = connectWillDevice;
   }
@@ -35,14 +40,14 @@ class bleManager {
   /// 监控蓝牙打开状态
   initBluetoothAdapter() {
     var that = this;
-    wx.openBluetoothAdapter({
-      success: (res) => {
-        that.isAvailable = true;
-      },
-      fail: (err) => {
-        that.isAvailable = false;
-      }
-    });
+    // wx.openBluetoothAdapter({
+    //   success: (res) => {
+    //     that.isAvailable = true;
+    //   },
+    //   fail: (err) => {
+    //     that.isAvailable = false;
+    //   }
+    // });
     wx.onBluetoothAdapterStateChange(function (res) {
       that.isAvailable = res.available;
     })
@@ -151,21 +156,35 @@ class bleManager {
     // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
     wx.onBLEConnectionStateChange((result) => {
       if (result.connected) {
-
+        for (var i = 0; i < that.dissmissDevice.length; i++) {
+          if (result.deviceId == that.dissmissDevice[i].deviceId) {
+            that.dissmissDevice.splice(i, 1);
+            break;
+          }
+        }
       } else {
-
-      }
-      ///是否已配对
-      wx.isBluetoothDevicePaired({
-        deviceId: result.deviceId,
-        ///{"isPaired":false,"errno":0,"errMsg":"isBluetoothDevicePaired:ok"}
-        success: (res) => {
-          console.log("gasdfqwerqwerqr==00===" + JSON.stringify(res));
-        },
-        fail: (err) => {
-          console.log("gasdfqwerqwerqr==111===" + JSON.stringify(err));
+        var has = false;
+        for (var i = 0; i < that.dissmissDevice.length; i++) {
+          if (result.deviceId == that.dissmissDevice[i].deviceId) {
+            has = true;
+            break;
+          }
         }
-      })
+        if (!has) {
+          that.dissmissDevice.push(result);
+        }
+      }
+      // ///是否已配对
+      // wx.isBluetoothDevicePaired({
+      //   deviceId: result.deviceId,
+      //   ///{"isPaired":false,"errno":0,"errMsg":"isBluetoothDevicePaired:ok"}
+      //   success: (res) => {
+      //     console.log("gasdfqwerqwerqr==00===" + JSON.stringify(res) + "===" + JSON.stringify(result));
+      //   },
+      //   fail: (err) => {
+      //     console.log("gasdfqwerqwerqr==111===" + JSON.stringify(err));
+      //   }
+      // })
     });
   }
 

+ 4 - 0
devices/bt_helper.js

@@ -78,6 +78,10 @@ class BtHelper {
     return this.bleManager.getCompareList();
   }
 
+  getDissmissDevice() {
+    return this.bleManager.getDissmissDevice();
+  }
+
   getCallBackConnect() {
     return this.bleManager.getCallBackConnect();
   }

+ 94 - 56
pages/index/index.js

@@ -86,50 +86,9 @@ Page({
     }
   },
 
-  tryConnectBle() {
-    var hasBle = false;
-    var hasConnectBle = false;
-    var hasWifi = false;
-    var bleDevice;
-    let that = this
-    that.data.deviceList.forEach(device => {
-      if (device.connectType == 1) {
-        hasBle = true
-        if (device.state == "online") {
-          hasConnectBle = true
-        }
-        bleDevice = device
-      } else if (device.connectType == 3 && device.state == "online") {
-        // wifi
-        hasWifi = true;
-      }
-    });
-    if (!hasWifi && !hasConnectBle && hasBle) {
-      // 没有wifi 没有连接的ble 有未连接的ble
-      console.log("去连接蓝牙")
-      let bt_helper = BtHelper.getInstance()
-      bt_helper.initBluetooth(function (adapterState, hasPermission) {
-        console.log("蓝牙状态", adapterState, hasPermission)
-        if (adapterState && hasPermission) {
-          bt_helper.connect(bleDevice, function (data) {
-            console.log("连接成功:", data)
-            if (data == true) {
-              // 蓝牙模式
-              bleDevice.connectType = 1
-              // 在线
-              bleDevice.state = 'online'
-              bleDevice.ProdModel = bleDevice.clientType
-              that.addConnectBlueDevice(connectDevice);
-            }
-          })
-        } else {}
-      })
-    }
-  },
-
   onDeviceLoad() {
     var that = this;
-    // BtHelper.getInstance().initBluetoothAdapter();
+    BtHelper.getInstance().initBluetoothAdapter();
     var l = store.getStore("deviceList");
     if (!strings.isEmpty(l)) {
       that.updateDeviceList(l, true);
@@ -153,6 +112,18 @@ Page({
     if (deviceList.length > 0) {
       if (BtHelper.getInstance().getCallBackConnect() == null) {
         var compareList = BtHelper.getInstance().getCompareList();
+        var dissmissDevice = BtHelper.getInstance().getDissmissDevice();
+
+        ///去掉未连接的离线的
+        for (var i = 0; i < dissmissDevice.length; i++) {
+          for (var j = 0; j < compareList.length; j++) {
+            if (compareList[j].deviceId == dissmissDevice[i].deviceId) {
+              compareList.splice(j, 1);
+              break;
+            }
+          }
+        }
+
         if (compareList.length > 0) {
           ///对比在线的蓝牙设备
           var isChanged = false;
@@ -163,7 +134,6 @@ Page({
               for (var j = 0; j < compareList.length; j++) {
                 // "state":"online" "offline" MW-SR1(4G_WIFI)
                 if (tempItem.deviceId === compareList[j].deviceId) {
-                  console.log("gasdfqwerqwerqdfasdfr==00==" + JSON.stringify(compareList[j]));
                   has = true;
                   break;
                 }
@@ -182,6 +152,7 @@ Page({
               }
             }
           }
+
           if (isChanged) {
             that.updateDeviceList(deviceList, false);
           }
@@ -252,7 +223,7 @@ Page({
 
   // 回调
   mqttCallback(type, option) {
-    console.log("gadsfadsfadsfa==888===" + type);
+    // console.log("gadsfadsfadsfa==888===" + type);
     var that = this;
     let payloads = null;
     if (option) {
@@ -386,7 +357,7 @@ Page({
   onlineDevice(payloads) {
     // 设置在线状态
     var that = this;
-    console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
+    // console.log("gadsfadsfadsfa==777===" + JSON.stringify(payloads));
 
     ///是否更新过在线离线状态
     var isUpdate = false;
@@ -986,21 +957,34 @@ Page({
     if (isInit) {
       deviceList[0].state = "offline";
     }
-    finalList.push(deviceList[0]);
-    console.log("online", deviceList[0].deviceId, deviceList[0].state);
 
-    var onLineList = [];
-    var onNoLineList = [];
+    var isFirstOnline = false;
+    if (deviceList[0].state == "online") {
+      isFirstOnline = true;
+      finalList.push(deviceList[0]);
+    }
 
     ///区分在线和离线
-    for (var i = 1; i < deviceList.length; i++) {
-      var device = deviceList[i];
+    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
       if (isInit) {
-        device.state = "offline";
+        deviceList[i].state = "offline";
       }
+    }
+
+    var onLineList = [];
+    var onNoLineList = [];
+    ///添加在线的
+    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+      var device = deviceList[i];
       if (device.state == "online") {
         onLineList.push(device)
-      } else {
+      }
+    }
+
+    ///添加离线的
+    for (var i = isFirstOnline ? 1 : 0; i < deviceList.length; i++) {
+      var device = deviceList[i];
+      if (device.state != "online") {
         onNoLineList.push(device)
       }
     }
@@ -1008,13 +992,19 @@ Page({
     // 区分在线wifi和蓝牙 wifi在前 离线在后
     var onLineWifiList = [];
     var onLineBlueList = [];
+    ///添加在线wifi
     onLineList.forEach(element => {
       if (element.connectType == 3) {
-        onLineWifiList.push(element)
-      } else {
+        onLineWifiList.push(element);
+      }
+    });
+    ///添加在线蓝牙
+    onLineList.forEach(element => {
+      if (element.connectType != 3) {
         onLineBlueList.push(element)
       }
     });
+
     finalList = finalList.concat(onLineWifiList);
     finalList = finalList.concat(onLineBlueList);
 
@@ -1026,13 +1016,19 @@ Page({
     ///区分离线wifi和蓝牙 wifi在前 离线在后
     var onNoLineWifiList = [];
     var onNoLineBlueList = [];
+    ///添加离线wifi
     onNoLineList.forEach(element => {
       if (element.connectType == 3) {
         onNoLineWifiList.push(element)
-      } else {
+      }
+    });
+    ///添加离线蓝牙
+    onNoLineList.forEach(element => {
+      if (element.connectType != 3) {
         onNoLineBlueList.push(element)
       }
     });
+
     finalList = finalList.concat(onNoLineWifiList);
     finalList = finalList.concat(onNoLineBlueList);
 
@@ -1127,6 +1123,48 @@ Page({
   },
 })
 
+
+// tryConnectBle() {
+//   var hasBle = false;
+//   var hasConnectBle = false;
+//   var hasWifi = false;
+//   var bleDevice;
+//   let that = this
+//   that.data.deviceList.forEach(device => {
+//     if (device.connectType == 1) {
+//       hasBle = true
+//       if (device.state == "online") {
+//         hasConnectBle = true
+//       }
+//       bleDevice = device
+//     } else if (device.connectType == 3 && device.state == "online") {
+//       // wifi
+//       hasWifi = true;
+//     }
+//   });
+//   if (!hasWifi && !hasConnectBle && hasBle) {
+//     // 没有wifi 没有连接的ble 有未连接的ble
+//     console.log("去连接蓝牙")
+//     let bt_helper = BtHelper.getInstance()
+//     bt_helper.initBluetooth(function (adapterState, hasPermission) {
+//       console.log("蓝牙状态", adapterState, hasPermission)
+//       if (adapterState && hasPermission) {
+//         bt_helper.connect(bleDevice, function (data) {
+//           console.log("连接成功:", data)
+//           if (data == true) {
+//             // 蓝牙模式
+//             bleDevice.connectType = 1
+//             // 在线
+//             bleDevice.state = 'online'
+//             bleDevice.ProdModel = bleDevice.clientType
+//             that.addConnectBlueDevice(connectDevice);
+//           }
+//         })
+//       } else {}
+//     })
+//   }
+// },
+
 // 去掉此功能,先留着吧
 // const other= {
 //   "url": "",