Просмотр исходного кода

feature: 处理搜索对比和搜索设备合并数据

Damon 8 месяцев назад
Родитель
Сommit
95b9137bb2
5 измененных файлов с 45 добавлено и 38 удалено
  1. 0 4
      app.js
  2. 24 17
      devices/ble_manager.js
  3. 14 9
      devices/bt_helper.js
  4. 5 5
      pages/connectBle/connectBle.js
  5. 2 3
      pages/index/index.js

+ 0 - 4
app.js

@@ -27,10 +27,6 @@ App({
     client: null,
     oneInitBluetooth: true,
 
-    ///即将要连接的设备
-    connectWillDevice: {},
-
-
     mDeviceList: [],
     // 当前设备
     // device: {},

+ 24 - 17
devices/ble_manager.js

@@ -7,9 +7,9 @@ class bleManager {
     that.hasPermission = false;
     that.scanDevices = [];
     that.publicDevice = null;
-    ///用来对比列表
     that.compareList = [];
-    that.searching = false;
+    that.connectWillDevice = null;
+    that.callBackConnect = null;
   }
 
   ///获取比较的数据
@@ -17,13 +17,20 @@ class bleManager {
     return this.compareList;
   }
 
-  ///处理当前正在连接中,对比暂停
-  setSearching(search) {
-    this.searching = search;
+  getConnectWillDevice() {
+    return this.connectWillDevice;
   }
 
-  getSearching() {
-    return this.searching;
+  setConnectWillDevice(connectWillDevice) {
+    this.connectWillDevice = connectWillDevice;
+  }
+
+  getCallBackConnect() {
+    return this.callBackConnect;
+  }
+
+  setCallBackConnect(callBackConnect) {
+    this.callBackConnect = callBackConnect;
   }
 
   /// 监控蓝牙打开状态
@@ -35,15 +42,15 @@ class bleManager {
   }
 
   ///监听搜索设备列表
-  getBluetoothDevices(callBack, toCompare) {
+  getBluetoothDevices() {
     var that = this;
     wx.onBluetoothDeviceFound(function (res) {
       ///第一种情况
       if (res.deviceId) {
-        if (!toCompare) {
-          if (res.name == getApp().globalData.connectWillDevice.clientType) {
+        if (that.callBackConnect != null) {
+          if (that.connectWillDevice != null && res.name == that.connectWillDevice.clientType) {
             res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
-            callBack(res);
+            that.callBackConnect(res);
           }
         } else {
           if (res.name != "") {
@@ -66,12 +73,12 @@ class bleManager {
       }
       ///第二种情况
       else if (res.devices) {
-        if (!toCompare) {
+        if (that.callBackConnect != null) {
           for (var i = 0; i < res.devices.length; i++) {
             var temp = res.devices[i];
-            if (temp.name == getApp().globalData.connectWillDevice.clientType) {
+            if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
               temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
-              callBack(temp);
+              that.callBackConnect(temp);
               break;
             }
           }
@@ -98,10 +105,10 @@ class bleManager {
       }
       ///第三种情况
       else if (res[0]) {
-        if (!toCompare) {
-          if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
+        if (that.callBackConnect != null) {
+          if (that.connectWillDevice != null && res[0].name == that.connectWillDevice.clientType) {
             res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
-            callBack(res[0]);
+            that.callBackConnect(res[0]);
           }
         } else {
           if (res[0].name != "") {

+ 14 - 9
devices/bt_helper.js

@@ -78,13 +78,20 @@ class BtHelper {
     return this.bleManager.getCompareList();
   }
 
-  ///处理当前正在连接中,对比暂停
-  setSearching(search) {
-    this.bleManager.setSearching(search);
+  getConnectWillDevice() {
+    return this.bleManager.connectWillDevice;
   }
 
-  getSearching() {
-    return this.bleManager.getSearching();
+  setConnectWillDevice(connectWillDevice) {
+    this.bleManager.setConnectWillDevice(connectWillDevice);
+  }
+
+  getCallBackConnect() {
+    return this.bleManager.getCallBackConnect();
+  }
+
+  setCallBackConnect(callBackConnect) {
+    this.bleManager.setCallBackConnect(callBackConnect);
   }
 
   ///初始化蓝牙适配器
@@ -94,11 +101,9 @@ class BtHelper {
   }
 
   ///获取蓝牙设备 connect:true,去连接,false,去对比
-  getBluetoothDevices(callBack, toCompare) {
+  getBluetoothDevices() {
     var that = this;
-    that.bleManager.getBluetoothDevices(function (res) {
-      callBack(res);
-    }, toCompare);
+    that.bleManager.getBluetoothDevices();
   }
 
   ///搜索蓝牙设备

+ 5 - 5
pages/connectBle/connectBle.js

@@ -26,7 +26,6 @@ Page({
   },
 
   onLoad(options) {
-    BtHelper.getInstance().setSearching(true);
     var that = this;
     var json = JSON.parse(options.param)
     console.log("要连接设备:", json)
@@ -35,13 +34,13 @@ Page({
     });
 
     ///准备连接的设备
-    app.globalData.connectWillDevice = that.data.connectDevice;
-    BtHelper.getInstance().getBluetoothDevices(function (res) {
+    BtHelper.getInstance().setConnectWillDevice(that.data.connectDevice);
+    BtHelper.getInstance().setCallBackConnect(function (res) {
       that.setStatus(2);
       BtHelper.getInstance().stopSearch();
       that.data.connectDevice.deviceId = res.deviceId;
       that.data.connectDevice.connectable = res.connectable;
-    }, false);
+    });
     that.startSearch();
   },
 
@@ -157,7 +156,8 @@ Page({
 
   ///关闭界面时候触发
   onUnload: function () {
-    BtHelper.getInstance().setSearching(false);
+    BtHelper.getInstance().setConnectWillDevice(null);
+    BtHelper.getInstance().setCallBackConnect(null);
     BtHelper.getInstance().stopSearch();
   }
 })

+ 2 - 3
pages/index/index.js

@@ -136,7 +136,7 @@ Page({
     }
 
     ///监听蓝牙设备
-    BtHelper.getInstance().getBluetoothDevices(function (res) {}, true);
+    BtHelper.getInstance().getBluetoothDevices();
     ///3秒去处理一下
     that.compareList();
     that.data.intervalId = setInterval(async function () {
@@ -146,7 +146,7 @@ Page({
 
   compareList() {
     var that = this;
-    if (!BtHelper.getInstance().getSearching()) {
+    if (BtHelper.getInstance().getCallBackConnect() == null) {
       var compareList = BtHelper.getInstance().getCompareList();
       if (compareList.length > 0) {
         ///对比在线的蓝牙设备
@@ -660,7 +660,6 @@ Page({
     } else if (item.state === "offline") {
       console.log("去连接蓝牙")
     } else {
-      console.log("gasdfadsfqwwerqweqrqr=====00==" + JSON.stringify(item));
       ///item
       // {"connectType":1,"deviceId":"E4:9F:80:09:40:EC","name":"E4:9F:80:09:40:EC","state":"online","ProdModel":"MW-SR1(4G_WIFI)","devName":"猫王音响x阿基米德 SR1 妙播收音机"}
       item.name = item.devName;