Explorar o código

脑机游戏中断连处理

chaooo hai 1 ano
pai
achega
67a4f3a03f

+ 0 - 11
src/components/connection/index.vue

@@ -156,17 +156,6 @@ export default {
       // 监听数据
       $this.$connection.notifyDatas($this);
       $this.$connection.watchBLEstatus($this);
-      // 游戏过程中脑机断连
-      let $game_close_status = game_store.getters.getGameCloseStatus();
-      if ($game_close_status === 1) {
-        // 重置默认条件
-        $this.connect_toy = 0;
-        $this.device_status = 0;
-        $this.$connection.watchBLEstatus($this);
-        // 清空链接的设备
-        ble_store.setters.clearDeviceToy();
-        game_store.setters.setPlayedTime(0);
-      }
       $this.$forceUpdate();
     }
   },

+ 0 - 2
src/components/connection/toys/selected.vue

@@ -75,8 +75,6 @@ export default {
       };
       gameStart($params).then(
         (res) => {
-          // 重置断开脑机的状态
-          game_store.setters.setGameCloseStatus(0);
           let $data = res.data;
           let $res = $data.data;
           if ($data.code === 0) {

+ 0 - 6
src/store/game.js

@@ -36,9 +36,6 @@ const setters = {
   setLED(bool) {
     wx.setStorageSync('led_status', bool)
   },
-  setGameCloseStatus(status) {
-    wx.setStorageSync('game_close_status', status)
-  },
 }
 
 const getters = {
@@ -78,9 +75,6 @@ const getters = {
   getLED() {
     return wx.getStorageSync('led_status')
   },
-  getGameCloseStatus() {
-    return wx.getStorageSync('game_close_status')
-  },
 }
 
 const remover = {

+ 90 - 59
src/utils/connection.js

@@ -299,6 +299,7 @@ export default {
   notifyDatas($this) {
     const that = this;
     let deviceId = ble_store.getters.getDeviceId();
+    console.log("%c监听脑机数据", "color:red;", deviceId);
     // 监听蓝牙低功耗设备的特征值变化事件
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = ab2hex(characteristic.value);
@@ -512,14 +513,20 @@ export default {
           //   $this.endTheGame();
           // }
           if($this && $this.$options.name && $this.$options.name === "StartGames"){
-            Notify({
-              type: 'danger',
-              duration: 0,
-              message: '智脑机已断开连接,正在尝试重新连接',
-              onOpened() {
-                that.reconnect(res.deviceId, $this);
-              }
-            });
+            //如果是一代脑则结束游戏
+            if (isJELLYFISH()) {
+              $this.endTheGame();
+            } else {
+              Notify({
+                type: 'danger',
+                duration: 0,
+                message: '智脑机已断开连接,正在尝试重新连接',
+                onOpened() {
+                  that.reconnectDevice(res.deviceId, $this);
+                  console.log("智脑机已断开连接deviceId", res.deviceId);
+                }
+              });
+            }
           }
         } else {
           that.clearStatus($this);
@@ -527,67 +534,79 @@ export default {
       }
     });
   },
-  /**
-   * 清除状态
-   * @param $this
-   */
-  clearStatus($this){
-    wx.closeBluetoothAdapter();
-    game_store.setters.setGameStatus(0);
-    ble_store.setters.clearDeviceToy();
-    $this.device_bg = false;
-    $this.device_status = 0;
-    $this.connect_toy = 0;
-    $this.$forceUpdate();
-  },
 
   /**
-   * todo:重新连接蓝牙
+   *  重新连接蓝牙
    */
-  reconnect($deviceId, $this) {
+  reconnectDevice($deviceId, $this){
     let that = this;
-    let $deviceInfo = getDeviceWriteInfo();
+    let $code = ble_store.getters.getDeviceSn();
     //重连的次数
     let $connect_count = 0;
     let $rec = setInterval(() => {
       let $game_status = game_store.getters.getGameStatus();
       LOG_DEBUG("正在尝试重新连接,游戏状态:", $game_status);
-      if ($game_status === 1 || $game_status === 2) {
-        let $code = ble_store.getters.getDeviceSn();
-        wx.createBLEConnection({
-          deviceId: $deviceInfo.deviceId,
-          success() {
-            clearInterval($rec)
-            Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
-            LOG_WECHAT($code, `第${$connect_count}次重新连接成功`);
-            let $system = wx.getSystemInfoSync()
-            if ($system.platform === 'ios') {
-              that.getBLEDeviceServices($deviceInfo.deviceId)
-              that.notifyDatas($this)
-            } else {
-              that.watchBLEstatus($this);
-            }
-          },
-          fail() {
+      // 开始搜寻附近的蓝牙外围设备。此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索。
+      wx.startBluetoothDevicesDiscovery({
+        allowDuplicatesKey: true,
+        success: function(res) {
+          try {
+            // 监听搜索到新设备的事件
+            wx.onBluetoothDeviceFound((res) => {
+              res.devices.forEach((device) => {
+                if (!device.name && !device.localName) { return; }
+                if (device.localName && device.localName !== "") {
+                  device.name = device.localName;
+                }
+                if (device["name"].toUpperCase() === $code) {
+                  // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
+                  wx.stopBluetoothDevicesDiscovery();
+                  // 连接低功耗蓝牙设备
+                  wx.createBLEConnection({
+                    deviceId: $deviceId,
+                    success() {
+                      clearInterval($rec)
+                      Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
+                      LOG_WECHAT($code, `第${$connect_count}次重新连接成功`);
+                      // 获取蓝牙设备服务
+                      that.getBLEDeviceServices($deviceId);
+                      // 继续游戏
+                      setTimeout(function(){
+                        $this.startTheGame();
+                      }, 3000);
+                    },
+                    fail(err) {
+                      Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
+                      LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, err.errMsg);
+                    }
+                  })
+                }
+              });
+            });
+          } catch (e) {
             Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
-            LOG_WECHAT($code, `第${$connect_count}次重新连接失败`);
-            game_store.setters.setGameCloseStatus(1);
-          }
-        })
-        if ($connect_count >= 3) {
-          let $game_status = game_store.getters.getGameStatus();
-          if ($game_status === 1 || $game_status === 2) {
-            $this.endTheGame();
+            LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, e.errMsg);
           }
-          that.clearStatus($this);
-          clearInterval($rec)
+        },
+        fail(err) {
+          Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
+          LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, err.errMsg);
+        },
+      });
+      if ($connect_count >= 3) {
+        let $game_status = game_store.getters.getGameStatus();
+        if ($game_status === 1 || $game_status === 2) {
+          $this.endTheGame();
         }
-        $connect_count += 1;
-      } else {
-        clearInterval($rec)
+        clearInterval($rec);
+        // 移除搜索到新设备的事件的全部监听函数
+        wx.offBluetoothDeviceFound();
+        // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
+        wx.stopBluetoothDevicesDiscovery();
+        that.clearStatus($this);
       }
-    }, 7000)
-
+      $connect_count += 1;
+    }, 7000);
   },
 
   /**
@@ -602,13 +621,10 @@ export default {
     game_store.setters.setGameStatus(0);
     // todo 清空链接的设备
     $this.connect_toy = 0;
-    //$this.connect_show = false;
     $this.device_bg = false;
-    //$this.connect_status = false;
     // 断开教具及蓝牙连接
     setTimeout(()=>{
       that.SendOrder("31", "断开教具及蓝牙连接");
-      // that.SendOrder("09", "关闭脑控");
       // 移除搜索到新设备的事件的全部监听函数
       wx.offBluetoothDeviceFound();
       // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
@@ -634,6 +650,21 @@ export default {
       });
     },500);
   },
+  /**
+   * 清除状态
+   */
+  clearStatus($this){
+    wx.closeBluetoothAdapter();
+    game_store.setters.setGameStatus(0);
+    ble_store.setters.clearDeviceToy();
+    $this.device_bg = false;
+    $this.device_status = 0;
+    $this.connect_toy = 0;
+    $this.$forceUpdate();
+  },
+  /**
+   * 错误信息
+   */
   connectionError(errCode) {
     if (errCode === 10000) {
       return "未初始化蓝牙适配器";