Browse Source

fix:增加wifi设备在线的蓝牙设备的在线判断

zeng.chen 2 months ago
parent
commit
b597778486
2 changed files with 37 additions and 18 deletions
  1. 21 14
      pages/pageA/index/index.js
  2. 16 4
      utils/lexin/scan.js

+ 21 - 14
pages/pageA/index/index.js

@@ -203,7 +203,7 @@ Page({
     let that = this;
     constant.lexinScan.compareList2(that.data.deviceList, function (device) {
       if (device) {
-        console.log("找到蓝牙了");
+        console.log("找到蓝牙了", JSON.stringify(device));
         that.data._willConnectBle = device;
         getApp().getBluetoothStatus();
       }
@@ -232,21 +232,28 @@ Page({
       BtHelper.getInstance().connect(device, function (isConnected, d) {
         console.log("自动连接成功", isConnected)
         if (isConnected) {
-          device.state = 'online';
-          let hasDev = that.data.deviceList.find(item => {
-            return item.deviceId === device.deviceId;
-          })
-
-          let hasOnline = that.data.deviceList.find(item => {
-            return item.state === 'online';
+          let hasMyOnline = false;
+          let hasOtherOnline = false;
+          that.data.deviceList.forEach(item => {
+            if (item.state === 'online') {
+              if (item.deviceId == device.deviceId) {
+                hasMyOnline = true;
+              } else {
+                hasOtherOnline = true;
+              }
+            }
           })
 
-          if (hasDev && hasOnline) {
-            // 有设备&&有在线的设备, 直接更新
-            hasDev.state = 'online';
-            that.updateDeviceList(that.data.deviceList, false, false);
-          } else {
+          if (hasMyOnline && !hasOtherOnline) {
+            // 自己已经在线,没有其他设备在线, 直接更新
+            console.log("自己已经在线,没有其他设备在线, 直接更新");
             that.addBlueDevice(device);
+          } else {
+            let bleDev = that.data.deviceList.find(item => {
+              return item.deviceId === device.deviceId;
+            });
+            bleDev.state = 'online';
+            that.updateDeviceList(that.data.deviceList, false, false);
           }
           that.stopIntervalId1();
         } else {
@@ -854,7 +861,7 @@ Page({
         // });
         if (!disDevice) {
           // console.log("")
-          console.log("没有找到在线的对应的蓝牙设备", JSON.stringify(deviceList))
+          console.log("没有找到在线的对应的蓝牙设备", deviceList.length)
           return;
         }
 

+ 16 - 4
utils/lexin/scan.js

@@ -10,7 +10,7 @@ module.exports = {
 var deviceListIndex = 0
 async function compareList2(deviceList, callback) {
   let isHasWifi = deviceList.find(item => {
-    return item.connectType == 3;
+    return item.connectType === 3 && item.state === "online";
   });
   if (isHasWifi) {
     //有wifi
@@ -45,10 +45,22 @@ async function compareList2(deviceList, callback) {
     return;
   }
 
-  console.log("有在线的3", JSON.stringify(deviceList[deviceListIndex]), deviceListIndex)
+  let bleDev = deviceList[deviceListIndex];
+  if (bleDev.connectType === 1) {
+    callback(deviceList[deviceListIndex])
+    deviceListIndex++
+  } else {
+    for (var i = 0; i < deviceList.length; i++) {
+      let element = deviceList[i];
+      if (element.connectType === 1) {
+        bleDev = element;
+        callback(element)
+        deviceListIndex = i;
+        return;
+      }
+    }
+  }
 
-  callback(deviceList[deviceListIndex])
-  deviceListIndex++
   if (deviceListIndex >= deviceList.length) {
     deviceListIndex = 0;
   }