Browse Source

feature: 解决多个设备都在线进行切换数据处理

332777428@qq.com 6 months ago
parent
commit
d946d808b2
2 changed files with 28 additions and 29 deletions
  1. 20 22
      pages/index/index.js
  2. 8 7
      utils/lexin/add.js

+ 20 - 22
pages/index/index.js

@@ -579,10 +579,10 @@ Page({
     }
 
     ///去蓝牙连接处理
+    // {"connectType":1,"deviceId":"E4:9F:80:09:40:EC","name":"E4:9F:80:09:40:EC","state":"online","ProdModel":"MW-SR1(4G_WIFI)","devName":"猫王音响x阿基米德 SR1 妙播收音机"}
     if (index === deviceListSelect && device.state === "online") {
       route_util.jumpParam('/pages/deviceDetail/detail', JSON.stringify(device))
     } else if (device.state === "offline") {
-      console.log("去连接蓝牙", device)
       device.name = device.devName;
       BtHelper.getInstance().connect(device, function (isConnected, d) {
         if (isConnected) {
@@ -591,7 +591,13 @@ Page({
         }
       });
     } else {
-      // {"connectType":1,"deviceId":"E4:9F:80:09:40:EC","name":"E4:9F:80:09:40:EC","state":"online","ProdModel":"MW-SR1(4G_WIFI)","devName":"猫王音响x阿基米德 SR1 妙播收音机"}
+      device.name = device.devName;
+      BtHelper.getInstance().connect(device, function (isConnected, d) {
+        if (isConnected) {
+          device.state = "online";
+          that.addBlueDevice(device);
+        }
+      });
     }
   },
 
@@ -677,23 +683,24 @@ Page({
     };
 
     ///删除当前设备
+    console.log("gasdfqwerqwerqrq==11===" + deviceList.length);
     var deviceId = deviceList[index].deviceId;
     deviceList = deviceList.filter((item, i) => {
       return deviceId !== item.deviceId;
     });
-    console.log("断开蓝牙连接", deviceId)
-
-    BtHelper.getInstance().disconnect({
-      "deviceId": deviceId
-    });
-
+    console.log("gasdfqwerqwerqrq==222===" + deviceList.length);
     store.setStore("deviceList", deviceList);
+
     var indexPage = that.data.indexPage;
     that.setData({
       deviceList: deviceList,
       indexPage: indexPage == 1 ? 0 : indexPage,
       deviceListSelect: isCurrentIndex ? null : deviceListSelect,
     });
+
+    BtHelper.getInstance().disconnect({
+      "deviceId": deviceId
+    });
   },
 
   /// 断开连接wifi
@@ -745,6 +752,7 @@ Page({
       deviceListSelect: 0,
       autoConnected: true,
     });
+
     event_bus.removeNotification(CmdEvent.eventName, that);
     event_bus.addNotification(CmdEvent.eventName, function (event) {
       if (event.cmdEvent == EnumCmdEvent.onoffline) {
@@ -770,9 +778,7 @@ Page({
           console.log("首页断开连接", disDevice)
           that.updateDeviceList(that.data.deviceList, false, false);
         }
-
       }
-
     });
   },
 
@@ -823,18 +829,10 @@ Page({
   updateDeviceList(deviceList, isInit, closeAllBlue) {
     var that = this;
     var finalList = lexin_add.updateDeviceList(deviceList, isInit, closeAllBlue);
-    if (!strings.isEmpty(deviceList) || !strings.isEmpty(finalList)) {
-      store.setStore("deviceList", finalList);
-
-      // todo 测试代码
-      // finalList.forEach(element => {
-      //   element.state = "online";
-      // });
-
-      that.setData({
-        deviceList: finalList,
-      });
-    }
+    store.setStore("deviceList", finalList);
+    that.setData({
+      deviceList: finalList,
+    });
   },
 
   ///点击banner事件

+ 8 - 7
utils/lexin/add.js

@@ -13,13 +13,12 @@ function addBlueDevice(device) {
   // 同一个设备处理
   var pages = getCurrentPages();
   var deviceList = pages[0].getDeviceList();
-  var tempList = deviceList.filter((v) => v.deviceId === device.deviceId);
-  const strings = require('../strings');
-  if (!strings.isEmpty(tempList)) {
-    deviceList = tempList.filter((v) => v.deviceId !== device.deviceId);
-  }
+  var finalList = deviceList.filter((v) => {
+    return v.deviceId != device.deviceId;
+  });
+
   /// 添加到第一个
-  deviceList.unshift({
+  finalList.unshift({
     connectType: 1,
     devName: device.name,
     state: "online",
@@ -30,7 +29,8 @@ function addBlueDevice(device) {
     clientType: device.clientType || device.ProdModel,
     imageUrl: device.imageUrl,
   });
-  return deviceList;
+  console.log("dafdafasdfasdfa==gggg==" + finalList.length);
+  return finalList;
 };
 
 // 新添加Wifi设备  猫王小王子OTR-X
@@ -55,6 +55,7 @@ function updateDeviceList(list, isInit, closeAllBlue) {
     return [];
   }
 
+  console.log("dafdafasdfasdfa==9999==" + list.length);
   const app = getApp();
   var finalList = [];
   if (isInit) {