Przeglądaj źródła

Merge branch 'master' of gitee.com:sh_13808852820/program

Foystor 3 lat temu
rodzic
commit
2fa0a59dd9

+ 1 - 1
src/components/device/device.vue

@@ -357,7 +357,7 @@ export default {
       // $this.choose_toy_window.show = false;
       $this.choose_toy_window.show = true;
       $this.toy_connected = false
-      bluetooth.sendControlClose();
+      bluetooth.SendOrder("09");
     },
     // 选好玩具
     choose_ok() {

+ 2 - 5
src/pages/start/index.vue

@@ -416,10 +416,7 @@ export default {
 
       bluetooth.watchingDevice($this);
       bluetooth.sendControl();
-
-      setTimeout(() => {
-        bluetooth.SendOrder("8a")
-      }, 1000)
+      bluetooth.sendToyPower_CheckReconected()
     },
 
     // ArrayBuffer转16进度字符串示例
@@ -528,7 +525,7 @@ export default {
 
       $this.att_list.push($data["att"]);
       $this.med_list.push($data["med"]);
-      $this.amp_list.push(Math.abs($data["att"] - $data["med"]));
+      $this.amp_list.push(100 - Math.abs($data["att"] - $data["med"]));
       $this.delta_list.push($data['delta']);
       $this.theta_list.push($data['theta']);
       $this.low_alphas.push($data['low_alpha']);

+ 78 - 21
src/utils/bluetooth.js

@@ -6,9 +6,13 @@ import {setDeviceInDb} from "../requests/game";
 var $ff = "ff";
 var $8f = "ffffffff"
 var control_close = false
-var connect_toy = true;
+var connect_toy = false;
+var current_toy_id = "00";
+var current_toy_UUID = "";
 
 export default {
+  //变量
+  connect_toy, control_close, current_toy_id, current_toy_UUID,
 
   // 打开大包数据
   sendOpenBigData() {
@@ -39,7 +43,8 @@ export default {
         that.SendOrder('09')
       } else {
         clearInterval($intv)
-        control_close = false
+        //清空当前数据
+        that.clearLocalDatas()
       }
     }, 1000)
   },
@@ -49,18 +54,33 @@ export default {
   },
   //发送一对一连接
   sendConnectOneToOne(id) {
+    current_toy_id = id;
     this.WriteBufferInBle(`03 00 ${id} 01 0A`)
   },
 
   //获取教具电量
-  sendToyPower() {
+  sendToyPower_CheckReconected() {
     let that = this;
-    let toy_intv = setInterval(() => {
-      if (connect_toy) {
-        that.SendOrder('8a')
-        connect_toy = false
-      }
-    }, 3000)
+    let $connect_false_count = 0;
+    let $game_status = game_store.getters.getGameStatus();
+    if ($game_status == 1) {
+      let toy_intv = setInterval(() => {
+        if (connect_toy) {
+          that.SendOrder('8a')
+          connect_toy = false
+          $connect_false_count = 0
+        } else {
+          $connect_false_count += 1;
+          if ($connect_false_count == 3) {
+            //调用重连教具
+            that.ReconnectToy()
+            clearInterval(toy_intv)
+          }
+        }
+
+        console.log("循环数:" + $connect_false_count)
+      }, 3000)
+    }
   },
 
   //  获取大包数据 进行绘制图表
@@ -291,9 +311,9 @@ export default {
     const that = this;
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
-      // if (hexStr.toUpperCase().indexOf("AADD") != -1) {
-      console.log(hexStr);
-      // }
+      if (hexStr.toUpperCase().indexOf("AADD") != -1) {
+        console.log(hexStr);
+      }
       let $data = that.get_big_data(hexStr);
       let $game_status = game_store.getters.getGameStatus();
 
@@ -339,10 +359,10 @@ export default {
         $this.toy_power = $power;
         //连接上教具的标识
         connect_toy = true;
-
-        //2021年10月25日09:16:06
-        //获取教具UUID
-        that.SendOrder('84')
+        if (current_toy_UUID == "") {
+          //获取教具UUID
+          that.SendOrder('84')
+        }
       }
 
       //监听佩戴正确
@@ -361,6 +381,8 @@ export default {
         let $_hexStr = hexStr.substr(6);
         let $datas = $_hexStr.substr(0, 10);
         if ($datas != "0000000000") {
+          console.log("以获取UUID:" + $datas)
+          current_toy_UUID = $datas;
           $this.toy_UUID = $datas;
         }
       }
@@ -414,6 +436,18 @@ export default {
         control_close = true;
       }
 
+      //收到发送UUID的应答立马发送连接教具的指令
+      if (hexStr.toUpperCase().indexOf("AADD8E") != -1) {
+        //发送教具连接
+        that.WriteBufferInBle(`03 00 ${current_toy_id} 02 0A`)
+      }
+      if (hexStr.toUpperCase().indexOf("AADD0A02") != -1) {
+        //发送教具连接
+        wx.showToast({
+          title: "教具重连成功"
+        })
+      }
+
 
       //  监听脑环电量
       let $power = that.get_device_elc(hexStr);
@@ -441,10 +475,10 @@ export default {
             game_store.setters.setPlayedTime($this.played_time)
             $this.played_time_text = that.formatPlaySeconds($this.played_time);
             $this.do_datas($data);
-            if ($this.played_time % 10 == 0) {
-              //读取教具电量
-              that.SendOrder('8a')
-            }
+            // if ($this.played_time % 10 == 0) {
+            //   //读取教具电量
+            //   that.sendToyPower_CheckReconected()
+            // }
           }
           if ($this.played_time == 0) {
             $this.post_data();
@@ -554,11 +588,14 @@ export default {
       },
     });
   },
+
   //写入指令
   SendOrder(id) {
     let $hexStr = `03 00 00 00 ${id}`;
     this.WriteBufferInBle($hexStr)
   },
+
+  // 写入16个字节的指令
   Send16Order(val, id) {
     let $str = val;
     let $str_ary = $str.split('');
@@ -570,7 +607,7 @@ export default {
     let $hexStr = "03 ff " + $str + ` ${id}`;
     this.WriteBufferInBle($hexStr, 16)
   },
-//解析
+  //解析
   DoAnalysis(hexStr, byte_count, sublen = 6) {
     //byte_count
     let $_str5 = hexStr.substr(sublen);
@@ -582,4 +619,24 @@ export default {
     }
     return $_data;
   },
+
+  //重连教具
+  ReconnectToy() {
+    //发送UUID
+    let $uuid_str = "";
+    for (let $i = 0; $i < current_toy_UUID.length; $i + 2) {
+      $uuid_str += current_toy_UUID.substr($i, 2) + " ";
+    }
+    console.log("uuid_str" + $uuid_str)
+    $uuid_str = $uuid_str.substr(0, $uuid_str.length - 1)
+    this.Send16Order($uuid_str, "8e")
+  },
+
+  //清空当前脚本的变量值
+  clearLocalDatas() {
+    control_close = false
+    connect_toy = false;
+    current_toy_id = "00";
+    current_toy_UUID = "";
+  }
 };