智方网络_吖荣 3 年之前
父節點
當前提交
56a0cbf207
共有 6 個文件被更改,包括 90 次插入72 次删除
  1. 1 1
      src/App.vue
  2. 2 2
      src/components/banner.vue
  3. 8 7
      src/components/device/device.vue
  4. 12 12
      src/pages/index/index.vue
  5. 5 6
      src/pages/start/index.vue
  6. 62 44
      src/utils/bluetooth.js

+ 1 - 1
src/App.vue

@@ -8,7 +8,7 @@ export default {
   //外发
   onShow(options){
       let query = options.query.other
-      console.log('进入小程序了',options)
+      // console.log('进入小程序了',options)
       // 有无参数
       if(query){
         console.log('外发给你')

+ 2 - 2
src/components/banner.vue

@@ -71,7 +71,7 @@ export default {
     },
     getBanners() {
       getBannerList().then((res) => {
-        console.log("活动列表ID:", res);
+        // console.log("活动列表ID:", res);
         let $res = res.data;
         let $activities = $res.data;
         let $new_act = $activities.pop();
@@ -90,7 +90,7 @@ export default {
     //   getJointBanner().then((res) => {
     //     console.log("banner活动", res.data.data)
     //     let $banner_img = res.data.data
-        
+
     //     $this.banners[0].src = $banner_img[0].img
     //     $this.banners[2].src = $banner_img[1].img
     //   });

+ 8 - 7
src/components/device/device.vue

@@ -183,6 +183,7 @@ export default {
       _deviceId: "",
       _device_index: false,
       toy_id: 0,
+      toy_hex:"",
 
       // 电量
       device_power: 0,
@@ -190,6 +191,9 @@ export default {
       //  开始游戏模块
       start_show: false,
       game_status: 0,
+
+      //当前发送的hex码
+      current_hex:""
     };
   },
   methods: {
@@ -206,9 +210,6 @@ export default {
       wx.scanCode({
         onlyFromCamera: true,
         success: (res) => {
-          wx.vibrateShort({
-            type: "heavy",
-          });
 
           let $data = res;
           if ($data.result) {
@@ -304,7 +305,7 @@ export default {
       // $this.choose_toy_window.show = false;
       $this.choose_toy_window.show = true;
 
-      bluetooth.sendEnd();
+      bluetooth.sendControlClose();
     },
     // 选好玩具
     choose_ok() {
@@ -319,10 +320,10 @@ export default {
         }
       });
       $this.toy_id = $toy.id;
-      let $hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
+      let $hex = $this.toy_hex =  $toy["hex"].substr($toy["hex"].length - 2, 2);
       //连接玩具
-
-      bluetooth.sendConnect($hex);
+      $this.current_hex = `03 00 ${$hex} 00 0a`;
+      bluetooth.sendConnectOneToMore($hex);
     },
     //修改玩具连接状态
     change_toy_connect_status($status = 0) {

+ 12 - 12
src/pages/index/index.vue

@@ -177,16 +177,16 @@ export default {
     } else{
        wx.setStorageSync("is_other", true);
     }
-    
-    wx.getStorageInfo({
-      success(res) {
-        console.log("当前存储的数据", res.keys);
-        let $keys = res.keys;
-        $keys.forEach(($val, $index) => {
-          console.log($val, wx.getStorageSync($val));
-        });
-      },
-    });
+
+    // wx.getStorageInfo({
+    //   success(res) {
+    //     console.log("当前存储的数据", res.keys);
+    //     let $keys = res.keys;
+    //     $keys.forEach(($val, $index) => {
+    //       console.log($val, wx.getStorageSync($val));
+    //     });
+    //   },
+    // });
     userDetail().then((res) => {
       let $data = res.data;
       //判断如果是第一次登录就 弹出领取体验卡
@@ -197,7 +197,7 @@ export default {
   onHide() {},
   onLoad($option) {
     if (process.env.NODE_ENV == "development") {
-      // wx.setStorageSync("token", "ZbNv-hpdpmxk895iqUrEwe4kCJjEWCJD");
+      wx.setStorageSync("token", "ogsti72oUSkbPl4OL7gl9woLTxR6fk4V");
     }
 
     if (game_store.getters.getGameStatus() == 1) {
@@ -220,7 +220,7 @@ export default {
       // });
       // 存在token时候录入
       setUserLogin().then((res) => {
-        console.log("录入", res);
+        // console.log("录入", res);
       });
     }
   },

+ 5 - 6
src/pages/start/index.vue

@@ -348,13 +348,12 @@ export default {
   },
   methods: {
     //游戏结束方法
-    game_finished() {  
+    game_finished() {
         if (!$this.executed){
-          bluetooth.sendEnd();
+          bluetooth.sendControlClose();
 
           $this.$emit("closePop", true);
-          //取消监听低功耗蓝牙设备
-          wx.offBLECharacteristicValueChange();
+
           //设置游戏状态为 停止游戏
           game_store.getters.getGameStatus() == 0
             ? game_store.setters.setGameStatus(0)
@@ -375,7 +374,7 @@ export default {
 
           $this.game_over();
 
-          
+
           Toast.loading({
               forbidClick: true,
               mask: true,
@@ -680,7 +679,7 @@ export default {
     console.log("小程序被销毁");
     if (!$this.is_end) {
       //停止传输数据
-      bluetooth.sendEnd();
+      bluetooth.sendControlClose();
       wx.offBLECharacteristicValueChange();
       //存储时间
       game_store.setters.setPlayedTime($this.played_time);

+ 62 - 44
src/utils/bluetooth.js

@@ -6,22 +6,38 @@ var $ff = "ff";
 var $8f = "ffffffff"
 export default {
 
-  // 发送开启脑电数据包指令
-  sendOpen() {
+  // 打开大包数据
+  sendOpenBigData() {
     this.WriteBufferInBle("03 00 00 00 ff");
   },
-  // 发送开启教具脑控指令
+  // 关闭大包数据
+  sendCloseBigData() {
+    this.WriteBufferInBle("03 00 00 00 00");
+  },
+  //获取当前连接的教具类型
+  sendGetToyType() {
+    this.WriteBufferInBle("03 00 00 00 06");
+  },
+  // 开启脑控
   sendControl() {
     this.WriteBufferInBle("03 00 00 00 07");
   },
-  //发送断开玩具的指令
-  sendEnd() {
+  //暂停脑控
+  sendControlPause() {
     //AA CC 03 00 00 00 09 F3
+    this.WriteBufferInBle("03 00 00 00 08");
+  },
+  // 关闭脑控
+  sendControlClose() {
     this.WriteBufferInBle("03 00 00 00 09");
   },
-  // 发送停止发送脑电数据包指令
-  sendPause() {
-    this.WriteBufferInBle("03 00 00 00 08");
+  //发送一对多连接
+  sendConnectOneToMore(id) {
+    this.WriteBufferInBle(`03 00 ${id} 00 0A`);
+  },
+  //发送一对一连接
+  sendConnectOneToOne(id) {
+    this.WriteBufferInBle(`03 00 ${id} 01 0A`)
   },
 
   //  获取大包数据 进行绘制图表
@@ -192,10 +208,7 @@ export default {
             game_store.setters.setServiceId(serviceId);
             game_store.setters.setCid(item.uuid);
             //打开数据帧
-            $this.sendOpen();
-            // $this.sendPause(deviceId, serviceId, item.uuid)
-            //获取玩具的值
-            // bluetooth.sendConnect($this.toy_hex, deviceId, serviceId, item.uuid)
+            $this.sendOpenBigData();
           }
 
           if (item.properties.notify || item.properties.indicate) {
@@ -215,15 +228,6 @@ export default {
   },
 
 
-  // 发送连接玩具指令
-  sendConnect($hex) {
-    //获取最后两个字节
-    this.WriteBufferInBle("03 00 " + $hex + " 00 0a");
-  },
-  //  获取当前连接得玩具
-  getConnectedToy() {
-    this.WriteBufferInBle("03 00 00 00 06");
-  },
   // ArrayBuffer转16进度字符串示例
   ab2hex(buffer) {
     var hexArr = Array.prototype.map.call(
@@ -234,11 +238,6 @@ export default {
     );
     return hexArr.join("");
   },
-  //断开发送数据
-  shutdownSendControl() {
-    this.WriteBufferInBle("03 00 00 00 0a");
-  },
-
   // 监听脑环是否带正
   watchingDevice($this) {
     const that = this;
@@ -246,33 +245,52 @@ export default {
       let hexStr = that.ab2hex(characteristic.value);
       console.log(hexStr);
       let $data = that.get_big_data(hexStr);
-      // wx.getBLEDeviceRSSI({
-      //   deviceId: characteristic.deviceId,
-      //   success(res) {
-      //     $this.rssi = Math.abs(res["RSSI"]);
-      //   },
-      // });
 
-      //监听连接教具
-      if (hexStr.substr(0, 8).toLowerCase() == "aadd0a00") {
-        if ($this.toy_item["hex"]) {
-          let $hex = $this.toy_item["hex"].substr($this.toy_item["hex"].length - 2, 2);
-          if (hexStr.substr(8, 2) == $hex) {
+      // 2021年10月20日17:18:13 判断教具 连接
+      if (hexStr.toUpperCase().indexOf("AADD0A") != -1) {
+        //没连接上教具
+        if (hexStr.toUpperCase().indexOf("AADD0A0000") != -1) {
+          $this.change_toy_connect_status(3);
+          return false;
+        }
+        let $hexAry = $this.current_hex.split(" ")
+        // 03 00 01 00 0a
+        //      [2] [3]
+        // 连接上教具
+        if (new RegExp("00").test($hexAry[2]) == false) {
+          if (new RegExp("00").test($hexAry[3]) == true) {
+            that.sendConnectOneToOne($hexAry[2])
+            $this.current_hex = `03 00 $hexAry[2] 01 0A`
+            console.log("一对多")
+          } else {
+            console.log("一对一")
+            $this.current_hex = "";
             wx.showToast({
               title: "已连接到" + $this.toy_item.name
             });
             $this.change_toy_connect_status(2);
-          } else if (hexStr.substr(8, 2) == "00") {
-            $this.change_toy_connect_status(3);
           }
         }
       }
+      //监听连接教具
+      // if (hexStr.substr(0, 8).toLowerCase() == "aadd0a00") {
+      //   if ($this.toy_item["hex"]) {
+      //     let $hex = $this.toy_item["hex"].substr($this.toy_item["hex"].length - 2, 2);
+      //     if (hexStr.substr(8, 2) == $hex) {
+      //       wx.showToast({
+      //         title: "已连接到" + $this.toy_item.name
+      //       });
+      //       $this.change_toy_connect_status(2);
+      //     } else if (hexStr.substr(8, 2) == "00") {
+      //       $this.change_toy_connect_status(3);
+      //     }
+      //   }
+      // }
 
       //暂停脑控,切断教具连接
-      if (hexStr.substr(0, 8).toLowerCase() == "aadd0900") {
-        this.shutdownSendControl();
-      }
-
+      // if (hexStr.substr(0, 8).toLowerCase() == "aadd0900") {
+      //   this.shutdownSendControl();
+      // }
 
       //监听佩戴正确
       if (hexStr.substr(0, 6) == "555520") {
@@ -317,7 +335,7 @@ export default {
           if ($this.played_time == 0) {
             $this.post_data();
             //判断是否隐藏 隐藏则不绘画
-            that.shutdownSendControl();
+            that.sendControlClose();
             let $hide_status = game_store.getters.getHideStatus();
             if (!$hide_status) {
               $this.game_finished();