Browse Source

feature: 没有连接的设备,会自动连接第一个在线蓝牙设备

Damon 8 months ago
parent
commit
d1fa3864b5
1 changed files with 50 additions and 21 deletions
  1. 50 21
      pages/index/index.js

+ 50 - 21
pages/index/index.js

@@ -50,6 +50,7 @@ Page({
     nickName: "未登录",
     userPic: './../../img/head_pic.png',
     intervalId: null,
+    autoConnected: false,
   },
 
   onLoad(options) {
@@ -157,34 +158,55 @@ Page({
         ///对比在线的蓝牙设备
         var isChanged = false;
         var tempList = that.data.deviceList;
-        for (var i = 0; i < tempList.length; i++) {
-          var tempItem = tempList[i];
-          if (tempItem.connectType != 3) {
-            var has = false;
-            for (var j = 0; j < compareList.length; j++) {
-              // "state":"online" "offline"
-              if (tempItem.deviceId === compareList[j].deviceId) {
-                has = true;
-                break;
+        if (tempList.length > 0) {
+          for (var i = 0; i < tempList.length; i++) {
+            var tempItem = tempList[i];
+            if (tempItem.connectType != 3) {
+              var has = false;
+              for (var j = 0; j < compareList.length; j++) {
+                // "state":"online" "offline"
+                if (tempItem.deviceId === compareList[j].deviceId) {
+                  has = true;
+                  break;
+                }
               }
-            }
 
-            if (has) {
-              if (tempItem.state != "online") {
-                isChanged = true;
-                tempItem.state = "online";
+              if (has) {
+                if (tempItem.state != "online") {
+                  isChanged = true;
+                  tempItem.state = "online";
+                }
+              } else {
+                if (tempItem.state != "offline") {
+                  isChanged = true;
+                  tempItem.state = "offline";
+                }
               }
-            } else {
-              if (tempItem.state != "offline") {
-                isChanged = true;
-                tempItem.state = "offline";
+            }
+          }
+          if (isChanged) {
+            that.updateDeviceList(tempList, false);
+          }
+
+          ///没有连接则连接第一个在线的蓝牙
+          if (!that.data.autoConnected) {
+            var dList = that.data.deviceList;
+            if (dList.length > 0) {
+              for (var i = 0; i < dList.length; i++) {
+                var item = dList[i];
+                if (item.connectType == 1 && tempItem.state == "online") {
+                  item.name = item.devName;
+                  BtHelper.getInstance().connect(item, function (data) {
+                    if (data) {
+                      that.addConnectBlueDevice(item);
+                    }
+                  });
+                  break;
+                }
               }
             }
           }
         }
-        if (isChanged) {
-          that.updateDeviceList(tempList, false);
-        }
       }
       BtHelper.getInstance().startScan(null, null, null);
     }
@@ -453,6 +475,7 @@ Page({
     deviceList.unshift(tempDevice);
     that.setData({
       deviceListIndex: 0,
+      autoConnected: true,
       deviceList: deviceList,
     });
 
@@ -705,6 +728,7 @@ Page({
 
     that.setData({
       deviceListIndex: index,
+      autoConnected: true,
       thisDeviceMac: `AIrSMArT_${that.data.deviceList[index].deviceId.split("BLUFI_")[1]}`
     });
 
@@ -806,6 +830,7 @@ Page({
               if (v.name === name) {
                 that.setData({
                   deviceListIndex: index,
+                  autoConnected: true,
                 });
               }
             });
@@ -851,10 +876,12 @@ Page({
       that.setData({
         indexPage: 0,
         deviceListIndex: 0,
+        autoConnected: true,
       });
     } else {
       that.setData({
         deviceListIndex: 0,
+        autoConnected: true,
       });
     }
   },
@@ -869,10 +896,12 @@ Page({
       that.setData({
         indexPage: 0,
         deviceListIndex: 0,
+        autoConnected: true,
       });
     } else {
       that.setData({
         deviceListIndex: 0,
+        autoConnected: true,
       });
     }
   },