Selaa lähdekoodia

feature: 解决蓝牙重连问题

Damon 7 kuukautta sitten
vanhempi
commit
13bab7ed90
3 muutettua tiedostoa jossa 38 lisäystä ja 6 poistoa
  1. 9 3
      pages/connectBle/connectBle.js
  2. 2 2
      pages/deviceConnect2/deviceConnect2.js
  3. 27 1
      pages/index/index.js

+ 9 - 3
pages/connectBle/connectBle.js

@@ -55,11 +55,18 @@ Page({
 
   async connectToDvice() {
     var that = this;
+    ///当前的同一个蓝牙
+    var isTheSame = getCurrentPages()[0].isTheSameBlue(that.data.connectDevice);
+    if (isTheSame) {
+      route_util.goBackRoute(route_constant.indexRoot);
+      return;
+    }
+
     ///断开蓝牙连接
     wx.showLoading({
       title: '请稍后'
     });
-    await getCurrentPages()[0].cancelCurrent();
+    // await getCurrentPages()[0].cancelCurrent();
     BtHelper.getInstance().connect(that.data.connectDevice, function (data) {
       console.log("连接成功:", data)
       wx.hideLoading();
@@ -67,8 +74,7 @@ Page({
       if (data) {
         that.data.connectDevice.connectType = 1;
         that.data.connectDevice.state = 'online';
-        var connectDevice = that.data.connectDevice;
-        getCurrentPages()[0].addConnectBlueDevice(connectDevice);
+        getCurrentPages()[0].addConnectBlueDevice(that.data.connectDevice);
         route_util.goBackRoute(route_constant.indexRoot);
       }
     });

+ 2 - 2
pages/deviceConnect2/deviceConnect2.js

@@ -132,7 +132,7 @@ Page({
     });
   },
 
-  Connect: async function (e) {
+  async Connect(e) {
     // if(!this.data.islanya) {
     //   return;
     // };
@@ -155,7 +155,7 @@ Page({
       isSubmit: true
     });
 
-    await getCurrentPages()[0].cancelCurrent();
+    // await getCurrentPages()[0].cancelCurrent();
     // 停止搜索
     xBlufi.notifyStartDiscoverBle({
       'isStart': false,

+ 27 - 1
pages/index/index.js

@@ -145,7 +145,7 @@ Page({
     that.data.intervalId = setInterval(async function () {
       isFirst = false;
       that.compareList();
-    }, isFirst ? 3 * 1000 : 10 * 1000);
+    }, isFirst ? 5 * 1000 : 10 * 1000);
   },
 
   async compareList() {
@@ -763,6 +763,31 @@ Page({
     });
   },
 
+
+  ///是否是同一个蓝牙
+  isTheSameBlue(connectDevice) {
+    var that = this;
+    var deviceListIndex = that.data.deviceListIndex;
+    if (deviceListIndex == null) {
+      return false;
+    }
+
+    var deviceList = that.data.deviceList;
+    if (deviceList.length > deviceListIndex) {
+      if (deviceList[deviceListIndex].connectType == 3) {
+        return false;
+      } else {
+        if (deviceList[deviceListIndex].deviceId == connectDevice.deviceId) {
+          return true;
+        } else {
+          return false;
+        }
+      }
+    } else {
+      return false;
+    }
+  },
+
   ///断开当前的
   async cancelCurrent() {
     var that = this;
@@ -782,6 +807,7 @@ Page({
 
   /// 断开蓝牙连接
   async cancelBlue(index) {
+    var that = this;
     const id = that.data.deviceList[index].deviceId;
     const deviceList = that.data.deviceList.filter((item, i) => {
       return id !== item.deviceId