Bladeren bron

feature: 处理蓝牙对比

Damon 8 maanden geleden
bovenliggende
commit
2e16400f35
5 gewijzigde bestanden met toevoegingen van 165 en 26 verwijderingen
  1. 91 13
      devices/ble_manager.js
  2. 17 3
      devices/bt_helper.js
  3. 7 6
      pages/connectBle/connectBle.js
  4. 48 1
      pages/index/index.js
  5. 2 3
      pages/index/index.wxml

+ 91 - 13
devices/ble_manager.js

@@ -7,6 +7,23 @@ class bleManager {
     that.hasPermission = false;
     that.hasPermission = false;
     that.scanDevices = [];
     that.scanDevices = [];
     that.publicDevice = null;
     that.publicDevice = null;
+    ///用来对比列表
+    that.compareList = [];
+    that.searching = false;
+  }
+
+  ///获取比较的数据
+  getCompareList() {
+    return this.compareList;
+  }
+
+  ///处理当前正在连接中,对比暂停
+  setSearching(search) {
+    this.searching = search;
+  }
+
+  getSearching() {
+    return this.searching;
   }
   }
 
 
   /// 监控蓝牙打开状态
   /// 监控蓝牙打开状态
@@ -18,32 +35,91 @@ class bleManager {
   }
   }
 
 
   ///监听搜索设备列表
   ///监听搜索设备列表
-  getBluetoothDevices(callBack) {
+  getBluetoothDevices(callBack, toCompare) {
     var that = this;
     var that = this;
     wx.onBluetoothDeviceFound(function (res) {
     wx.onBluetoothDeviceFound(function (res) {
       ///第一种情况
       ///第一种情况
       if (res.deviceId) {
       if (res.deviceId) {
-        if (res.name == getApp().globalData.connectWillDevice.clientType) {
-          res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
-          callBack(res);
+        if (!toCompare) {
+          if (res.name == getApp().globalData.connectWillDevice.clientType) {
+            res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
+            callBack(res);
+          }
+        } else {
+          if (res.name != "") {
+            if (that.compareList.length > 0) {
+              var has = false;
+              for (var i = 0; i < that.compareList.length; i++) {
+                if (res.deviceId == that.compareList[i].deviceId) {
+                  has = true;
+                  break;
+                }
+              }
+              if (!has) {
+                that.compareList.push(res);
+              }
+            } else {
+              that.compareList.push(res);
+            }
+          }
         }
         }
       }
       }
       ///第二种情况
       ///第二种情况
       else if (res.devices) {
       else if (res.devices) {
-        for (var i = 0; i < res.devices.length; i++) {
-          var temp = res.devices[i];
-          if (temp.name == getApp().globalData.connectWillDevice.clientType) {
-            temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
-            callBack(temp);
-            break;
+        if (!toCompare) {
+          for (var i = 0; i < res.devices.length; i++) {
+            var temp = res.devices[i];
+            if (temp.name == getApp().globalData.connectWillDevice.clientType) {
+              temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
+              callBack(temp);
+              break;
+            }
+          }
+        } else {
+          for (var i = 0; i < res.devices.length; i++) {
+            if (that.compareList.length > 0) {
+              var has = false;
+              for (var j = 0; j < that.compareList.length; j++) {
+                if (res.devices[i].name != "") {
+                  if (res.devices[i].deviceId == that.compareList[j].deviceId) {
+                    has = true;
+                    break;
+                  }
+                }
+              }
+              if (!has) {
+                that.compareList.push(res.devices[i]);
+              }
+            } else {
+              that.compareList.push(res.devices[i]);
+            }
           }
           }
         }
         }
       }
       }
       ///第三种情况
       ///第三种情况
       else if (res[0]) {
       else if (res[0]) {
-        if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
-          res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
-          callBack(res[0]);
+        if (!toCompare) {
+          if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
+            res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
+            callBack(res[0]);
+          }
+        } else {
+          if (res[0].name != "") {
+            if (that.compareList.length > 0) {
+              var has = false;
+              for (var i = 0; i < that.compareList.length; i++) {
+                if (res[0].deviceId == that.compareList[i].deviceId) {
+                  has = true;
+                  break;
+                }
+              }
+              if (!has) {
+                that.compareList.push(res[0]);
+              }
+            } else {
+              that.compareList.push(res[0]);
+            }
+          }
         }
         }
       }
       }
     });
     });
@@ -56,6 +132,7 @@ class bleManager {
 
 
   // 开始搜索蓝牙设备
   // 开始搜索蓝牙设备
   startScan(boolean) {
   startScan(boolean) {
+    var that = this;
     wx.closeBluetoothAdapter({
     wx.closeBluetoothAdapter({
       complete: function (res) {
       complete: function (res) {
         wx.openBluetoothAdapter({
         wx.openBluetoothAdapter({
@@ -70,6 +147,7 @@ class bleManager {
               allowDuplicatesKey: false,
               allowDuplicatesKey: false,
               success: function (res) {
               success: function (res) {
                 boolean(true);
                 boolean(true);
+                that.compareList = [];
               },
               },
               fail(err) {},
               fail(err) {},
             })
             })

+ 17 - 3
devices/bt_helper.js

@@ -73,18 +73,32 @@ class BtHelper {
     }, this)
     }, this)
   }
   }
 
 
+  ///获取比较的数据
+  getCompareList() {
+    return this.bleManager.getCompareList();
+  }
+
+  ///处理当前正在连接中,对比暂停
+  setSearching(search) {
+    this.bleManager.setSearching(search);
+  }
+
+  getSearching() {
+    return this.bleManager.getSearching();
+  }
+
   ///初始化蓝牙适配器
   ///初始化蓝牙适配器
   initBluetoothAdapter() {
   initBluetoothAdapter() {
     var that = this;
     var that = this;
     that.bleManager.initBluetoothAdapter();
     that.bleManager.initBluetoothAdapter();
   }
   }
 
 
-  ///获取蓝牙设备
-  getBluetoothDevices(callBack) {
+  ///获取蓝牙设备 connect:true,去连接,false,去对比
+  getBluetoothDevices(callBack, toCompare) {
     var that = this;
     var that = this;
     that.bleManager.getBluetoothDevices(function (res) {
     that.bleManager.getBluetoothDevices(function (res) {
       callBack(res);
       callBack(res);
-    });
+    }, toCompare);
   }
   }
 
 
   ///搜索蓝牙设备
   ///搜索蓝牙设备

+ 7 - 6
pages/connectBle/connectBle.js

@@ -3,7 +3,7 @@ const app = getApp();
 const {
 const {
   BtHelper
   BtHelper
 } = require('../../devices/bt_helper');
 } = require('../../devices/bt_helper');
-const toastUtil = require('../../utils/toast_util');
+
 import route_constant from '../../utils/route_constant.js'
 import route_constant from '../../utils/route_constant.js'
 import route_util from '../../utils/route_util.js';
 import route_util from '../../utils/route_util.js';
 Page({
 Page({
@@ -26,6 +26,8 @@ Page({
   },
   },
 
 
   onLoad(options) {
   onLoad(options) {
+    BtHelper.getInstance().setSearching(true);
+
     var that = this;
     var that = this;
     var json = JSON.parse(options.param)
     var json = JSON.parse(options.param)
     console.log("要连接设备:", json)
     console.log("要连接设备:", json)
@@ -40,15 +42,16 @@ Page({
       BtHelper.getInstance().stopSearch();
       BtHelper.getInstance().stopSearch();
       that.data.connectDevice.deviceId = res.deviceId;
       that.data.connectDevice.deviceId = res.deviceId;
       that.data.connectDevice.connectable = res.connectable;
       that.data.connectDevice.connectable = res.connectable;
-    });
+    }, false);
     that.startSearch();
     that.startSearch();
   },
   },
 
 
   ///搜索设备
   ///搜索设备
-  startSearch() {
+  async startSearch() {
     var that = this;
     var that = this;
     that.setStatus(0);
     that.setStatus(0);
     ///搜索到对应的设备
     ///搜索到对应的设备
+    await BtHelper.getInstance().stopSearch();
     BtHelper.getInstance().startScan(
     BtHelper.getInstance().startScan(
       function (b) {
       function (b) {
         if (!b) {
         if (!b) {
@@ -59,10 +62,7 @@ Page({
   },
   },
 
 
   connectToDvice() {
   connectToDvice() {
-    toastUtil.show("开始连接设备")
     var that = this;
     var that = this;
-    //  搜索到
-    BtHelper.getInstance().stopSearch();
     BtHelper.getInstance().connect(that.data.connectDevice, function (data) {
     BtHelper.getInstance().connect(that.data.connectDevice, function (data) {
       console.log("连接成功:", data)
       console.log("连接成功:", data)
       that.setStatus(data ? 3 : 4)
       that.setStatus(data ? 3 : 4)
@@ -160,6 +160,7 @@ Page({
 
 
   ///关闭界面时候触发
   ///关闭界面时候触发
   onUnload: function () {
   onUnload: function () {
+    BtHelper.getInstance().setSearching(false);
     BtHelper.getInstance().stopSearch();
     BtHelper.getInstance().stopSearch();
   }
   }
 })
 })

+ 48 - 1
pages/index/index.js

@@ -48,7 +48,8 @@ Page({
     ////我的界面
     ////我的界面
     loginStatus: true,
     loginStatus: true,
     nickName: "未登录",
     nickName: "未登录",
-    userPic: './../../img/head_pic.png'
+    userPic: './../../img/head_pic.png',
+    intervalId: null,
   },
   },
 
 
   onLoad(options) {
   onLoad(options) {
@@ -133,6 +134,48 @@ Page({
       that.updateDeviceList(list, true);
       that.updateDeviceList(list, true);
       // that.tryConnectBle()
       // that.tryConnectBle()
     }
     }
+
+    ///监听蓝牙设备
+    BtHelper.getInstance().getBluetoothDevices(function (res) {}, true);
+    ///3秒去处理一下
+    that.compareList();
+    that.data.intervalId = setInterval(async function () {
+      that.compareList();
+    }, 3 * 1000);
+  },
+
+  compareList() {
+    var that = this;
+    if (!BtHelper.getInstance().getSearching()) {
+      var compareList = BtHelper.getInstance().getCompareList();
+      if (compareList.length > 0) {
+        ///对比在线的蓝牙设备
+        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"
+              if (tempItem.deviceId === compareList[j].deviceId) {
+                has = true;
+                isChanged = true;
+                break;
+              }
+            }
+            tempItem.state = has ? "online" : "offline";
+          }
+        }
+        if (isChanged) {
+          that.updateDeviceList(tempList, false);
+        }
+      }
+      // await BtHelper.getInstance().stopSearch();
+      BtHelper.getInstance().startScan(
+        function (b) {},
+      );
+    }
   },
   },
 
 
   addNewDeviceId() {
   addNewDeviceId() {
@@ -504,6 +547,10 @@ Page({
   },
   },
 
 
   onUnload() {
   onUnload() {
+    if (!strings.isEmpty(that.data.intervalId)) {
+      clearInterval(that.data.intervalId);
+      that.data.intervalId = null;
+    }
     if (app.globalData.client === null) {
     if (app.globalData.client === null) {
       return;
       return;
     };
     };

+ 2 - 3
pages/index/index.wxml

@@ -17,13 +17,12 @@
       <image mode="heightFix" src="./../../img/add_device.png" style="width: 48rxp;height: 48rpx;"></image>
       <image mode="heightFix" src="./../../img/add_device.png" style="width: 48rxp;height: 48rpx;"></image>
       <view style="color: white; margin-left: 8rpx; font-size: 32rpx;">添加设备</view>
       <view style="color: white; margin-left: 8rpx; font-size: 32rpx;">添加设备</view>
     </view>
     </view>
-    
+
     <scroll-view scroll-y="true" style="height: calc(100vh - 118rpx - 14rpx - 20rpx  - 88rpx - 64rpx - 58rpx - {{bannerList.length>0?296:0}}rpx -  {{navBarHeight}}px);margin-top: 22rpx;padding-top: 10rpx; ">
     <scroll-view scroll-y="true" style="height: calc(100vh - 118rpx - 14rpx - 20rpx  - 88rpx - 64rpx - 58rpx - {{bannerList.length>0?296:0}}rpx -  {{navBarHeight}}px);margin-top: 22rpx;padding-top: 10rpx; ">
       <view wx:if="{{deviceList}}">
       <view wx:if="{{deviceList}}">
         <block wx:key="key" wx:for-item="item" wx:for-index="index" wx:for="{{deviceList}}">
         <block wx:key="key" wx:for-item="item" wx:for-index="index" wx:for="{{deviceList}}">
-          {{item.state && item.state === 'online'&&deviceListIndex === index}}
           <view class="item_public" style="width: calc(100vw - 72rpx);border: {{item.state && item.state === 'online'&&deviceListIndex === index  ? '4rpx solid #6546A3' : item.state && item.state === 'online'?'2rpx solid #6546A3':'2rpx solid #F2F5F7'}}; margin-left: {{item.state && item.state === 'online'&&deviceListIndex === index  ?0:4}}rpx;" data-item="{{item}}" data-index="{{index}}" bind:tap="onTapItem">
           <view class="item_public" style="width: calc(100vw - 72rpx);border: {{item.state && item.state === 'online'&&deviceListIndex === index  ? '4rpx solid #6546A3' : item.state && item.state === 'online'?'2rpx solid #6546A3':'2rpx solid #F2F5F7'}}; margin-left: {{item.state && item.state === 'online'&&deviceListIndex === index  ?0:4}}rpx;" data-item="{{item}}" data-index="{{index}}" bind:tap="onTapItem">
-          
+
             <image class="item_public_image" src="./../../img/min.png" style="opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};"></image>
             <image class="item_public_image" src="./../../img/min.png" style="opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};"></image>
             <view class="item_public_right" style="margin-right: 26rpx; opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};">
             <view class="item_public_right" style="margin-right: 26rpx; opacity: {{item.state && item.state === 'online' ? '1' : '0.5'}};">
               <view style="height: 190rpx; margin-top: 37rpx; margin-left: 32rpx; flex-direction: column; display: flex;">
               <view style="height: 190rpx; margin-top: 37rpx; margin-left: 32rpx; flex-direction: column; display: flex;">