Преглед изворни кода

feature:修改掉线的监控问题

zeng.chen пре 6 месеци
родитељ
комит
0b99331ddc
1 измењених фајлова са 36 додато и 14 уклоњено
  1. 36 14
      pages/index/index.js

+ 36 - 14
pages/index/index.js

@@ -22,7 +22,7 @@ import lexin_message from '../../utils/lexin/message.js';
 import {
   BtHelper
 } from '../../devices/bt_helper.js';
-import event_bus from '../../utils/event_bus.js';
+import event_bus, { addNotification } from '../../utils/event_bus.js';
 import {
   EnumCmdEvent,
   CmdEvent
@@ -238,21 +238,31 @@ Page({
       BtHelper.getInstance().startScan(null, null, function (res) { });
       var isFirst = true;
       console.log("开始监听蓝牙设备");
-      ///再秒再对比一次
-      that.stopIntervalId1();
-      let count = 0;
-      that.data.intervalId1 = setInterval(async function () {
-        isFirst = false;
-        that.compareList();
-        // if (count > 4) {
-        that.stopIntervalId1();
-        // } else {
-        //   count++;
-        // }
-      }, isFirst ? 3 * 1000 : 10 * 1000);
+      that.startBleTimer(isFirst);
+      that.addNotification()
     }
   },
+  startBleTimer(isFirst) {
+    let that = this;
+    ///再秒再对比一次
+    that.stopIntervalId1();
+    let count = 0;
+    that.data.intervalId1 = setInterval(async function () {
+      console.log("开始扫描蓝牙设备");
 
+      that.compareList();
+      // if (count > 4) {
+      if (isFirst) {
+        that.stopIntervalId1();
+        that.startBleTimer(false);
+      }
+      // } else {
+      //   count++;
+      // }
+      isFirst = false;
+    }, isFirst ? 3 * 1000 : 10 * 1000);
+
+  },
   /// 对比蓝牙数据
   compareList() {
 
@@ -289,7 +299,7 @@ Page({
         if (isConnected) {
           device.state = 'online';
           that.addBlueDevice(device);
-          that.stopIntervalId1();
+          // that.stopIntervalId1();
         } else {
           device.state = 'offline';
         }
@@ -773,10 +783,21 @@ Page({
       deviceListSelect: 0,
       autoConnected: true,
     });
+    that.addNotification()
+  },
 
+  addNotification() {
+    let that = this;
     event_bus.removeNotification(CmdEvent.eventName, that);
     event_bus.addNotification(CmdEvent.eventName, function (event) {
       if (event.cmdEvent == EnumCmdEvent.onoffline) {
+        let isFirst = false;
+        // 有下线就开启监听扫描
+        if (event.commonValue == "online") {
+          that.stopIntervalId1();
+        } else {
+          that.startBleTimer(isFirst);
+        }
 
         console.log("首页ble通知:", event.deviceId)
         let disDevice = that.data.deviceList.find(item => {
@@ -1051,6 +1072,7 @@ Page({
   onUnload() {
     var that = this;
     event_bus.removeNotification(CmdEvent.eventName, that)
+    console.log("界面onUnload")
     that.stopIntervalId1();
     that.stopIntervalId2();
     if (!strings.isEmpty(that.data.intervalId)) {