Browse Source

feature: 处理外部类与内部类的问题

Damon 8 months ago
parent
commit
f5a2f70197
3 changed files with 555 additions and 563 deletions
  1. 534 538
      devices/ble_manager.js
  2. 6 5
      devices/bt_helper.js
  3. 15 20
      pages/connectBle/connectBle.js

File diff suppressed because it is too large
+ 534 - 538
devices/ble_manager.js


+ 6 - 5
devices/bt_helper.js

@@ -313,16 +313,17 @@ class BtHelper {
   }
 
   async search(stopCall) {
-    let res = await this.bleManager.startScan()
-    this.timer = null;
-    this.timer = setTimeout(() => {
-      this.stopSearch();
+    var that = this;
+    let res = await that.bleManager.startScan()
+    that.timer = null;
+    that.timer = setTimeout(() => {
+      that.stopSearch();
       if (stopCall) {
         stopCall()
       }
     }, 10000);
     // console.log(res);
-    // return await this._helper.search();
+    // return await that._helper.search();
   }
 
   async findDevices(callback) {

+ 15 - 20
pages/connectBle/connectBle.js

@@ -27,6 +27,12 @@ Page({
 
   onLoad(options) {
     var that = this;
+    let json = JSON.parse(options.param)
+    console.log("要连接设备:", json)
+    that.setData({
+      connectDevice: json
+    });
+
     var b = BtHelper.getInstance();
     that.data.btHelper = b;
     b.initBluetooth(function (adapterState, hasPermission) {
@@ -39,9 +45,6 @@ Page({
         //     console.log('MAC: ' + res.macaddress);
         //   }
         // });
-
-        ///搜索中
-        that.setStatus(0);
         that.startSearch();
       } else {
         ///搜索失败
@@ -50,11 +53,16 @@ Page({
     })
 
     that.findAllDevice()
+  },
 
-    let json = JSON.parse(options.param)
-    console.log("要连接设备:", json)
-    that.setData({
-      connectDevice: json
+  async startSearch() {
+    var that = this;
+    that.setStatus(0)
+    that.data.btHelper.search(async function () {
+      const connectedDevices = await that.getConnectedDevices()
+      if (connectedDevices.length == 0) {
+        that.setStatus(1)
+      }
     })
   },
 
@@ -83,7 +91,6 @@ Page({
         subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。"
         buttonTips = "重新搜索"
         deviceImg = deviceLinkResp.icon1
-
         break;
       case 2:
         // 搜索到
@@ -91,7 +98,6 @@ Page({
         subTips = ""
         buttonTips = "连接"
         deviceImg = deviceLinkResp.icon2
-
         break;
       case 3:
         // 连接成功
@@ -177,17 +183,6 @@ Page({
     return connectedDevices;
   },
 
-  async startSearch() {
-    var that = this;
-    that.setStatus(0)
-    that.data.btHelper.search(async function () {
-      const connectedDevices = await that.getConnectedDevices()
-      if (connectedDevices.length == 0) {
-        that.setStatus(1)
-      }
-    })
-  },
-
   findAllDevice() {
     var that = this;
     that.data.btHelper.findDevices(function (devices) {