浏览代码

重新开启教具重连

叶振荣 3 年之前
父节点
当前提交
8bff5ce51d
共有 2 个文件被更改,包括 31 次插入18 次删除
  1. 1 1
      src/components/device/toy/connecting.vue
  2. 30 17
      src/utils/bluetooth.js

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

@@ -94,7 +94,7 @@
               {{ toy['name'] }}
               {{ toy['name'] }}
             </template>
             </template>
             <template v-if="connect_toy == 2 || connect_toy == 4">
             <template v-if="connect_toy == 2 || connect_toy == 4">
-              {{ toy['name'] }}({{toy_power}}%)
+              {{ toy['name'] }}
             </template>
             </template>
           </text>
           </text>
         </div>
         </div>

+ 30 - 17
src/utils/bluetooth.js

@@ -104,21 +104,20 @@ export default {
   //获取教具电量
   //获取教具电量
   sendToyPower_CheckReconected() {
   sendToyPower_CheckReconected() {
     let that = this;
     let that = this;
-
     let toy_intv = setInterval(() => {
     let toy_intv = setInterval(() => {
       let $game_status = game_store.getters.getGameStatus();
       let $game_status = game_store.getters.getGameStatus();
       if ($game_status == 1) {
       if ($game_status == 1) {
-        // if (!connect_toy) {
-        //   wx.showToast({
-        //     title: `教具已断开`
-        //   })
-        //   //调用重连教具
-        //   that.SendOrder("09")
-        //   that.ReconnectToy()
-        //   clearInterval(toy_intv)
-        // } else {
+        if (!connect_toy) {
+          wx.showToast({
+            title: `教具已断开`
+          })
+          //调用重连教具
+          that.SendOrder("09")
+          that.ReconnectToy()
+          clearInterval(toy_intv)
+        } else {
         that.SendOrder('8a')
         that.SendOrder('8a')
-        // }
+        }
       }
       }
     }, 3000)
     }, 3000)
   },
   },
@@ -275,8 +274,6 @@ export default {
       } else {
       } else {
         clearInterval($rec)
         clearInterval($rec)
       }
       }
-
-
     }, 5000)
     }, 5000)
 
 
   },
   },
@@ -427,23 +424,25 @@ export default {
         }
         }
       }
       }
 
 
-
       //2021年10月21日16:30:07
       //2021年10月21日16:30:07
       // 获取教具电量
       // 获取教具电量
       if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
       if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
         let $_hexStr = hexStr.substr(6);
         let $_hexStr = hexStr.substr(6);
         let $power = parseInt($_hexStr.substr(0, 2), 16)
         let $power = parseInt($_hexStr.substr(0, 2), 16)
         let $voltage = parseInt($_hexStr.substr(2, 2), 16)
         let $voltage = parseInt($_hexStr.substr(2, 2), 16)
-        console.log("电量:" + $power)
-        console.log("电压:" + $voltage / 10)
-        $this.toy_power = $power;
+
+        $this.toy_power = Math.round(that.CalBLEPower($voltage));
+
         //连接上教具的标识
         //连接上教具的标识
+        //FF为无效
         if ($power != 0) {
         if ($power != 0) {
           connect_toy = true;
           connect_toy = true;
         }
         }
         if (hexStr.toUpperCase().indexOf("FF") != -1) {
         if (hexStr.toUpperCase().indexOf("FF") != -1) {
           connect_toy = false;
           connect_toy = false;
         }
         }
+
+
         if (current_toy_UUID == "") {
         if (current_toy_UUID == "") {
           //获取教具UUID
           //获取教具UUID
           that.SendOrder('84')
           that.SendOrder('84')
@@ -769,5 +768,19 @@ export default {
     connect_toy = false;
     connect_toy = false;
     current_toy_id = "00";
     current_toy_id = "00";
     current_toy_UUID = "";
     current_toy_UUID = "";
+  },
+  //计算电量
+  CalBLEPower($voltage) {
+    let $max = 4100;
+    let $min = 3500;
+
+    //(当前电压值(mV)-最低电压值(mV)) / (最大电压值(mV)-最低电压值(mV)) *100%
+    //当前电压
+    let $_voltage = $voltage * 100;
+    // console.log(`计算的当前电压:${$_voltage}`)
+
+    let $percent = (($_voltage - $min) / ($max - $min)) * 100;
+    // console.log(`计算的电量:${$percent}`);
+    return $percent;
   }
   }
 };
 };