Explorar o código

feature: 处理流程进度

Damon hai 7 meses
pai
achega
517108d277
Modificáronse 3 ficheiros con 93 adicións e 36 borrados
  1. 79 23
      devices/ble_manager.js
  2. 10 10
      devices/bt_helper.js
  3. 4 3
      pages/index/index.js

+ 79 - 23
devices/ble_manager.js

@@ -85,6 +85,9 @@ class bleManager {
         if (that.callBackConnect != null) {
           for (var i = 0; i < res.devices.length; i++) {
             var temp = res.devices[i];
+            if (temp.name == "MW-SR1(4G_WIFI)") {
+              console.log("gadsfqewrqewrqwerqrqr==111==" + JSON.stringify(res.devices[i]));
+            }
             if (that.connectWillDevice != null && temp.name == that.connectWillDevice.clientType) {
               temp.mac = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
               if (that.callBackConnect != null) {
@@ -143,9 +146,20 @@ class bleManager {
         }
       }
     });
+
+    ///监听已连接或没有连接
+    // {"deviceId":"E4:9F:80:09:40:EC","connected":false}
+    wx.onBLEConnectionStateChange((result) => {
+      if (result.connected) {
+
+      } else {
+
+      }
+      console.log("gadsfqewrqewrqwerqrqr==222==" + JSON.stringify(result));
+    });
   }
 
-  ///获取已连接的设备
+  ///获取 所有搜索到的蓝牙设备
   getConnectedDevices() {
     var that = this;
     wx.getBluetoothDevices({
@@ -167,6 +181,9 @@ class bleManager {
               var has = false;
               for (var j = 0; j < that.compareList.length; j++) {
                 if (res.devices[i].name != "") {
+                  if (res.devices[i].name == "MW-SR1(4G_WIFI)") {
+                    console.log("gadsfqewrqewrqwerqrqr==000==" + JSON.stringify(res.devices[i]));
+                  }
                   if (res.devices[i].deviceId == that.compareList[j].deviceId) {
                     has = true;
                     break;
@@ -188,6 +205,28 @@ class bleManager {
     });
   }
 
+  ///获取 已连接的蓝牙设备
+  getConnectedBluetoothDevices() {
+    // [{"deviceId":"E4:9F:80:09:40:EC","name":"MW-SR1(4G_WIFI)"}]
+    // wx.getConnectedBluetoothDevices({
+    //   success: (res) => {
+    //     console.log("时代发生的法==33==" + JSON.stringify(res));
+    //     if (res.devices.length > 0) {
+    //       console.log("时代发生的法==22==" + JSON.stringify(res.devices));
+    //       for (var i = 0; i < res.devices.length; i++) {
+    //         console.log("时代发生的法==11==" + JSON.stringify(res.devices[i]));
+    //         if (res.devices[i].name == "MW-SR1(4G_WIFI)") {
+    //           console.log("gasdfqwerqwerqdfasdfr==11==" + JSON.stringify(res.devices[i]));
+    //         }
+    //       }
+    //     } else {
+
+    //     }
+    //   },
+    //   fail: (err) => {}
+    // });
+  }
+
   ///获取数据
   buf2hex(buffer) {
     return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
@@ -280,6 +319,7 @@ class bleManager {
       allowDuplicatesKey: true,
       success: function (res) {
         that.getConnectedDevices();
+        that.getConnectedBluetoothDevices();
 
         that.doStartScaning = false;
         that.requestBlueTime = that.getCurrentMills();
@@ -311,7 +351,7 @@ class bleManager {
           resolve(res);
         },
         fail: (err) => {
-          reject(new Error('停止搜索失败'));
+          reject('停止搜索失败');
         }
       });
     });
@@ -323,10 +363,13 @@ class bleManager {
   }
 
   // 断开与指定设备的连接
-  disconnect() {
+  disconnect(mDevice) {
     var that = this;
     const strings = require('../utils/strings');
     let device = that.publicDevice;
+    if (mDevice != null) {
+      device = mDevice;
+    }
     if (strings.isEmpty(device)) {
       return;
     }
@@ -361,6 +404,39 @@ class bleManager {
     });
   }
 
+  // 连接到指定设备
+  async connectToDevice(device) {
+    var that = this;
+    return new Promise((resolve, reject) => {
+      console.log("开始连接蓝牙:", device.deviceId)
+
+      ///是否已配对
+      // wx.isBluetoothDevicePaired({
+      //   deviceId: device.deviceId,
+      //   ///{"isPaired":false,"errno":0,"errMsg":"isBluetoothDevicePaired:ok"}
+      //   success: (res) => {
+      //     console.log("gasdfqwerqwerqr==00===" + JSON.stringify(res));
+      //   },
+      //   fail: (err) => {
+      //     console.log("gasdfqwerqwerqr==111===" + JSON.stringify(err));
+      //   }
+      // })
+      wx.createBLEConnection({
+        deviceId: device.deviceId,
+        success: (res) => {
+          that.publicDevice = device
+          console.log('连接成功:', res);
+          resolve(true);
+        },
+        fail: (err) => {
+          that.publicDevice = null
+          console.error('连接失败:', err);
+          resolve(false);
+        }
+      });
+    });
+  }
+
   // 发送数据到指定设备
   async sendData(data) {
     var that = this
@@ -449,26 +525,6 @@ class bleManager {
     // }
     return isNot;
   }
-  // 连接到指定设备
-  async connectToDevice(device) {
-    var that = this;
-    return new Promise((resolve, reject) => {
-      console.log("开始连接蓝牙:", device.deviceId)
-      wx.createBLEConnection({
-        deviceId: device.deviceId,
-        success: (res) => {
-          that.publicDevice = device
-          console.log('连接成功:', res);
-          resolve(true);
-        },
-        fail: (err) => {
-          that.publicDevice = null
-          console.error('连接失败:', err);
-          resolve(false);
-        }
-      });
-    });
-  }
 
   // 发现服务
   discoverServices(deviceId) {

+ 10 - 10
devices/bt_helper.js

@@ -117,8 +117,8 @@ class BtHelper {
   }
 
   ///断开设备连接
-  async disconnect() {
-    await this.bleManager.disconnect()
+  async disconnect(device) {
+    await this.bleManager.disconnect(device)
   }
 
   handleCommand(event, mDevice) {
@@ -363,28 +363,28 @@ class BtHelper {
     this.getDeviceInfo()
   }
 
-  async connect(data, onChanged) {
+  async connect(device, onChanged) {
     // await this._helper.connect({ data, onChanged, isClick });
     var that = this;
-    await that.disconnect(data);
+    await that.disconnect(device);
     clearTimeout(that.timer);
     that.timer = null;
     that.bleManager.stopSearch()
-    var res = await that.bleManager.connectToDevice(data);
+    var res = await that.bleManager.connectToDevice(device);
     console.log(res ? '连接成功' : '连接失败');
     // that.setData({ connectedDeviceId: deviceId });
     if (res === false) {
       console.log("连接失败")
-      that.disconnect(data)
+      that.disconnect(device)
       if (onChanged) {
         onChanged(false)
       }
       return
     }
-    const serviceId = await that.bleManager.discoverServices(data.deviceId);
+    const serviceId = await that.bleManager.discoverServices(device.deviceId);
     if (serviceId == "") {
       console.log("连接失败")
-      that.disconnect(data)
+      that.disconnect(device)
       if (onChanged) {
         onChanged(false)
       }
@@ -392,10 +392,10 @@ class BtHelper {
     }
     // that.setData({ services });
     console.log("服务ID:" + serviceId)
-    var characteristics = await that.bleManager.discoverCharacteristics(data.deviceId, serviceId)
+    var characteristics = await that.bleManager.discoverCharacteristics(device.deviceId, serviceId)
     if (characteristics == "") {
       console.log("连接失败")
-      that.disconnect(data)
+      that.disconnect(device)
       if (onChanged) {
         onChanged(false)
       }

+ 4 - 3
pages/index/index.js

@@ -161,8 +161,9 @@ Page({
             if (tempItem.connectType != 3) {
               var has = false;
               for (var j = 0; j < compareList.length; j++) {
-                // "state":"online" "offline"
+                // "state":"online" "offline" MW-SR1(4G_WIFI)
                 if (tempItem.deviceId === compareList[j].deviceId) {
+                  console.log("gasdfqwerqwerqdfasdfr==00==" + JSON.stringify(compareList[j]));
                   has = true;
                   break;
                 }
@@ -191,7 +192,7 @@ Page({
             if (dList.length > 0) {
               for (var i = 0; i < dList.length; i++) {
                 var item = dList[i];
-                if (item.connectType == 1 && tempItem.state == "online") {
+                if (item.connectType == 1 && item.state == "online") {
                   item.name = item.devName;
                   if (that.data.deviceListIndex == null) {
                     BtHelper.getInstance().connect(item, function (data) {
@@ -807,7 +808,7 @@ Page({
   /// 断开蓝牙连接
   async cancelBlue(index, onlyCancel) {
     var that = this;
-    await BtHelper.getInstance().disconnect();
+    // await BtHelper.getInstance().disconnect();
     if (onlyCancel) {
       return;
     }