Sfoglia il codice sorgente

fix:判断蓝牙设备的在线问题

zeng.chen 2 mesi fa
parent
commit
ab0612e0a5
2 ha cambiato i file con 20 aggiunte e 6 eliminazioni
  1. 19 5
      pages/pageA/index/index.js
  2. 1 1
      utils/lexin/scan.js

+ 19 - 5
pages/pageA/index/index.js

@@ -74,7 +74,7 @@ Page({
     that.onBannerLoad();
   },
 
-  onShow() {},
+  onShow() { },
 
   onLoginLoad() {
     var that = this;
@@ -157,7 +157,7 @@ Page({
       ///监听蓝牙设备
       BtHelper.getInstance().initBluetoothAdapter();
       BtHelper.getInstance().getBluetoothDevices();
-      BtHelper.getInstance().startScan(null, null, function (res) {});
+      BtHelper.getInstance().startScan(null, null, function (res) { });
       var isFirst = true;
       console.log("开始监听蓝牙设备");
       that.startBleTimer(isFirst, true);
@@ -233,7 +233,21 @@ Page({
         console.log("自动连接成功", isConnected)
         if (isConnected) {
           device.state = 'online';
-          that.updateDeviceList(device);
+          let hasDev = that.data.deviceList.find(item => {
+            return item.deviceId === device.deviceId;
+          })
+
+          let hasOnline = that.data.deviceList.find(item => {
+            return item.state === 'online';
+          })
+
+          if (hasDev && hasOnline) {
+            // 有设备&&有在线的设备, 直接更新
+            hasDev.state = 'online';
+            that.updateDeviceList(that.data.deviceList, false, false);
+          } else {
+            that.addBlueDevice(device);
+          }
           that.stopIntervalId1();
         } else {
           device.state = 'offline';
@@ -266,7 +280,7 @@ Page({
         case "connect":
           constant.lexinSubscribe.subscribeAllDevice();
           break;
-          ///再走有在线
+        ///再走有在线
         case "message_onoffline":
           constant.lexinConnect.searchOnlineDevice(payloads, function (list) {
             that.updateDeviceList(list, false, false);
@@ -329,7 +343,7 @@ Page({
           break;
         default:
       }
-    } catch (e) {}
+    } catch (e) { }
   },
 
   ///去连接设备数据

+ 1 - 1
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 && item.state === "online";
+    return item.connectType == 3;
   });
   if (isHasWifi) {
     //有wifi