瀏覽代碼

脑环断连处理

chaooo 1 年之前
父節點
當前提交
7645a2596b
共有 1 個文件被更改,包括 32 次插入27 次删除
  1. 32 27
      src/utils/connection.js

+ 32 - 27
src/utils/connection.js

@@ -544,6 +544,8 @@ export default {
     let $code = ble_store.getters.getDeviceSn();
     //重连的次数
     let $connect_count = 0;
+    let $reconnect_success = false;
+    let $reconnect_err = "";
     let $rec = setInterval(() => {
       let $game_status = game_store.getters.getGameStatus();
       LOG_DEBUG("正在尝试重新连接,游戏状态:", $game_status);
@@ -566,47 +568,50 @@ export default {
                   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);
+                      $reconnect_success = true;
                     },
                     fail(err) {
-                      Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
-                      LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, err.errMsg);
+                      $reconnect_err = err.errMsg;
                     }
                   })
                 }
               });
             });
-          } catch (e) {
-            Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
-            LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, e.errMsg);
+          } catch (err) {
+            $reconnect_err = err.errMsg;
           }
         },
         fail(err) {
-          Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
-          LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, err.errMsg);
+          $reconnect_err = 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;
+      if ($reconnect_success) {
         clearInterval($rec);
-        // 移除搜索到新设备的事件的全部监听函数
-        wx.offBluetoothDeviceFound();
-        // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
-        wx.stopBluetoothDevicesDiscovery();
-        that.clearStatus($this);
+        Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
+        LOG_WECHAT($code, `第${$connect_count}次重新连接成功`);
+        // 获取蓝牙设备服务
+        that.getBLEDeviceServices($deviceId);
+        // 继续游戏
+        setTimeout(function(){
+          $this.startTheGame();
+        }, 3000);
+      } else {
+        Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
+        LOG_WECHAT($code, `第${$connect_count}次重新连接失败`, $reconnect_err);
+        if ($connect_count >= 3) {
+          let $game_status = game_store.getters.getGameStatus();
+          if ($game_status === 1 || $game_status === 2) {
+            $this.endTheGame();
+          }
+          clearInterval($rec);
+          // 移除搜索到新设备的事件的全部监听函数
+          wx.offBluetoothDeviceFound();
+          // 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
+          wx.stopBluetoothDevicesDiscovery();
+          that.clearStatus($this);
+        }
       }
-      $connect_count += 1;
     }, 7000);
   },