ソースを参照

fix:优化蓝牙设备上线的判断

zeng.chen 2 ヶ月 前
コミット
911c54369e
1 ファイル変更10 行追加6 行削除
  1. 10 6
      pages/pageA/index/index.js

+ 10 - 6
pages/pageA/index/index.js

@@ -159,7 +159,6 @@ Page({
       BtHelper.getInstance().getBluetoothDevices();
       BtHelper.getInstance().startScan(null, null, function (res) { });
       var isFirst = true;
-      console.log("开始监听蓝牙设备");
       that.startBleTimer(isFirst, true);
       that.addNotification()
     }
@@ -178,6 +177,7 @@ Page({
       // 强制开启监听 && (有蓝牙在线 || 有wifi在线)就return
       return;
     }
+
     ///再秒再对比一次
     that.stopIntervalId1();
     let count = 0;
@@ -232,19 +232,18 @@ Page({
       BtHelper.getInstance().connect(device, function (isConnected, d) {
         console.log("自动连接成功", isConnected)
         if (isConnected) {
-          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;
+                return;
               }
             }
           })
 
-          if (hasMyOnline && !hasOtherOnline) {
+          if (!hasOtherOnline) {
             // 自己已经在线,没有其他设备在线, 直接更新
             console.log("自己已经在线,没有其他设备在线, 直接更新");
             that.addBlueDevice(device);
@@ -252,7 +251,9 @@ Page({
             let bleDev = that.data.deviceList.find(item => {
               return item.deviceId === device.deviceId;
             });
-            bleDev.state = 'online';
+            if (bleDev) {
+              bleDev.state = 'online';
+            }
             that.updateDeviceList(that.data.deviceList, false, false);
           }
           that.stopIntervalId1();
@@ -299,6 +300,8 @@ Page({
               deviceListSelect: null,
               indexPage: indexPage == 1 ? 0 : indexPage,
             });
+            console.log("开启蓝牙监听1:",)
+
             that.startBleTimer(true, false);
           }, function () {
             var indexPage = that.data.indexPage;
@@ -307,6 +310,8 @@ Page({
               deviceListSelect: null,
               indexPage: indexPage == 1 ? 0 : indexPage,
             });
+            console.log("开启蓝牙监听2:",)
+
             that.startBleTimer(true, false);
 
           });
@@ -841,7 +846,6 @@ Page({
             return item.state == "online" && item.connectType === 3
           })
           if (!wifiDevice) {
-            console.log("开启蓝牙监听:", event.deviceId, event.commonValue)
             that.startBleTimer(isFirst, true);
           }
         }