Преглед на файлове

630/691蓝牙模组兼容

叶振荣 преди 3 години
родител
ревизия
998913110c
променени са 5 файла, в които са добавени 122 реда и са изтрити 63 реда
  1. 1 0
      src/components/device/device.vue
  2. 5 1
      src/components/device/toy/connecting.vue
  3. 1 0
      src/pages/index/index.vue
  4. 58 25
      src/pages/start/index.vue
  5. 57 37
      src/utils/bluetooth.js

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

@@ -44,6 +44,7 @@
             :connect_toy="connect_toy"
             :toy_id="toy_action"
             :toy="toy_item"
+            :toy_sn="toy_sn"
             :device_bg="device_bg"
             :device_power="device_power"
             :toy_power="toy_power"

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

@@ -94,7 +94,7 @@
               {{ toy['name'] }}
             </template>
             <template v-if="connect_toy == 2 || connect_toy == 4">
-              {{ toy['name'] }}
+              {{ toy_sn }}
             </template>
           </text>
         </div>
@@ -296,6 +296,7 @@ export default {
     "device_bg",
     "device_power",
     "toy_power",
+    "toy_sn",
   ],
   data() {
     return {
@@ -345,6 +346,9 @@ export default {
             // 设置游戏记录id
             game_store.setters.setGameRecordId($res["game_record_id"]);
 
+            //打开脑控
+            $this.$bluetooth.sendControl();
+
             Toast.success($data.errmsg);
             setTimeout(() => {
               mpvue.navigateTo({

+ 1 - 0
src/pages/index/index.vue

@@ -173,6 +173,7 @@ export default {
     });
   },
   onShow() {
+
     if (wx.getStorageSync("is_other_first") == false) {
       wx.setStorageSync("is_other", false);
     } else {

+ 58 - 25
src/pages/start/index.vue

@@ -46,7 +46,7 @@
     <div class="game_status padding-tb">
       <van-row>
         <van-col
-          span="8"
+          span="5"
         >
           <div
             class="flex flex-direction align-center justify-center"
@@ -71,35 +71,63 @@
             <text class="text-default">未正确佩戴</text>
           </div>
         </van-col>
-        <van-col span="8">
+        <van-col span="4">
           <div class="flex flex-direction align-center justify-center">
-            <view class="elc_power_container">
-              <img
-                src="https://img.shuimuai.com/web/dianchi_3.png"
-                alt=""
-                :style="{ width: device_power + '%' }"
-                class="device_elc"
-              />
-            </view>
+<!--            <view class="elc_power_container">-->
+<!--              <img-->
+<!--                src="https://img.shuimuai.com/web/dianchi_3.png"-->
+<!--                alt=""-->
+<!--                :style="{ width: device_power + '%' }"-->
+<!--                class="device_elc"-->
+<!--              />-->
+<!--            </view>-->
             <text class="text-default">{{ device_power }}%</text>
-            <text class="text-default">设备电量</text>
+            <text class="text-default">脑环电量</text>
+          </div>
+        </van-col>
+         <van-col span="4">
+          <div class="flex flex-direction align-center justify-center">
+<!--            <view class="elc_power_container">-->
+<!--              <img-->
+<!--                src="https://img.shuimuai.com/web/dianchi_3.png"-->
+<!--                alt=""-->
+<!--                :style="{ width: device_power + '%' }"-->
+<!--                class="device_elc"-->
+<!--              />-->
+<!--            </view>-->
+            <text class="text-default">{{ device_voltage }}</text>
+            <text class="text-default">脑环电压</text>
           </div>
         </van-col>
 
-        <van-col span="8">
+        <van-col span="4">
           <div class="flex flex-direction align-center justify-center">
-            <view class="elc_power_container">
-              <img
-                src="https://img.shuimuai.com/web/dianchi_3.png"
-                alt=""
-                :style="{ width: toy_power + '%' }"
-                class="device_elc"
-              />
-            </view>
-            <text class="text-default">{{ toy_power }}%</text>
+<!--            <view class="elc_power_container">-->
+<!--              <img-->
+<!--                src="https://img.shuimuai.com/web/dianchi_3.png"-->
+<!--                alt=""-->
+<!--                :style="{ width: toy_power + '%' }"-->
+<!--                class="device_elc"-->
+<!--              />-->
+<!--            </view>-->
+            <text class="text-default">{{ toy_power }}</text>
             <text class="text-default">教具电量</text>
           </div>
         </van-col>
+        <van-col span="4">
+          <div class="flex flex-direction align-center justify-center">
+<!--            <view class="elc_power_container">-->
+<!--              <img-->
+<!--                src="https://img.shuimuai.com/web/dianchi_3.png"-->
+<!--                alt=""-->
+<!--                :style="{ width: toy_power + '%' }"-->
+<!--                class="device_elc"-->
+<!--              />-->
+<!--            </view>-->
+            <text class="text-default">{{ toy_voltage }}</text>
+            <text class="text-default">教具电压</text>
+          </div>
+        </van-col>
       </van-row>
     </div>
 
@@ -288,7 +316,6 @@ export default {
 
       timeData: {},
       mode_item: {},
-      device_power: 100,
 
       user_info: {},
       //设备值
@@ -351,7 +378,13 @@ export default {
       //  蓝牙信号强度
       rssi: 0,
       game_status: 0,
+
       toy_power: 0,
+      toy_voltage:0,
+      device_power: 0,
+      device_voltage:0,
+
+
       toy_UUID: "",
     };
   },
@@ -414,10 +447,10 @@ export default {
       });
 
       $this.$bluetooth.watchingDevice($this);
-      $this.$bluetooth.sendControl();
-      setTimeout(() => {
+      // $this.$bluetooth.sendControl();
+      // setTimeout(() => {
         $this.$bluetooth.sendToyPower_CheckReconected()
-      },2000)
+      // }, 2000)
     },
 
     // ArrayBuffer转16进度字符串示例

+ 57 - 37
src/utils/bluetooth.js

@@ -20,6 +20,11 @@ var FlagOpenControl = false;
 
 var ToyPower = 0;
 
+//Math.round(new Date() / 1000)
+var logTime = "";
+
+var current_hex = "";
+
 
 //2021年11月10日14:59:38
 // 0为旧 1为新
@@ -54,7 +59,7 @@ function getServicesAndCharateristc() {
 
 export default {
   //变量
-  connect_toy, control_close, current_toy_id, current_toy_UUID,ToyPower,
+  connect_toy, control_close, current_toy_id, current_toy_UUID, ToyPower,
 
   // 打开大包数据
   sendOpenBigData() {
@@ -118,9 +123,9 @@ export default {
         //   // $connect_false_count += 1;
         //   clearInterval(toy_intv)
         // } else {
-          // $connect_false_count = 0;
-          that.SendOrder('8a')
-          // connect_toy = false
+        // $connect_false_count = 0;
+        that.SendOrder('8a')
+        // connect_toy = false
         // }
 
         //3次获取不到电量则重新连接
@@ -315,7 +320,7 @@ export default {
         }
       },
       fail(res) {
-        console.log("连接蓝牙成功,获取服务失败",res);
+        console.log("连接蓝牙成功,获取服务失败", res);
       },
     });
   },
@@ -390,17 +395,11 @@ export default {
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
 
-      wx.getSystemInfo({
-        success(res) {
-          if (res.platform == "ios") {
-            console.log(hexStr)
-          } else {
-            if (hexStr.toUpperCase().indexOf("AADD") != -1) {
-              console.log(hexStr);
-            }
-          }
-        }
-      });
+      if (hexStr.toUpperCase().indexOf("AADD") != -1) {
+        let nowTime = Math.round(new Date() / 1000);
+        console.log(`${(nowTime - logTime)}s`)
+        console.log(hexStr);
+      }
       let $data = that.get_big_data(hexStr);
       let $game_status = game_store.getters.getGameStatus();
 
@@ -429,8 +428,8 @@ export default {
           });
           $this.toy_connected = true;
           $this.change_toy_connect_status(2);
-          //连接成功后 获取一次教具电量信息
-          that.SendOrder('8a')
+          //连接成功后 获取一次教具名称
+          that.SendOrder('87')
         }
 
         if (new RegExp("02").test($hex_index) == true) {
@@ -445,16 +444,30 @@ export default {
         }
       }
 
+      //2021年11月23日10:39:49
+      // 获取教具名称
+      if (hexStr.toUpperCase().indexOf("AADD87") != -1) {
+          let $datas = that.DoAnalysis(hexStr, 10)
+          console.log($datas)
+          $this.toy_sn = $datas;
+          //获取教具电量
+          that.SendOrder('8a')
+      }
+
       //2021年10月21日16:30:07
       // 获取教具电量
       if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
-        let $_hexStr = hexStr.substr(hexStr.toUpperCase().indexOf("AADD8A")+6);
+        let $_hexStr = hexStr.substr(hexStr.toUpperCase().indexOf("AADD8A") + 6);
         let $power = parseInt($_hexStr.substr(0, 2), 16)
         let $voltage = parseInt($_hexStr.substr(2, 2), 16)
 
         // $this.toy_power = Math.round(that.CalBLEPower($voltage));
-        $this.toy_power = $power
-        ToyPower = $power;
+        if ($power > 0) {
+          $this.toy_power = $power
+          $this.toy_voltage = $voltage
+          ToyPower = $power;
+        }
+
         //连接上教具的标识
         //FF为无效
         if ($power != 0) {
@@ -535,7 +548,7 @@ export default {
         let $version_data = $_data.substr(0, 2).split('')
         $this.device_data.hardware_version = `${$version_data[0]}.${$version_data[1]}`
 
-        setDeviceInDb($this, DeviceId)
+        // setDeviceInDb($this, DeviceId)
       }
 
       if (hexStr.toUpperCase().indexOf("AADD09") != -1) {
@@ -556,23 +569,28 @@ export default {
       }
 
 
-      //  监听脑环电量
-      let $power = that.get_device_elc(hexStr);
-      if ($power) {
-        // console.log("当前脑环电量:", $power)
-        $this.device_power = $power;
-      }
-      if ($power < 10 && $power > 0) {
-        wx.showToast({
-          title: "脑环电量不足",
-          icon: "none",
-          duration: 2000,
-          success() {
-            // $this.change_device_status(0);
-          },
-        });
+      if (hexStr.substr(0, 8) == "55550203") {
+        let $power = parseInt(hexStr.substr(8, 2), 16);
+        let $voltage = parseInt(hexStr.substr(10, 2), 16);
+        //  监听脑环电量
+        if ($power) {
+          // console.log("当前脑环电量:", $power)
+          $this.device_power = $power;
+          $this.device_voltage = $voltage;
+        }
+        if ($power < 10 && $power > 0) {
+          wx.showToast({
+            title: "脑环电量不足",
+            icon: "none",
+            duration: 2000,
+            success() {
+              // $this.change_device_status(0);
+            },
+          });
+        }
       }
 
+
       //游戏中模块
       if ($game_status == 1 && $data) {
         try {
@@ -660,6 +678,7 @@ export default {
   //写入buffer
 
   WriteBufferInBle($hex, $buffer_len = 8) {
+    let that = this;
     let $deviceInfo = getServicesAndCharateristc();
     let $hex_header = "aa cc ";
     // let $hex = "03 00 01 00 0a";
@@ -687,6 +706,7 @@ export default {
       value: buffer,
       success: function (res) {
         console.log($hex + ',写入成功')
+        logTime = Math.round(new Date() / 1000);
       },
       fail: function (err) {
         console.log($hex + "写入失败");