Pārlūkot izejas kodu

修改了新的BLE协议

智方网络_吖荣 3 gadi atpakaļ
vecāks
revīzija
5fa6e93a9a

+ 1 - 1
config/dev.env.js

@@ -3,6 +3,6 @@ var prodEnv = require('./prod.env')
 
 module.exports = merge(prodEnv, {
   NODE_ENV: '"development"',
-  REQUEST_URI: "'https://api.shuimuai.com/'"
+  REQUEST_URI: "'https://testapi.shuimuai.com/'"
   // REQUEST_URI: "'https://testapi.shuimuai.com/'"
 })

+ 3 - 3
src/components/device/connected.vue

@@ -72,9 +72,9 @@ export default {
 }
 
 .device_elc {
-  height: 10 rpx;
-  border-radius: 5 rpx;
-  bottom: 3 rpx;
+  height: 5px;
+  border-radius: 2.5px;
+  bottom: 1.5px;
 }
 
 </style>

+ 40 - 9
src/components/device/device.vue

@@ -47,6 +47,7 @@
             :toy="toy_item"
             :device_bg="device_bg"
             :device_power="device_power"
+            :toy_power="toy_power"
             @open_choose_toy="open_choose_toy"
             @change_toy_connect_status="change_toy_connect_status"
             @change_status="change_device_status"
@@ -113,8 +114,8 @@
         </button>
       </div>
     </van-popup>
-    <van-toast id="van-toast" />
-    <van-dialog id="van-dialog" />
+    <van-toast id="van-toast"/>
+    <van-dialog id="van-dialog"/>
 
     <!--    <van-popup-->
     <!--      :show="start_show"-->
@@ -141,7 +142,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 } from "../../requests/game";
+import {game_devices, getDeviceBySn} from "../../requests/game";
 import game_store from "@/store/game";
 import bluetooth from "@/utils/bluetooth";
 import ble_store from "../../store/bluetooth";
@@ -183,17 +184,23 @@ export default {
       _deviceId: "",
       _device_index: false,
       toy_id: 0,
-      toy_hex:"",
+      toy_hex: "",
 
       // 电量
       device_power: 0,
+      //教具电量
+      toy_power: 0,
+      //教具名称
+      toy_sn: "",
+      //UUID
+      toy_UUID: "",
 
       //  开始游戏模块
       start_show: false,
       game_status: 0,
 
       //当前发送的hex码
-      current_hex:""
+      current_hex: ""
     };
   },
   methods: {
@@ -220,6 +227,7 @@ export default {
             // 判断新的标识值
             $this.code = $code[1].toUpperCase();
             console.log("头环码", $code);
+
             //打开蓝牙设备
             wx.getSystemInfo({
               success(res) {
@@ -320,7 +328,7 @@ export default {
         }
       });
       $this.toy_id = $toy.id;
-      let $hex = $this.toy_hex =  $toy["hex"].substr($toy["hex"].length - 2, 2);
+      let $hex = $this.toy_hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
       //连接玩具
       $this.current_hex = `03 00 ${$hex} 00 0a`;
       bluetooth.sendConnectOneToMore($hex);
@@ -341,7 +349,7 @@ export default {
       //当脑环断开
       if ($status == 0) {
         game_store.setters.setGameStatus(0);
-        // bluetooth.shutdownSendControl();
+        bluetooth.sendControlClose();
         // 清空链接得设备 三值
         game_store.setters.clearDeviceToy();
         $this.connect_toy = $status;
@@ -366,8 +374,31 @@ export default {
       wx.startBluetoothDevicesDiscovery({
         allowDuplicatesKey: true,
         success: (res) => {
-          $this.onBluetoothDeviceFound();
-          $this.change_device_status(1);
+          //2021年10月21日15:07:57  通过sn 返回deviceId
+          getDeviceBySn($this.code).then((res) => {
+            let $data = res.data;
+            console.log($data)
+            $this.change_device_status(1);
+            if ($data.code == 0) {
+              $data = $data.data;
+              wx.getSystemInfo({
+                success(res) {
+                  // 判断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;
+                  }
+                  $this.createBLEConnection()
+                }
+              });
+            } else {
+              $this.onBluetoothDeviceFound();
+            }
+          })
+
         },
         fail(err) {
           $this.change_device_status(3);

+ 3 - 2
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_power}}%)
             </template>
           </text>
         </div>
@@ -297,6 +297,7 @@ export default {
     "toy",
     "device_bg",
     "device_power",
+    "toy_power",
   ],
   data() {
     return {
@@ -328,7 +329,7 @@ export default {
       $this.pay_type = $event;
       let $params = {
         type: $event,
-        device_id: $this.toy_id, 
+        device_id: $this.toy_id,
         access_token: wx.getStorageSync("token"),
       };
       gameStart($params).then(

+ 10 - 0
src/requests/game.js

@@ -43,3 +43,13 @@ export function gameList($params = {}) {
   $params["access_token"] = wx.getStorageSync("token");
   return fly.post("game/game-list", $params);
 }
+
+//2021年10月21日15:08:57
+//获取设备
+export function getDeviceBySn($sn){
+  let $params = {
+    "access_token":wx.getStorageSync("token"),
+    "sn":$sn
+  }
+  return fly.post("equipment/equipment-search",$params)
+}

+ 64 - 34
src/utils/bluetooth.js

@@ -92,16 +92,6 @@ export default {
     let $high_beta_2 = hex.substr(hex.indexOf("0418") + 36, 2);
     let $high_beta_3 = hex.substr(hex.indexOf("0418") + 38, 2);
     let $high_beta = parseInt($high_beta_1 + $high_beta_2 + $high_beta_3, 16);
-    console.log(
-      "att: " + $att,
-      "med: " + $med,
-      "delta: " + $delta,
-      "theta: " + $theta,
-      "low_alpha: " + $low_alpha,
-      "high_alpha: " + $high_alpha,
-      "low_beta: " + $low_beta,
-      "high_beta: " + $high_beta,
-    );
     let $max_num = 30000;
     return {
       att: $att,
@@ -243,8 +233,11 @@ export default {
     const that = this;
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
-      console.log(hexStr);
+      if (hexStr.toUpperCase().indexOf("AADD")!=-1){
+        console.log(hexStr);
+      }
       let $data = that.get_big_data(hexStr);
+      let $game_status = game_store.getters.getGameStatus();
 
       // 2021年10月20日17:18:13 判断教具 连接
       if (hexStr.toUpperCase().indexOf("AADD0A") != -1) {
@@ -272,25 +265,17 @@ export default {
           }
         }
       }
-      //监听连接教具
-      // 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();
-      // }
+
+
+      //2021年10月21日16:30:07 获取教具电量
+      if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
+        let $_hexStr = hexStr.substr(6);
+        let $power = parseInt($_hexStr.substr(0, 2), 16)
+        let $voltage = parseInt($_hexStr.substr(2, 2), 16)
+        console.log("电量:" + $power)
+        console.log("电压:" + $voltage / 10)
+        $this.toy_power = $power;
+      }
 
       //监听佩戴正确
       if (hexStr.substr(0, 6) == "555520") {
@@ -301,9 +286,38 @@ 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;
+
+          }
+        }
+
+      }
+
+
       //  监听脑环电量
       let $power = that.get_device_elc(hexStr);
       if ($power) {
+        //发送一次获取教具的UUID
+        that.SendOrder("84");
+
         // console.log("当前脑环电量:", $power)
         $this.device_power = $power;
       }
@@ -319,7 +333,6 @@ export default {
       }
 
       //游戏中模块
-      let $game_status = game_store.getters.getGameStatus();
       if ($game_status == 1 && $data) {
         try {
           if ($this.played_time > 0) {
@@ -403,7 +416,7 @@ export default {
     return $errmsg;
   },
   //写入buffer
-  WriteBufferInBle($hex) {
+  WriteBufferInBle($hex,$buffer_len = 8) {
     let $device_id = game_store.getters.getDeviceId();
     let $service_id = game_store.getters.getServiceId()
     let $charateristic = game_store.getters.getCharacterId();
@@ -419,7 +432,7 @@ export default {
     let $checksum = ($hex_sum ^ parseInt($8f, 16)) & parseInt($ff, 16);
 
     $hex = $hex_header + $hex + " " + $checksum.toString(16);
-    let buffer = new ArrayBuffer(8);
+    let buffer = new ArrayBuffer($buffer_len);
     let dataView = new DataView(buffer);
     $hex_ary = $hex.split(" ");
     $hex_ary.forEach(($val, $index) => {
@@ -439,5 +452,22 @@ export default {
         console.log(err);
       },
     });
-  }
+  },
+  //写入指令
+  SendOrder(id) {
+    let $hexStr = `03 00 00 00 ${id}`;
+    this.WriteBufferInBle($hexStr)
+  },
+  Send16Order(val, id) {
+    let $str = val;
+    let $str_ary = $str.split('');
+    $str = "";
+    $str_ary.forEach(($val, $index) => {
+      $str += $val.charCodeAt().toString(16) + " "
+    });
+    $str = $str.substr(0, $str.length - 1)
+    let $hexStr = "03 ff " + $str + ` ${id}`;
+    this.WriteBufferInBle($hexStr, 16)
+  },
+
 };