Kaynağa Gözat

优化蓝牙连接流程2

chaooo 1 yıl önce
ebeveyn
işleme
675c92db17

+ 2 - 2
src/components/connection/brains/connected.vue

@@ -5,7 +5,7 @@ export default {
   methods: {
     /**
      * 修改设备连接状态
-     * @param $status 设备状态 0为未连接,1:连接中,2:已连接 3:连接失败
+     * @param $status 设备状态 0为未连接,1:连接中,2:已连接 3:连接失败/断开连接
      */
     setDeviceStatus($status = 0) {
       this.$emit("deviceStatus", $status);
@@ -52,7 +52,7 @@ export default {
     <!--        按钮组合-->
     <van-row>
       <van-col span="9" offset="1">
-        <button class="cu-btn bg-red lg text-white" @click="setDeviceStatus(0)">
+        <button class="cu-btn bg-red lg text-white" @click="setDeviceStatus(3)">
           <img src="https://img.shuimuai.com/m_duankainaohuan.png" class="cut_brain_icon" alt="">
           <text class="padding-lr cut_text">断开脑机</text>
         </button>

+ 39 - 2
src/components/connection/index.vue

@@ -93,7 +93,8 @@ export default {
         $this.device_bg = false;
         $this.connect_status = false;
         if ($status * 1 > 0) {
-          // 断开脑机
+          // 断开教具及蓝牙连接
+          $this.$bluetooth.SendOrder("31");
           // 移除搜索到新设备的事件的全部监听函数
           wx.offBluetoothDeviceFound();
           // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
@@ -172,7 +173,43 @@ export default {
       $this.game_status = 1;
       $this.connect_toy = 4;
     },
-  }
+  },
+  onShow() {
+    //判断是否游戏中
+    let $game_status = game_store.getters.getGameStatus();
+    // 游戏过程中关闭脑机状态
+    let $game_close_status = game_store.getters.getGameCloseStatus();
+
+    if ($game_status === 3) {
+      let $ble_status = wx.getStorageSync("ble_link_status");
+      if ($ble_status === false) {
+        //断开蓝牙连接
+        $this.setDeviceStatus(0);
+      }
+
+      //不在游戏状态
+      $this.connect_toy = 0;
+      $this.connect_show = true;
+      // 监听数据
+      $this.$bluetooth.notifyDatas($this);
+      $this.$bluetooth.watchBLEstatus($this);
+
+      $this.toy_UUID = "";
+      $this.$forceUpdate();
+      // 状态为1的时候重置为1 小乌龟
+      if ($game_close_status === 1) {
+        // 重置默认条件
+        $this.connect_toy = 0;
+        $this.connect_show = false;
+        $this.device_status = 0;
+        $this.$bluetooth.watchBLEstatus($this);
+        $this.toy_UUID = "";
+        $this.$forceUpdate();
+        // 清空链接的设备
+        game_store.setters.clearDeviceToy();
+      }
+    }
+  },
 };
 </script>
 

+ 2 - 0
src/components/connection/toys/selected.vue

@@ -71,6 +71,8 @@ export default {
       };
       gameStart($params).then(
         (res) => {
+          // 重置断开脑机的状态
+          game_store.setters.setGameCloseStatus(0);
           let $data = res.data;
           let $res = $data.data;
           if ($data.code === 0) {

+ 3 - 8
src/pages/start/index.vue

@@ -207,7 +207,7 @@ function initMedChart(canvas, width, height) {
 
 var $timeout;
 export default {
-  name: "index_container",
+  name: "start_container",
   components: {
     mpvueEcharts,
   },
@@ -903,25 +903,20 @@ export default {
 
 .circle_text {
   width: 100%;
-  padding-bottom: 50px;
-  padding-bottom: 50px;
-
 }
 
 .online_att_txt {
   font-size: 40px;
-  font-family: Arial;
+  font-family: Arial,serif;
   font-weight: bold;
   color: #FAB615;
-  /*height: 186px;*/
 }
 
 .online_med_txt {
   font-size: 40px;
-  font-family: Arial;
+  font-family: Arial,serif;
   font-weight: bold;
   color: #40FF31;
-  /*height: 186px;*/
 }
 
 .online_desc {

+ 6 - 0
src/store/game.js

@@ -65,6 +65,9 @@ const setters = {
   setLED(bool) {
     wx.setStorageSync('led_status', bool)
   },
+  setGameCloseStatus(status) {
+    wx.setStorageSync('game_close_status', status)
+  },
 }
 
 const getters = {
@@ -124,6 +127,9 @@ const getters = {
   getLED() {
     return wx.getStorageSync('led_status')
   },
+  getGameCloseStatus() {
+    return wx.getStorageSync('game_close_status')
+  },
 }
 
 const remover = {

+ 24 - 19
src/utils/bluetooth.js

@@ -132,27 +132,31 @@ export default {
       that.SendLedOrder("01");
     },500);
     setTimeout(()=>{
-      // 断开教具及蓝牙连接
-      that.SendOrder("31");
+      // 关闭脑控
+      that.SendOrder('09');
+      //清空当前数据
+      that.clearLocalDatas();
     },1000);
+    // setTimeout(()=>{
+    //   // 断开教具及蓝牙连接
+    //   that.SendOrder("31");
+    // },1500);
     // 兼容二代脑机,不能退出循环
-    control_close = false;
-    control_close_intv = setInterval(() => {
-      if (!control_close) {
-        // 关闭脑控
-        that.SendOrder('09')
-        //如果是jellyfish则发送00教具
-        if (!is_new) {
-          that.sendConnectOneToMore('00')
+    if (!is_new) {
+      control_close = false;
+      control_close_intv = setInterval(() => {
+        if (!control_close) {
+          //如果是jellyfish则发送00教具
+            that.sendConnectOneToMore('00')
+        } else {
+          //设置打开脑控为false
+          FlagOpenControl = false;
+          clearInterval(control_close_intv)
+          //清空当前数据
+          that.clearLocalDatas()
         }
-      } else {
-        //设置打开脑控为false
-        FlagOpenControl = false;
-        clearInterval(control_close_intv)
-        //清空当前数据
-        that.clearLocalDatas()
-      }
-    }, 2000)
+      }, 2000)
+    }
   },
   /**
    *  todo:发送一对多连接
@@ -319,6 +323,7 @@ export default {
           },
           fail(res) {
             Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
+            game_store.setters.setGameCloseStatus(1);
           }
         })
         if ($connect_count >= 3) {
@@ -463,7 +468,7 @@ export default {
       })
 
       let hexStr = that.ab2hex(characteristic.value);
-      // console.log("数据", hexStr, "长度", hexStr.length / 2);
+      console.log("plant监听脑机数据:", hexStr, "当前页面名称:", $this.$options.name);
 
       if (hexStr.toUpperCase().indexOf("AADD") != -1 || hexStr.toUpperCase().indexOf("AAEE") != -1 && update_state == false) {
         let nowTime = Math.round(new Date() / 1000);