Selaa lähdekoodia

添加了mac UUID 的显示

智方网络_吖荣 3 vuotta sitten
vanhempi
commit
89a6b80915
3 muutettua tiedostoa jossa 91 lisäystä ja 86 poistoa
  1. 24 20
      src/components/device/device.vue
  2. 51 40
      src/pages/start/index.vue
  3. 16 26
      src/utils/bluetooth.js

+ 24 - 20
src/components/device/device.vue

@@ -42,7 +42,7 @@
         <div class="connecting_toy">
           <toy_connecting
             :connect_toy="connect_toy"
-            :deviceId="_deviceId"
+            :deviceId="device.deviceId"
             :toy_id="toy_action"
             :toy="toy_item"
             :device_bg="device_bg"
@@ -117,6 +117,10 @@
     <van-toast id="van-toast"/>
     <van-dialog id="van-dialog"/>
 
+    <view style="width: 100%;height: 300px;">
+      <view>脑环Mac:{{ device.deviceId }}</view>
+      <view>教具UUID:{{ toy_UUID }}</view>
+    </view>
     <!--    <van-popup-->
     <!--      :show="start_show"-->
     <!--      :closeable="false"-->
@@ -181,7 +185,7 @@ export default {
       connect_toy: 0,
       code: "jellyfish1234",
       device: {},
-      _deviceId: "",
+      deviceId: "",
       _device_index: false,
       toy_id: 0,
       toy_hex: "",
@@ -202,7 +206,7 @@ export default {
       //当前发送的hex码
       current_hex: "",
       //判断是否已经连接教具
-      toy_connected:false
+      toy_connected: false
     };
   },
   methods: {
@@ -215,7 +219,6 @@ export default {
     },
     //扫描连接蓝牙
     scan_to_bluetooth() {
-      // console.log("在扫码时是否存在deviceId?:", this._deviceId);
       wx.scanCode({
         onlyFromCamera: true,
         success: (res) => {
@@ -356,14 +359,21 @@ export default {
         game_store.setters.clearDeviceToy();
         $this.connect_toy = $status;
         $this.connect_show = false;
-        $this._deviceId = "";
         wx.closeBLEConnection({
-          deviceId: $this._deviceId,
+          deviceId: $this.device.deviceId,
           success(res) {
             Toast.success({
               message: "已成功断开",
             });
           },
+          fail(res) {
+            console.log("断开连接error:" + res)
+          },
+          complete(res) {
+            $this.device = {}
+            $this.toy_UUID = "";
+            $this.$forceUpdate();
+          }
         });
         wx.closeBluetoothAdapter();
       } else if ($status == 2) {
@@ -388,12 +398,10 @@ export default {
                   // 判断ios 和 安卓
                   if (res.platform == "ios") {
                     $this.device.deviceId = $data.ios_device_id;
-                    $this._deviceId = $data.ios_device_id;
                   } else {
                     $this.device.deviceId = $data.device_id;
-                    $this._deviceId = $data.device_id;
                   }
-                  if ($this._deviceId) {
+                  if ($this.device.deviceId) {
                     console.log("finded")
                     $this.createBLEConnection()
                   } else {
@@ -415,16 +423,10 @@ export default {
     },
     //打开蓝牙搜索
     onBluetoothDeviceFound() {
-      // console.log(
-      //   "打开蓝牙搜索 device_status",
-      //   $this.device_status,
-      //   "是否有id _deviceId",
-      //   $this._deviceId
-      // );
       try {
         // 5秒后判断 这5秒在搜索设备,搜索到就赋值给_deviceId
         setTimeout(() => {
-          if (!$this._deviceId) {
+          if (!$this.device.deviceId) {
             wx.stopBluetoothDevicesDiscovery();
             $this.change_device_status(0);
             Toast.fail("未连接到设备");
@@ -451,10 +453,9 @@ export default {
               $this.stopBluetoothDevicesDiscovery();
               $this.device = device;
               game_store.setters.setDeviceId(device.deviceId);
-              $this._deviceId = device.deviceId;
               $this.createBLEConnection();
               //录入库
-              setDeviceInDb($this.code,device.deviceId)
+              setDeviceInDb($this.code, device.deviceId)
 
             }
           });
@@ -536,6 +537,8 @@ export default {
       //   //断开蓝牙连接
       //   $this.change_device_status(0);
       // }
+      $this.toy_UUID = "";
+      $this.$forceUpdate();
       // 状态为1的时候重置为1 小乌龟
       if ($game_close_status == 1) {
         // 重置默认条件
@@ -543,8 +546,9 @@ export default {
         $this.connect_show = false;
         $this.device_status = 0;
         bluetooth.watch_bluetooth_status($this);
-        $this._deviceId = "";
-
+        $this.device.deviceId = "";
+        $this.toy_UUID = "";
+        $this.$forceUpdate();
         // 清空链接的设备
         game_store.setters.clearDeviceToy();
 

+ 51 - 40
src/pages/start/index.vue

@@ -1,5 +1,9 @@
 <template>
   <div id="playing_container">
+    <view style="width: 100%;height: 100px;color: white">
+      <view>脑环Mac:{{ deviceId }}</view>
+      <view>教具UUID:{{ toy_UUID }}</view>
+    </view>
     <!--    计费面板模块-->
     <div class="game_panel padding-top-sm">
       <van-row gutter="16">
@@ -289,7 +293,7 @@ export default {
 
       user_info: {},
       //设备值
-      _deviceId: "",
+      deviceId: "",
       _serviceId: "",
       _characteristicId: "",
 
@@ -348,49 +352,50 @@ export default {
       //  蓝牙信号强度
       rssi: 0,
       game_status: 0,
-      toy_power:100
+      toy_power: 0,
+      toy_UUID: "",
     };
   },
   methods: {
     //游戏结束方法
     game_finished() {
-        if (!$this.executed){
-          bluetooth.sendControlClose();
-
-          $this.$emit("closePop", true);
-
-          //设置游戏状态为 停止游戏
-          game_store.getters.getGameStatus() == 0
-            ? game_store.setters.setGameStatus(0)
-            : game_store.setters.setGameStatus(3);
-          //游戏结束重置游戏时间
-          wx.removeStorageSync("play_time");
-          //删除游戏专注数据
-          game_store.remover.rmGameAttMedDatas();
-          //删除隐藏时间
-          game_store.remover.rmHideTime();
-          //游戏结束重置游戏模式
-          // game_store.setters.setMode(0);
-          //删除游戏得id
-          game_store.setters.removeToyHex();
-
-          // 结束游戏在提交报告函数里
-          $this.post_data();
-
-          $this.game_over();
-
-
-          Toast.loading({
-              forbidClick: true,
-              mask: true,
-              message: "正在生成报告...",
-              duration: 2000,
-              onClose() {
-                $this.to_report();
-              }
-          });
-        }
-        $this.executed = true;
+      if (!$this.executed) {
+        bluetooth.sendControlClose();
+
+        $this.$emit("closePop", true);
+
+        //设置游戏状态为 停止游戏
+        game_store.getters.getGameStatus() == 0
+          ? game_store.setters.setGameStatus(0)
+          : game_store.setters.setGameStatus(3);
+        //游戏结束重置游戏时间
+        wx.removeStorageSync("play_time");
+        //删除游戏专注数据
+        game_store.remover.rmGameAttMedDatas();
+        //删除隐藏时间
+        game_store.remover.rmHideTime();
+        //游戏结束重置游戏模式
+        // game_store.setters.setMode(0);
+        //删除游戏得id
+        game_store.setters.removeToyHex();
+
+        // 结束游戏在提交报告函数里
+        $this.post_data();
+
+        $this.game_over();
+
+
+        Toast.loading({
+          forbidClick: true,
+          mask: true,
+          message: "正在生成报告...",
+          duration: 2000,
+          onClose() {
+            $this.to_report();
+          }
+        });
+      }
+      $this.executed = true;
     },
     to_report() {
       mpvue.redirectTo({
@@ -411,6 +416,10 @@ export default {
 
       bluetooth.watchingDevice($this);
       bluetooth.sendControl();
+
+      setTimeout(() => {
+        bluetooth.SendOrder("8a")
+      }, 1000)
     },
 
     // ArrayBuffer转16进度字符串示例
@@ -613,7 +622,9 @@ export default {
     }
   },
   mounted() {
-    $this._deviceId = game_store.getters.getDeviceId();
+    $this.deviceId = "";
+    $this.toy_UUID = "";
+    $this.deviceId = game_store.getters.getDeviceId();
     $this._serviceId = game_store.getters.getServiceId();
     $this._characteristicId = game_store.getters.getCharacterId();
     // setTimeout(() => {

+ 16 - 26
src/utils/bluetooth.js

@@ -279,6 +279,10 @@ export default {
         console.log("电量:" + $power)
         console.log("电压:" + $voltage / 10)
         $this.toy_power = $power;
+
+        //2021年10月25日09:16:06
+        //获取教具UUID
+        that.SendOrder('84')
       }
 
       //监听佩戴正确
@@ -290,30 +294,16 @@ export default {
         $this.device_bg = $s1 == "00";
       }
 
-      //2021年10月21日17:22:37
-      //收到一次UUID就发送一次获取教具的电量
-      //读取教具UUID
-      // if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
-      //   let $_hexStr = hexStr.substr(6);
-      //   let $datas = $_hexStr.substr(0, 10);
-      //   if ($datas != "0000000000") {
-      //     $this.toy_UUID = $datas;
-      //     //读取教具电量
-      //     that.SendOrder('8a')
-      //   } else {
-      //     //如果游戏中则重新连接
-      //     if ($game_status == 1) {
-      //       //先发UUID 再发id 02
-      //       console.log($this.toy_UUID)
-      //       console.log(`aa cc 03 00 ${$this.toy_hex} 02 0a`)
-      //     } else {
-      //       $this.toy_UUID = ""
-      //       $this.toy_power = 0;
-      //
-      //     }
-      //   }
-      //
-      // }
+      // 2021年10月25日09:15:50
+      // 收到一次UUID就发送一次获取教具的电量
+      // 读取教具UUID
+      if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
+        let $_hexStr = hexStr.substr(6);
+        let $datas = $_hexStr.substr(0, 10);
+        if ($datas != "0000000000") {
+          $this.toy_UUID = $datas;
+        }
+      }
 
 
       //  监听脑环电量
@@ -343,8 +333,8 @@ export default {
             $this.played_time_text = that.formatPlaySeconds($this.played_time);
             $this.do_datas($data);
             if ($this.played_time % 10 == 0) {
-                  //读取教具电量
-                  that.SendOrder('8a')
+              //读取教具电量
+              that.SendOrder('8a')
             }
           }
           if ($this.played_time == 0) {