瀏覽代碼

feature: 处理重新搜索数据的问题

Damon 7 月之前
父節點
當前提交
39ae92e3d8
共有 2 個文件被更改,包括 127 次插入68 次删除
  1. 117 58
      devices/ble_manager.js
  2. 10 10
      pages/index/index.js

+ 117 - 58
devices/ble_manager.js

@@ -139,6 +139,27 @@ class bleManager {
     });
   }
 
+  ///获取已连接的设备
+  getConnectedDevices() {
+    wx.getBluetoothDevices({
+      success: (res) => {
+        if (res.devices && res.devices.length > 0) {
+          for (var i = 0; i < res.devices.length; i++) {
+            var temp = res.devices[i];
+            if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
+              temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
+              that.callBackConnect(temp);
+              break;
+            }
+          }
+        }
+      },
+      fail: (err) => {
+        console.error('获取蓝牙设备列表失败', err);
+      }
+    });
+  }
+
   ///获取数据
   buf2hex(buffer) {
     return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
@@ -196,15 +217,53 @@ class bleManager {
       waitMills = delayMiliis - reduce;
     }
 
-    if (waitMills > 0) {
-      await that.delay(waitMills);
-    }
+    // if (waitMills > 0) {
+    //   await that.delay(waitMills);
+    // }
 
     if (callBackConnect == null && lastPageRoute == connectBleRoot) {
       that.doStartScaning = false;
       return;
     }
 
+    wx.stopBluetoothDevicesDiscovery({
+      success: (res) => {
+        wx.startBluetoothDevicesDiscovery({
+          allowDuplicatesKey: true,
+          success: function (res) {
+            that.getConnectedDevices();
+            that.doStartScaning = false;
+            that.requestBlueTime = that.getCurrentMills();
+            if (boolean != null) {
+              boolean(true);
+            }
+            that.setConnectWillDevice(connectWillDevice);
+            that.setCallBackConnect(callBackConnect);
+            that.compareList = [];
+          },
+          fail(err) {
+            that.doStartScaning = false;
+            that.requestBlueTime = that.getCurrentMills();
+            if (boolean != null) {
+              boolean(false);
+            }
+          },
+        })
+      },
+      fail: (err) => {
+        wx.showModal({
+          title: '提示',
+          content: '请检查手机蓝牙是否打开',
+          showCancel: false,
+          success: function (res) {}
+        });
+        that.doStartScaning = false;
+        if (boolean != null) {
+          boolean(false);
+        }
+      }
+    });
+
     // wx.startBluetoothDevicesDiscovery({
     //   allowDuplicatesKey: true,
     //   success: function (res) {
@@ -226,61 +285,61 @@ class bleManager {
     //   },
     // })
 
-    wx.closeBluetoothAdapter({
-      complete: function (res) {
-        wx.openBluetoothAdapter({
-          success: function (res) {
-            wx.getBluetoothAdapterState({
-              success: function (res) {
-                that.doStartScaning = false;
-                console.log("gadsfasdfqwerqwerqwerqr==xxx==" + JSON.stringify(res));
-              },
-              fail(err) {
-                that.doStartScaning = false;
-                console.log("gadsfasdfqwerqwerqwerqr==yyyy==" + JSON.stringify(res));
-                if (boolean != null) {
-                  boolean(false);
-                }
-              },
-            })
-            wx.startBluetoothDevicesDiscovery({
-              allowDuplicatesKey: false,
-              success: function (res) {
-                console.log("gadsfasdfqwerqwerqwerqr==mmmm==" + JSON.stringify(res));
-                that.doStartScaning = false;
-                that.requestBlueTime = that.getCurrentMills();
-                if (boolean != null) {
-                  boolean(true);
-                }
-                that.setConnectWillDevice(connectWillDevice);
-                that.setCallBackConnect(callBackConnect);
-                that.compareList = [];
-              },
-              fail(err) {
-                console.log("gadsfasdfqwerqwerqwerqr==nnnn==" + JSON.stringify(err));
-                that.doStartScaning = false;
-                that.requestBlueTime = that.getCurrentMills();
-                if (boolean != null) {
-                  boolean(false);
-                }
-              },
-            })
-          },
-          fail: function (res) {
-            wx.showModal({
-              title: '提示',
-              content: '请检查手机蓝牙是否打开',
-              showCancel: false,
-              success: function (res) {}
-            });
-            that.doStartScaning = false;
-            if (boolean != null) {
-              boolean(false);
-            }
-          }
-        })
-      }
-    })
+    // wx.closeBluetoothAdapter({
+    //   complete: function (res) {
+    //     wx.openBluetoothAdapter({
+    //       success: function (res) {
+    //         wx.getBluetoothAdapterState({
+    //           success: function (res) {
+    //             that.doStartScaning = false;
+    //             console.log("gadsfasdfqwerqwerqwerqr==xxx==" + JSON.stringify(res));
+    //           },
+    //           fail(err) {
+    //             that.doStartScaning = false;
+    //             console.log("gadsfasdfqwerqwerqwerqr==yyyy==" + JSON.stringify(res));
+    //             if (boolean != null) {
+    //               boolean(false);
+    //             }
+    //           },
+    //         })
+    //         wx.startBluetoothDevicesDiscovery({
+    //           allowDuplicatesKey: false,
+    //           success: function (res) {
+    //             console.log("gadsfasdfqwerqwerqwerqr==mmmm==" + JSON.stringify(res));
+    //             that.doStartScaning = false;
+    //             that.requestBlueTime = that.getCurrentMills();
+    //             if (boolean != null) {
+    //               boolean(true);
+    //             }
+    //             that.setConnectWillDevice(connectWillDevice);
+    //             that.setCallBackConnect(callBackConnect);
+    //             that.compareList = [];
+    //           },
+    //           fail(err) {
+    //             console.log("gadsfasdfqwerqwerqwerqr==nnnn==" + JSON.stringify(err));
+    //             that.doStartScaning = false;
+    //             that.requestBlueTime = that.getCurrentMills();
+    //             if (boolean != null) {
+    //               boolean(false);
+    //             }
+    //           },
+    //         })
+    //       },
+    //       fail: function (res) {
+    //         wx.showModal({
+    //           title: '提示',
+    //           content: '请检查手机蓝牙是否打开',
+    //           showCancel: false,
+    //           success: function (res) {}
+    //         });
+    //         that.doStartScaning = false;
+    //         if (boolean != null) {
+    //           boolean(false);
+    //         }
+    //       }
+    //     })
+    //   }
+    // })
   }
 
   // 停止搜索

+ 10 - 10
pages/index/index.js

@@ -128,7 +128,7 @@ Page({
 
   onDeviceLoad() {
     var that = this;
-    BtHelper.getInstance().initBluetoothAdapter();
+    // BtHelper.getInstance().initBluetoothAdapter();
     var deviceList = wx.getStorageSync("deviceList") || "";
     if (!strings.isEmpty(deviceList)) {
       var list = JSON.parse(deviceList);
@@ -141,15 +141,15 @@ Page({
     ///3秒去处理一下
     BtHelper.getInstance().startScan(null, null, null);
     ///先接收数据
-    var isFirst = true;
-    setTimeout(() => {
-      that.compareList();
-      ///再秒再对比一次
-      that.data.intervalId = setInterval(async function () {
-        isFirst = false;
-        that.compareList();
-      }, isFirst ? 10 * 1000 : 20 * 1000);
-    }, 3 * 1000);
+    // var isFirst = true;
+    // setTimeout(() => {
+    //   that.compareList();
+    //   ///再秒再对比一次
+    //   that.data.intervalId = setInterval(async function () {
+    //     isFirst = false;
+    //     that.compareList();
+    //   }, isFirst ? 10 * 1000 : 20 * 1000);
+    // }, 3 * 1000);
   },
 
   async compareList() {