Browse Source

体验版C端小程序
调整了 蓝牙链接的顺序 兼容了新旧脑环
依旧关闭了教具重连
修复了 在游戏中脑环断开后,点结束游戏 会重复跳出报告 的bug
修复了 偶发的 点击开始游戏后 教具白灯常亮没打开脑控的 bug
修复了 偶发的 脑环与小程序重连后 小程序页面会卡死的 bug
修复了小程序 苹果连接不上 脑环和教具

叶振荣 3 năm trước cách đây
mục cha
commit
a760f3357f
3 tập tin đã thay đổi với 114 bổ sung111 xóa
  1. 27 23
      src/components/device/device.vue
  2. 4 4
      src/pages/start/index.vue
  3. 83 84
      src/utils/bluetooth.js

+ 27 - 23
src/components/device/device.vue

@@ -117,10 +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>
+<!--    <view style="width: 100%;height: 300px;">-->
+<!--      <view>脑环Mac:{{ device.deviceId }}</view>-->
+<!--      <view>教具UUID:{{ toy_UUID }}</view>-->
+<!--    </view>-->
     <!--    <van-popup-->
     <!--      :show="start_show"-->
     <!--      :closeable="false"-->
@@ -384,7 +384,7 @@ export default {
         if ($this.toy_connected == false) {
           $this.change_toy_connect_status(3);
         }
-      }, 5000)
+      }, 10000)
     },
     //修改玩具连接状态
     change_toy_connect_status($status = 0) {
@@ -437,7 +437,7 @@ export default {
     //开始蓝牙被发现
     startBluetoothDevicesDiscovery() {
       wx.startBluetoothDevicesDiscovery({
-        services: ["0000fff0-0000-1000-8000-00805f9b34fb", "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"],
+        services: ["0000FFF0-0000-1000-8000-00805F9B34FB", "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"],
         allowDuplicatesKey: true,
         success: (res) => {
           //2021年10月21日15:07:57  通过sn 返回deviceId
@@ -481,21 +481,17 @@ export default {
     onBluetoothDeviceFound() {
       try {
         // 5秒后判断 这5秒在搜索设备,搜索到就赋值给_deviceId
-        setTimeout(() => {
-          if (!$this.device.deviceId) {
-            wx.stopBluetoothDevicesDiscovery();
-            $this.change_device_status(0);
-            Toast.fail("未连接到设备");
-          } else if ($this.device_status == 1) {
-            wx.stopBluetoothDevicesDiscovery();
-            $this.change_device_status(0);
-            Toast.fail("设备未连接到");
-            // console.log(
-            //   "设备未连接到device_status的状态为:",
-            //   $this.device_status
-            // );
-          }
-        }, 5000);
+        // setTimeout(() => {
+        //   if (!$this.device.deviceId) {
+        //     wx.stopBluetoothDevicesDiscovery();
+        //     $this.change_device_status(0);
+        //     Toast.fail("未连接到设备");
+        //   } else if ($this.device_status == 1) {
+        //     wx.stopBluetoothDevicesDiscovery();
+        //     $this.change_device_status(0);
+        //     Toast.fail("设备未连接到");
+        //   }
+        // }, 5000);
         // 小乌龟
         wx.onBluetoothDeviceFound((res) => {
           res.devices.forEach((device) => {
@@ -530,8 +526,16 @@ export default {
 
           //成功连接脑环蓝牙
           $this.change_device_status(2);
-          bluetooth.openNotify($this)
-          bluetooth.watch_bluetooth_status($this);
+
+          let $system = wx.getSystemInfoSync()
+          if ($system.platform == 'ios') {
+            bluetooth.getBLEDeviceServices($this.device.deviceId)
+            bluetooth.watchingDevice($this)
+          } else {
+            bluetooth.openNotify($this)
+            bluetooth.watch_bluetooth_status($this);
+          }
+
 
           //录入库
           //读取信息逐个写入

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

@@ -1,9 +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>
+<!--    <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">

+ 83 - 84
src/utils/bluetooth.js

@@ -22,17 +22,17 @@ var FlagOpenControl = false;
 //2021年11月10日14:59:38
 // 0为旧 1为新
 var services = [
-  "6e400001-b5a3-f393-e0a9-e50e24dcca9e",
-  "0000fff0-0000-1000-8000-00805f9b34fb"
+  "6e400001-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase(),
+  "0000fff0-0000-1000-8000-00805f9b34fb".toUpperCase()
 ]
 var charateristics = [
   {
-    notify: "6e400003-b5a3-f393-e0a9-e50e24dcca9e",
-    write: "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
+    notify: "6e400003-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase(),
+    write: "6e400002-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase()
   },
   {
-    notify: "0000fff1-0000-1000-8000-00805f9b34fb",
-    write: "0000fff2-0000-1000-8000-00805f9b34fb"
+    notify: "0000fff1-0000-1000-8000-00805f9b34fb".toUpperCase(),
+    write: "0000fff2-0000-1000-8000-00805f9b34fb".toUpperCase()
   },
 ]
 
@@ -42,9 +42,9 @@ function getServicesAndCharateristc() {
   let $deviceId = game_store.getters.getDeviceId();
   let $index = $brainSn.toUpperCase().indexOf("JELLYFISH") != -1 ? 0 : 1;
   return {
-    deviceId:$deviceId,
-    service:services[$index],
-    charateristic:charateristics[$index]
+    deviceId: $deviceId,
+    service: services[$index],
+    charateristic: charateristics[$index]
   }
 }
 
@@ -274,83 +274,72 @@ export default {
   },
 
   // //获取蓝牙设备服务
-  // getBLEDeviceServices(deviceId) {
-  //   const $this = this;
-  //   current_device_mac = deviceId
-  //   wx.getBLEDeviceServices({
-  //     deviceId,
-  //     success: (res) => {
-  //       for (let i = 0; i < res.services.length; i++) {
-  //         console.log("serviceItem:" + res.services[i].uuid);
-  //         if (res.services[i].uuid.indexOf('6E') != -1 || res.services[i].uuid.indexOf('0000FFF0') != -1) {
-  //           console.log("SelectedServiceItem:" + res.services[i].uuid);
-  //           $this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
-  //           return;
-  //         }
-  //       }
-  //     },
-  //     fail(res) {
-  //       console.log("连接蓝牙成功,获取服务失败");
-  //     },
-  //   });
-  // },
-  //
-  // //获取蓝牙设备某个服务中所有特征值
-  // getBLEDeviceCharacteristics(deviceId, serviceId) {
-  //   console.log(deviceId, serviceId);
-  //   const $this = this;
-  //   wx.getBLEDeviceCharacteristics({
-  //     deviceId,
-  //     serviceId,
-  //     success: (res) => {
-  //       console.log("getBLEDeviceCharacteristics success", res.characteristics);
-  //       for (let i = 0; i < res.characteristics.length; i++) {
-  //         let item = res.characteristics[i];
-  //         if (item.properties.read) {
-  //           wx.readBLECharacteristicValue({
-  //             deviceId,
-  //             serviceId,
-  //             characteristicId: item.uuid,
-  //           });
-  //         }
-  //         if (item.properties.write) {
-  //           $this.deviceId = deviceId;
-  //           $this.serviceId = serviceId;
-  //           $this.cid = item.uuid;
-  //           game_store.setters.setDeviceId(deviceId);
-  //           game_store.setters.setServiceId(serviceId);
-  //           game_store.setters.setCid(item.uuid);
-  //           //打开数据帧
-  //           $this.sendOpenBigData();
-  //         }
-  //
-  //         if (item.properties.notify || item.properties.indicate) {
-  //           wx.notifyBLECharacteristicValueChange({
-  //             deviceId,
-  //             serviceId,
-  //             characteristicId: item.uuid,
-  //             state: true,
-  //           });
-  //           game_store.setters.setReadCid(item.uuid);
-  //         }
-  //       }
-  //     },
-  //     fail(res) {
-  //       console.error("getBLEDeviceCharacteristics", res);
-  //     },
-  //   });
-  // },
-
-  openNotify($this){
+  getBLEDeviceServices(deviceId) {
+    const that = this;
+    current_device_mac = deviceId
+    wx.getBLEDeviceServices({
+      deviceId,
+      success: (res) => {
+        for (let i = 0; i < res.services.length; i++) {
+          console.log("serviceItem:" + res.services[i].uuid);
+          if (res.services[i].uuid.indexOf('6E') != -1 || res.services[i].uuid.indexOf('0000FFF0') != -1) {
+            console.log("SelectedServiceItem:" + res.services[i].uuid);
+            that.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
+            return;
+          }
+        }
+      },
+      fail(res) {
+        console.log("连接蓝牙成功,获取服务失败");
+      },
+    });
+  },
+
+  //获取蓝牙设备某个服务中所有特征值
+  getBLEDeviceCharacteristics(deviceId, serviceId) {
+    console.log(deviceId, serviceId);
+    const $this = this;
+    wx.getBLEDeviceCharacteristics({
+      deviceId,
+      serviceId,
+      success: (res) => {
+        console.log("getBLEDeviceCharacteristics success", res.characteristics);
+        for (let i = 0; i < res.characteristics.length; i++) {
+          let item = res.characteristics[i];
+          if (item.properties.write) {
+            $this.deviceId = deviceId;
+            $this.serviceId = serviceId;
+            $this.cid = item.uuid;
+            //打开数据帧
+            $this.sendOpenBigData();
+          }
+
+          if (item.properties.notify || item.properties.indicate) {
+            wx.notifyBLECharacteristicValueChange({
+              deviceId,
+              serviceId,
+              characteristicId: item.uuid,
+              state: true,
+            });
+          }
+        }
+      },
+      fail(res) {
+        console.error("getBLEDeviceCharacteristics", res);
+      },
+    });
+  },
+
+  openNotify($this) {
     let that = this;
     let $deviceInfo = getServicesAndCharateristc();
     console.log($deviceInfo)
     wx.notifyBLECharacteristicValueChange({
-      deviceId:$deviceInfo.deviceId,
-      serviceId:$deviceInfo.service,
+      deviceId: $deviceInfo.deviceId,
+      serviceId: $deviceInfo.service,
       characteristicId: $deviceInfo.charateristic.notify,
       state: true,
-      success(){
+      success() {
         that.watchingDevice($this)
         that.sendOpenBigData();
       }
@@ -374,9 +363,18 @@ export default {
     const that = this;
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
-      if (hexStr.toUpperCase().indexOf("AADD") != -1) {
-        console.log(hexStr);
-      }
+
+      wx.getSystemInfo({
+        success(res) {
+          if (res.platform == "ios") {
+            console.log(hexStr)
+          } else {
+            if (hexStr.toUpperCase().indexOf("AADD") != -1) {
+              console.log(hexStr);
+            }
+          }
+        }
+      });
       let $data = that.get_big_data(hexStr);
       let $game_status = game_store.getters.getGameStatus();
 
@@ -631,6 +629,7 @@ export default {
     return $errmsg;
   },
   //写入buffer
+
   WriteBufferInBle($hex, $buffer_len = 8) {
     let $deviceInfo = getServicesAndCharateristc();
     let $hex_header = "aa cc ";
@@ -662,7 +661,7 @@ export default {
       },
       fail: function (err) {
         console.log($hex + "写入失败");
-        console.log(err);
+        console.log(err,$deviceInfo);
       },
     });
   },