Explorar el Código

优化ios系统蓝牙连接

chaooo hace 1 año
padre
commit
cfb224bff7

+ 26 - 19
src/components/connection/brains/connecting.vue

@@ -3,6 +3,7 @@ import ble_store from "@/store/bluetooth";
 import Dialog from "../../../../static/vant/dialog/dialog";
 import Toast from "../../../../static/vant/toast/toast";
 import WechatLog from "@/utils/wechat_log";
+import game_store from "../../../store/game";
 
 let $this;
 export default {
@@ -19,21 +20,27 @@ export default {
   },
   created() {
     $this = this;
+    // 不断获取教具电量
+    let connect_interval = setInterval(() => {
+      console.log("created监听到父级扫码状态", $this.status);
+      if ($this.status*1 === 1) {
+        $this.device_status = 1;
+        $this.getBluetoothState();
+        clearInterval(connect_interval);
+      }
+    }, 500);
   },
-  mounted() {
-    $this.code = ble_store.getters.getDeviceSn();
-  },
-  watch: {
-    'status': {
-      handler: function($status) {
-        console.log("监听到父级扫码状态",$status);
-        if ($status*1 === 1) {
-          this.device_status = 1;
-          this.getBluetoothState();
-        }
-      }, immediate: true
-    }
-  },
+  // watch: {
+  //   'status': {
+  //     handler: function($status) {
+  //       console.log("监听到父级扫码状态",$status);
+  //       if ($status*1 === 1) {
+  //         $this.device_status = 1;
+  //         $this.getBluetoothState();
+  //       }
+  //     }, immediate: true
+  //   }
+  // },
   methods:{
     /**
      * 修改设备连接状态
@@ -50,15 +57,15 @@ export default {
     getBluetoothState() {
       // 获取本机蓝牙适配器状态
       wx.getBluetoothAdapterState({
-        success: function (res) {
+        success: function(res) {
           if (res.available) {
             // 开始搜寻附近的蓝牙外围设备。此操作比较耗费系统资源,请在搜索到需要的设备后及时调用 wx.stopBluetoothDevicesDiscovery 停止搜索。
             wx.startBluetoothDevicesDiscovery({
               allowDuplicatesKey: true,
-              success: () => {
+              success: function(res) {
                 $this.onBluetoothFound();
               },
-              fail() {
+              fail(err) {
                 // 失败取消连接
                 $this.setDeviceStatus(3);
               },
@@ -141,7 +148,7 @@ export default {
       // 连接蓝牙低功耗设备。(若小程序在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需再次进行搜索操作)
       wx.createBLEConnection({
         deviceId: deviceId,
-        success: () => {
+        success: function() {
           // 协商设置蓝牙低功耗的最大传输单元 (Maximum Transmission Unit, MTU)。需在 wx.createBLEConnection 调用成功后调用。仅安卓系统 5.1 以上版本有效,iOS 因系统限制不支持。
           wx.setBLEMTU({
             deviceId,
@@ -150,13 +157,13 @@ export default {
               console.log("设置mtu成功", JSON.stringify(res));
             }
           })
-          // $this.$bluetooth.current_device_sn = $this.code;
           ble_store.setters.setDeviceId(deviceId);
           // 获取蓝牙设备服务
           $this.$connection.getBLEDeviceServices(deviceId);
           //成功连接脑机蓝牙
           $this.setDeviceStatus(2);
           // 记录日志
+          $this.code = ble_store.getters.getDeviceSn();
           WechatLog.info("脑机蓝牙连接成功:", $this.code);
         },
         fail(err) {

+ 7 - 7
src/components/connection/index.vue

@@ -45,13 +45,13 @@ export default {
   created() {
     $this = this;
   },
-  watch: {
-    'connect_toy': {
-      handler: function($status) {
-        console.log("监听connect_toy状态变化",$status);
-      }, immediate: true
-    }
-  },
+  // watch: {
+  //   'connect_toy': {
+  //     handler: function($status) {
+  //       console.log("监听connect_toy状态变化",$status);
+  //     }, immediate: true
+  //   }
+  // },
   methods: {
     /**
      * 获取设备扫码连接状态

+ 1 - 1
src/pages/games/start/index.vue

@@ -438,7 +438,7 @@ export default {
         line: this.game_datas['att'].join(','),
         line_med: this.game_datas['med'].join(','),
         ..._game_datas,
-        sn: wx.getStorageSync('deviceSn')
+        sn: ble_store.getters.getDeviceSn()
       };
       if (!$params.line) {
         return false;

+ 1 - 1
src/pages/user/index.vue

@@ -28,7 +28,7 @@
         <view>
           <!-- <img src="https://img.shuimuai.com/m_icon_jifen%402x.png"/>
           <text>积分</text> -->
-          <text>智脑成长系数</text>
+          <text>成长积分</text>
         </view>
       </view>
     </view>