Sfoglia il codice sorgente

点击断开连接添加发送09关闭脑控指令

智方网络_吖荣 3 anni fa
parent
commit
e6e65e8acc
2 ha cambiato i file con 39 aggiunte e 14 eliminazioni
  1. 7 3
      src/components/device/device.vue
  2. 32 11
      src/utils/bluetooth.js

+ 7 - 3
src/components/device/device.vue

@@ -401,21 +401,26 @@ export default {
       //当脑环断开
       if ($status == 0) {
         game_store.setters.setGameStatus(0);
-        bluetooth.sendControlClose();
         // 清空链接得设备 三值
         game_store.setters.clearDeviceToy();
         $this.connect_toy = $status;
         $this.connect_show = false;
         $this.device_bg = false;
+
+        bluetooth.SendOrder("09")
+
+        $this.change_toy_connect_status(0);
+        //断开蓝牙连接
         wx.closeBLEConnection({
           deviceId: $this.device.deviceId,
           success(res) {
             Toast.success({
               message: "已成功断开",
             });
+            wx.closeBluetoothAdapter();
           },
           fail(res) {
-            console.log("断开连接error:" + res)
+            console.log("断开连接error:", res)
           },
           complete(res) {
             $this.device = {}
@@ -423,7 +428,6 @@ export default {
             $this.$forceUpdate();
           }
         });
-        wx.closeBluetoothAdapter();
       } else if ($status == 2) {
         $this.connect_show = true;
       }

+ 32 - 11
src/utils/bluetooth.js

@@ -5,6 +5,9 @@ import {setDeviceInDb} from "../requests/game";
 
 var $ff = "ff";
 var $8f = "ffffffff"
+var control_close = false
+var connect_toy = true;
+
 export default {
 
   // 打开大包数据
@@ -30,7 +33,15 @@ export default {
   },
   // 关闭脑控
   sendControlClose() {
-    this.SendOrder('09')
+    let that = this
+    let $intv = setInterval(() => {
+      if (!control_close) {
+        that.SendOrder('09')
+      } else {
+        clearInterval($intv)
+        control_close = false
+      }
+    }, 1000)
   },
   //发送一对多连接
   sendConnectOneToMore(id) {
@@ -41,6 +52,17 @@ export default {
     this.WriteBufferInBle(`03 00 ${id} 01 0A`)
   },
 
+  //获取教具电量
+  sendToyPower() {
+    let that = this;
+    let toy_intv = setInterval(() => {
+      if (connect_toy) {
+        that.SendOrder('8a')
+        connect_toy = false
+      }
+    }, 3000)
+  },
+
   //  获取大包数据 进行绘制图表
   get_big_data(hex) {
     if (hex.substr(0, 6) != "555520") {
@@ -138,15 +160,6 @@ export default {
           // game_store.setters.setGameStatus(0);
           // 结束状态更改为1
 
-        } else {
-          //断开玩具连接
-          try {
-            $this.change_toy_connect_status(0);
-            //断开蓝牙连接
-            $this.change_device_status(0);
-          } catch (e) {
-            console.log("方法不存在");
-          }
         }
       }
     });
@@ -271,7 +284,9 @@ export default {
     );
     return hexArr.join("");
   },
-  // 监听脑环是否带正
+
+
+  // 监听脑环数据
   watchingDevice($this) {
     const that = this;
     wx.onBLECharacteristicValueChange((characteristic) => {
@@ -322,6 +337,8 @@ export default {
         console.log("电量:" + $power)
         console.log("电压:" + $voltage / 10)
         $this.toy_power = $power;
+        //连接上教具的标识
+        connect_toy = true;
 
         //2021年10月25日09:16:06
         //获取教具UUID
@@ -393,6 +410,10 @@ export default {
         setDeviceInDb($this, $this.device.deviceId)
       }
 
+      if (hexStr.toUpperCase().indexOf("AADD09") != -1) {
+        control_close = true;
+      }
+
 
       //  监听脑环电量
       let $power = that.get_device_elc(hexStr);