Prechádzať zdrojové kódy

630/691蓝牙模组兼容

叶振荣 3 rokov pred
rodič
commit
40eff8c3bf

+ 6 - 15
src/components/device/device.vue

@@ -117,19 +117,6 @@
     <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"-->
-    <!--      position="bottom"-->
-    <!--      custom-style="height: 100%"-->
-    <!--      @close="onStartGameShowClose"-->
-    <!--    >-->
-    <!--      <gameIng v-if="game_status" @closePop="onStartGameShowClose"></gameIng>-->
-    <!--    </van-popup>-->
   </div>
 </template>
 
@@ -146,7 +133,7 @@ import toy_connecting from "@/components/device/toy/connecting";
 import gameIng from "@/pages/start/index";
 //获取个人信息
 import Toast from "../../../static/vant/toast/toast";
-import {game_devices, getDeviceBySn, setDeviceInDb} from "../../requests/game";
+import {game_devices, getDeviceBySn} from "../../requests/game";
 import game_store from "@/store/game";
 import {getNowDate} from "../../utils";
 
@@ -192,7 +179,7 @@ export default {
       //教具电量
       toy_power: 0,
       //教具名称
-      toy_sn: "",
+      toy_sn: "教具",
       //UUID
       toy_UUID: "",
 
@@ -269,6 +256,9 @@ export default {
 
             // 判断新的标识值
             $this.code = $code
+            //判断是新还是旧
+            game_store.setters.setIsNew($code.indexOf('AI') != -1)
+
             console.log("头环码", $code);
             game_store.setters.setDeviceSn($this.code)
             //设备信息
@@ -527,6 +517,7 @@ export default {
         deviceId: deviceId,
         success: (res) => {
           console.log("成功连接");
+          $this.$bluetooth.current_device_sn = $this.code
           game_store.setters.setDeviceId(deviceId);
 
           //成功连接脑环蓝牙

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

@@ -451,7 +451,7 @@ export default {
 
       // $this.$bluetooth.sendControl();
       // setTimeout(() => {
-        $this.$bluetooth.sendToyPower_CheckReconected()
+        $this.$bluetooth.sendToyPower()
       // }, 2000)
     },
 

+ 1 - 1
src/store/bluetooth.js

@@ -7,7 +7,7 @@ const getters = {
 const setters = {
   setBluetoothLinkStatus($bool) {
     wx.setStorageSync('ble_link_status', $bool)
-  }
+  },
 }
 
 export default {

+ 8 - 12
src/store/game.js

@@ -3,6 +3,7 @@ const setters = {
     wx.removeStorageSync('deviceId')
     wx.removeStorageSync('deviceSn')
     wx.removeStorageSync('serviceId')
+    wx.removeStorageSync('is_new')
     // wx.removeStorageSync('characteristicId')
   },
   //setter模块
@@ -15,12 +16,6 @@ const setters = {
   setServiceId(serviceId) {
     wx.setStorageSync('serviceId', serviceId)
   },
-  // setCid(characteristicId) {
-  //   wx.setStorageSync('characteristicId', characteristicId)
-  // },
-  // setReadCid(value) {
-  //   wx.setStorageSync('read_characteristicId', value)
-  // },
   setGameStatus(status) {
     wx.setStorageSync('game_status', status)
   },
@@ -63,6 +58,9 @@ const setters = {
   //记录游戏数据
   setGameAttMedDatas($param) {
     wx.setStorageSync('game_data', $param)
+  },
+  setIsNew($param) {
+    wx.setStorageSync('is_new', $param)
   }
 }
 
@@ -76,12 +74,6 @@ const getters = {
   getServiceId() {
     return wx.getStorageSync('serviceId')
   },
-  // getCharacterId() {
-  //   return wx.getStorageSync('characteristicId')
-  // },
-  // getReadCharacterId() {
-  //   return wx.getStorageSync('read_characteristicId')
-  // },
   getGameStatus() {
     return wx.getStorageSync('game_status')
   },
@@ -124,6 +116,10 @@ const getters = {
   //记录游戏数据
   getGameAttMedDatas() {
     return wx.getStorageSync('game_data')
+  },
+  //记录游戏数据
+  getIsNew() {
+    return wx.getStorageSync('is_new')
   }
 }
 

+ 162 - 119
src/utils/bluetooth.js

@@ -1,11 +1,11 @@
 import game_store from "../store/game";
 import ble_store from "../store/bluetooth";
 import Notify from "../../static/vant/notify/notify";
-import {setDeviceInDb} from "../requests/game";
 
 var $ff = "ff";
 var $8f = "ffffffff"
 var control_close = false
+var current_device_sn = ""
 //记录当前脑环的mac地址
 var current_device_mac = "";
 //记录教具连接状态
@@ -25,6 +25,7 @@ var logTime = "";
 
 var current_hex = "";
 
+var is_new = game_store.getters.getIsNew();
 
 //2021年11月10日14:59:38
 // 0为旧 1为新
@@ -59,32 +60,47 @@ 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, current_device_sn, is_new,
 
-  // 打开大包数据
+  /**
+   *  todo:打开大包数据
+   */
   sendOpenBigData() {
     this.SendOrder('ff')
   },
-  // 关闭大包数据
+  /**
+   *  todo:关闭大包数据
+   */
   sendCloseBigData() {
     this.SendOrder('00')
   },
-  //获取当前连接的教具类型
+  /**
+   *  todo:获取当前连接的教具类型
+   */
   sendGetToyType() {
     this.SendOrder('06')
   },
-  // 开启脑控
+  /**
+   *  todo:开启脑控
+   */
   sendControl() {
     let that = this;
     that.SendOrder('07')
   },
-  // 关闭脑控
+  /**
+   *  todo:关闭脑控
+   */
   sendControlClose() {
     let that = this
     control_close = false;
     let $intv = setInterval(() => {
       if (!control_close) {
         that.SendOrder('09')
+        //如果是jellyfish则发送00教具
+        if(!is_new){
+          that.sendConnectOneToMore('00')
+        }
+
       } else {
         //设置打开脑控为false
         FlagOpenControl = false;
@@ -94,49 +110,41 @@ export default {
       }
     }, 1000)
   },
-  //发送一对多连接
+  /**
+   *  todo:发送一对多连接
+   */
   sendConnectOneToMore(id) {
     this.WriteBufferInBle(`03 00 ${id} 00 0A`);
   },
-  //发送一对一连接
+  /**
+   *  todo:发送一对一连接
+   */
   sendConnectOneToOne(id) {
     current_toy_id = id;
     this.WriteBufferInBle(`03 00 ${id} 01 0A`)
   },
 
-  //获取教具电量
-  sendToyPower_CheckReconected() {
+  /**
+   *  todo:获取教具电量
+   */
+  sendToyPower() {
     let that = this;
-    let $connect_false_count = 0;
-    let toy_intv = setInterval(() => {
-      let $game_status = game_store.getters.getGameStatus();
-      if ($game_status == 1) {
-        // if (!connect_toy) {
-        //   // $connect_false_count += 1;
-        //   clearInterval(toy_intv)
-        // } else {
-        // $connect_false_count = 0;
-        that.SendOrder('8a')
-        // connect_toy = false
-        // }
-        //3次获取不到电量则重新连接
-        // if ($connect_false_count == 3) {
-        //   wx.showToast({
-        //     title: `教具已断开`
-        //   })
-        //   //调用重连教具
-        //   that.SendOrder("09")
-        //   that.ReconnectToy()
-        //   //教具断开 打开脑控也关闭
-        //   FlagOpenControl = false
-        //   $connect_false_count = 0;
-        //   clearInterval(toy_intv)
-        // }
-      }
-    }, 3000)
+    if (is_new) {
+      let $connect_false_count = 0;
+      let toy_intv = setInterval(() => {
+        let $game_status = game_store.getters.getGameStatus();
+        if ($game_status == 1) {
+          that.SendOrder('8a')
+        }
+      }, 3000)
+    }else{
+      console.log("旧脑环不支持获取教具电量")
+    }
   },
 
-  //  获取大包数据 进行绘制图表
+  /**
+   *  todo:获取大包数据进行绘制图表
+   */
   get_big_data(hex) {
     if (hex.substr(0, 6) != "555520") {
       return false;
@@ -200,7 +208,9 @@ export default {
       high_beta: $high_beta >= $max_num ? $max_num : $high_beta
     };
   },
-  //获取设备电量
+  /**
+   *  todo:获取设备电量
+   */
   get_device_elc(hex) {
     if (hex.substr(0, 8) != "55550203") {
       return false;
@@ -209,7 +219,9 @@ export default {
     return $power;
   },
 
-  //  监听蓝牙连接状态
+  /**
+   *  todo:监听蓝牙连接状态
+   */
   watch_bluetooth_status($this) {
     let that = this;
     // 微信自身监听低功耗蓝牙连接状态的改变事件
@@ -235,7 +247,7 @@ export default {
           // 结束状态更改为1
         } else {
           //关闭脑控
-          that.sendControlClose();
+          // that.sendControlClose();
           game_store.setters.setGameStatus(0);
           // 清空链接得设备 三值
           game_store.setters.clearDeviceToy();
@@ -253,7 +265,9 @@ export default {
     });
   },
 
-  //重新连接蓝牙
+  /**
+   * todo:重新连接蓝牙
+   */
   reconnect($deviceId, $this) {
     let that = this;
     let $deviceInfo = getServicesAndCharateristc();
@@ -294,7 +308,10 @@ export default {
 
   },
 
-  // //获取蓝牙设备服务
+  /**
+   * todo 获取蓝牙设备服务
+   * @param deviceId
+   */
   getBLEDeviceServices(deviceId) {
     const that = this;
     current_device_mac = deviceId
@@ -307,6 +324,7 @@ export default {
             console.log("SelectedServiceItem:" + res.services[i].uuid);
             that.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
             game_store.setters.setServiceId(res.services[i].uuid)
+            is_new = game_store.getters.getIsNew()
             return;
           }
         }
@@ -317,7 +335,9 @@ export default {
     });
   },
 
-  //获取蓝牙设备某个服务中所有特征值
+  /**
+   * todo 获取蓝牙设备某个服务中所有特征值
+   */
   getBLEDeviceCharacteristics(deviceId, serviceId) {
     console.log(deviceId, serviceId);
     const $this = this;
@@ -368,7 +388,11 @@ export default {
     });
   },
 
-  // ArrayBuffer转16进度字符串示例
+  /**
+   * todo ArrayBuffer转16进度字符串示例
+   * @param buffer
+   * @returns {string}
+   */
   ab2hex(buffer) {
     var hexArr = Array.prototype.map.call(
       new Uint8Array(buffer),
@@ -380,8 +404,12 @@ export default {
   },
 
 
-  // 监听脑环数据
+  /**
+   * todo 监听脑环数据
+   * @param $this
+   */
   watchingDevice($this) {
+    console.log("监听数据", "is_new:" + is_new)
     const that = this;
     let DeviceId = game_store.getters.getDeviceId();
     wx.onBLECharacteristicValueChange((characteristic) => {
@@ -418,15 +446,25 @@ export default {
           $this.change_toy_connect_status(3);
           return false;
         }
-        let $hex_index = hexStr.substr(hexStr.length - 4, 2)
-        let $toy_id = hexStr.substr(8, 2)
+        let $mHexStrIndex = hexStr.toUpperCase().indexOf("AADD0A");
+        let $hex_index = hexStr.substr($mHexStrIndex + 28, 2)
+        let $toy_id = hexStr.substr($mHexStrIndex + 8, 2)
         console.log("连接", $hex_index)
         console.log("玩具", $toy_id)
         // 连接上教具
         if (new RegExp("00").test($hex_index) == true) {
-          that.sendConnectOneToOne($toy_id)
-          $this.current_hex = `03 00 ${$toy_id} 01 0A`
-          console.log("一对多")
+          if (is_new) {
+            that.sendConnectOneToOne($toy_id)
+            $this.current_hex = `03 00 ${$toy_id} 01 0A`
+            console.log("一对多")
+          } else {
+            $this.current_hex = "";
+            wx.showToast({
+              title: "已连接到" + $this.toy_item.name
+            });
+            $this.toy_connected = true;
+            $this.change_toy_connect_status(2);
+          }
         }
         if (new RegExp("01").test($hex_index) == true) {
           console.log("一对一")
@@ -448,21 +486,26 @@ export default {
             title: "教具重连成功"
           })
           that.sendControl()
-          that.sendToyPower_CheckReconected();
+          that.sendToyPower();
         }
       }
 
       //2021年11月23日10:39:49
       // 获取教具名称
       if (hexStr.toUpperCase().indexOf("AADD87") != -1) {
-        let $datas = that.DoAnalysis(hexStr, 10)
+        let $mHexStr = hexStr.substr(hexStr.toUpperCase().indexOf("AADD87"))
+        let $datas = that.DoAnalysis($mHexStr, 10)
         let $number = $datas.match(/\d+/)
-        let toy_list_pre= {'01': "SW",'02': "KL",'04': "SC",'05': "PP",'06': "SU",'09': "UF"}
+        let toy_list_pre = {'01': "SW", '02': "KL", '04': "SC", '05': "PP", '06': "SU", '09': "UF"}
         let $sn = toy_list_pre[current_toy_id] + $number;
         $this.toy_sn = $sn;
         //获取教具电量
         that.SendOrder('8a')
       }
+      if (hexStr.toUpperCase().indexOf("AAEE87") != -1) {
+        //获取教具电量
+        that.SendOrder('87')
+      }
 
       //2021年10月21日16:30:07
       // 获取教具电量
@@ -517,50 +560,6 @@ export default {
       }
 
 
-      //2021年10月26日11:09:01
-      // 量产结束读取数据
-      if (hexStr.toUpperCase().indexOf("AADD02") != -1) {
-        //脑环型号
-        let $ring_type = hexStr.substr(10).substr(0, hexStr.substr(10).length - 2);
-        for (let $i = 0; $i < $ring_type.length; $i += 2) {
-          let $code = parseInt($ring_type.substr($i, 2), 16)
-          $this.device_data.product_model += String.fromCharCode($code)
-        }
-        that.SendOrder("01")
-
-      }
-      if (hexStr.toUpperCase().indexOf("AADD01") != -1) {
-        let _UUID = ""
-        let $_str4 = hexStr.substr(6);
-        let $UUID = $_str4.substr(0, $_str4.length - 2);
-        for (let $i = 0; $i < $UUID.length; $i += 8) {
-          let $code = $UUID.substr($i, 8);
-          _UUID += `${$code}-`
-        }
-        _UUID = _UUID.substr(0, _UUID.length - 1)
-        $this.device_data.UUID = _UUID
-        that.SendOrder("03")
-      }
-      if (hexStr.toUpperCase().indexOf("AADD03") != -1) {
-        let $_data = that.DoAnalysis(hexStr, 12);
-        $this.device_data.software_version = $_data.substr(0, 4)
-        // $this.device_data.software_date = $_data.substr(4)
-        that.SendOrder("8d")
-      }
-      if (hexStr.toUpperCase().indexOf("AADD8D") != -1) {
-        let $_str5 = hexStr.substr(10);
-        let $version_date = $_str5.substr(0, $_str5.length - 2);
-        let $_data = "";
-        for (let $i = 0; $i < $version_date.length; $i += 2) {
-          let $code = parseInt($version_date.substr($i, 2), 16)
-          $_data += String.fromCharCode($code)
-        }
-        let $version_data = $_data.substr(0, 2).split('')
-        $this.device_data.hardware_version = `${$version_data[0]}.${$version_data[1]}`
-
-        // setDeviceInDb($this, DeviceId)
-      }
-
       if (hexStr.toUpperCase().indexOf("AADD09") != -1) {
         control_close = true;
       }
@@ -577,17 +576,17 @@ export default {
       if (hexStr.toUpperCase().indexOf("AADD07") != -1) {
         FlagOpenControl = true;
         console.log($game_status)
-        if($game_status != 1){
-           setTimeout(() => {
-              mpvue.navigateTo({
-                url: "/pages/start/main",
-                success() {
-                  // 设置游戏状态为游戏中
-                  game_store.setters.setGameStatus(1);
-                  $this.gameStart();
-                },
-              });
-            }, 800);
+        if ($game_status != 1) {
+          setTimeout(() => {
+            mpvue.navigateTo({
+              url: "/pages/start/main",
+              success() {
+                // 设置游戏状态为游戏中
+                game_store.setters.setGameStatus(1);
+                $this.gameStart();
+              },
+            });
+          }, 800);
         }
       }
 
@@ -643,7 +642,12 @@ export default {
       }
     });
   },
-  // 游玩时间倒计时
+
+  /**
+   * todo 游玩时间倒计时
+   * @param value
+   * @returns {string}
+   */
   formatPlaySeconds(value) {
     // 字符串转数字
     var secondTime = parseInt(value); // 秒
@@ -672,7 +676,12 @@ export default {
     return result;
   },
 
-  //根据错误代码返回字符串信息
+  /**
+   * todo 根据错误代码返回字符串信息
+   * @param errCode
+   * @returns {string}
+   * @constructor
+   */
   GetopenBluetoothAdapterError(errCode) {
 
     let $errmsg = "";
@@ -698,8 +707,13 @@ export default {
 
     return $errmsg;
   },
-  //写入buffer
 
+  /**
+   * todo 写入buffer
+   * @param $hex
+   * @param $buffer_len
+   * @constructor
+   */
   WriteBufferInBle($hex, $buffer_len = 8) {
     let that = this;
     let $deviceInfo = getServicesAndCharateristc();
@@ -738,13 +752,22 @@ export default {
     });
   },
 
-  //写入指令
+  /**
+   * todo写入8位指令
+   * @param id 末尾id
+   * @constructor
+   */
   SendOrder(id) {
     let $hexStr = `03 00 00 00 ${id}`;
     this.WriteBufferInBle($hexStr)
   },
 
-  // 写入16个字节的指令
+  /**
+   * todo 写入16个字节的指令
+   * @param val
+   * @param id
+   * @constructor
+   */
   Send16Order(val, id) {
     let $str = val;
     let $str_ary = $str.split('');
@@ -757,7 +780,16 @@ export default {
 
     this.WriteBufferInBle($hexStr, 16)
   },
-  //解析
+
+
+  /**
+   * todo 解析hex
+   * @param hexStr
+   * @param byte_count
+   * @param sublen
+   * @returns {string}
+   * @constructor
+   */
   DoAnalysis(hexStr, byte_count, sublen = 6) {
     //byte_count
     let $_str5 = hexStr.substr(sublen);
@@ -770,7 +802,10 @@ export default {
     return $_data;
   },
 
-  //重连教具
+  /**
+   * todo 重连教具
+   * @constructor
+   */
   ReconnectToy() {
     current_device_mac = game_store.getters.getDeviceId();
     let $game_status = game_store.getters.getGameStatus();
@@ -830,14 +865,22 @@ export default {
 
   },
 
-  //清空当前脚本的变量值
+  /**
+   * todo 清空当前脚本的变量值
+   */
   clearLocalDatas() {
     control_close = false
     connect_toy = false;
     current_toy_id = "00";
     current_toy_UUID = "";
   },
-  //计算电量
+
+  /**
+   * todo 计算电量
+   * @param $voltage
+   * @returns {number}
+   * @constructor
+   */
   CalBLEPower($voltage) {
     let $max = 4100;
     let $min = 3500;