Переглянути джерело

630/691蓝牙模组兼容

叶振荣 3 роки тому
батько
коміт
74c05f9374

+ 42 - 51
src/components/device/device.vue

@@ -42,7 +42,6 @@
         <div class="connecting_toy">
           <toy_connecting
             :connect_toy="connect_toy"
-            :deviceId="device.deviceId"
             :toy_id="toy_action"
             :toy="toy_item"
             :device_bg="device_bg"
@@ -182,7 +181,6 @@ export default {
       //连接玩具 0:未连接 1:连接中 2:已连接 3:连接失败 4:游戏中
       connect_toy: 0,
       code: "jellyfish1234",
-      device: {},
       deviceId: "",
       _device_index: false,
       toy_id: 0,
@@ -250,7 +248,7 @@ export default {
     scan_to_bluetooth() {
       wx.scanCode({
         onlyFromCamera: true,
-        scanType:['barCode', 'qrCode'],
+        scanType: ['barCode', 'qrCode'],
         success: (res) => {
           let $data = res;
           if ($data.result) {
@@ -258,12 +256,12 @@ export default {
             let $code = ""
 
             //二维码
-            if (res.scanType=="QR_CODE"){
+            if (res.scanType == "QR_CODE") {
               $code = url.match(/\?ring=(.*)/)
-              ? url.match(/\?ring=(.*)/)
-              : url.match(/\?code=(.*)/);
+                ? url.match(/\?ring=(.*)/)
+                : url.match(/\?code=(.*)/);
               $code = $code[1].toUpperCase();
-            }else{
+            } else {
               //一维码
               $code = res.result.toUpperCase();
             }
@@ -352,7 +350,7 @@ export default {
             });
           }
         },
-        fail(res){
+        fail(res) {
           console.log(res)
         }
       });
@@ -415,21 +413,21 @@ export default {
       if ($status == 0) {
         game_store.setters.setGameStatus(0);
         // 清空链接得设备 三值
-        game_store.setters.clearDeviceToy();
         $this.connect_toy = $status;
         $this.connect_show = false;
         $this.device_bg = false;
 
         $this.$bluetooth.SendOrder("09")
-
+        let deviceId = game_store.getters.getDeviceId();
         $this.change_toy_connect_status(0);
         //断开蓝牙连接
         wx.closeBLEConnection({
-          deviceId: $this.device.deviceId,
+          deviceId: deviceId,
           success(res) {
             Toast.success({
               message: "已成功断开",
             });
+            game_store.setters.clearDeviceToy();
             wx.closeBluetoothAdapter();
           },
           fail(res) {
@@ -449,38 +447,32 @@ export default {
     //开始蓝牙被发现
     startBluetoothDevicesDiscovery() {
       wx.startBluetoothDevicesDiscovery({
-        services: ["0000FFF0-0000-1000-8000-00805F9B34FB", "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"],
         allowDuplicatesKey: true,
         success: (res) => {
           //2021年10月21日15:07:57  通过sn 返回deviceId
           getDeviceBySn($this.code).then((res) => {
             let $data = res.data;
-            console.log($data)
+            console.log($data.code)
             $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;
-                  } else {
-                    $this.device.deviceId = $data.device_id;
-                  }
-                  if ($this.device.deviceId) {
+            $data = $data.data;
+            wx.getSystemInfo({
+              success(res) {
+                // 判断ios 和 安卓
+                if (res.platform == "ios") {
+                  $this.onBluetoothDeviceFound();
+                } else {
+                  if ($data.device_id) {
                     console.log("finded")
                     $this.device_finded = true
-                    $this.createBLEConnection()
+                    $this.createBLEConnection($data.device_id)
                   } else {
                     $this.device_finded = false
                     console.log('unfinded')
                     $this.onBluetoothDeviceFound();
                   }
                 }
-              });
-            } else {
-              $this.onBluetoothDeviceFound();
-            }
+              }
+            });
           })
 
         },
@@ -515,8 +507,8 @@ export default {
             }
             if (device["name"].toUpperCase() == $this.code) {
               $this.stopBluetoothDevicesDiscovery();
-              $this.device = device;
-              $this.createBLEConnection();
+              console.log("搜索设备", device)
+              $this.createBLEConnection(device.deviceId);
             }
           });
         });
@@ -529,33 +521,33 @@ export default {
       wx.stopBluetoothDevicesDiscovery();
     },
     //连接低功耗蓝牙设备。
-    createBLEConnection() {
+    createBLEConnection(deviceId) {
       wx.createBLEConnection({
-        deviceId: $this.device.deviceId,
+        deviceId: deviceId,
         success: (res) => {
           console.log("成功连接");
-          game_store.setters.setDeviceId($this.device.deviceId);
+          game_store.setters.setDeviceId(deviceId);
 
           //成功连接脑环蓝牙
           $this.change_device_status(2);
 
-          let $system = wx.getSystemInfoSync()
-          if ($system.platform == 'ios') {
-            $this.$bluetooth.getBLEDeviceServices($this.device.deviceId)
-            $this.$bluetooth.watchingDevice($this)
-          } else {
-            $this.$bluetooth.openNotify($this)
-            $this.$bluetooth.watch_bluetooth_status($this);
-            //录入库
-            //读取信息逐个写入
-            if (!$this.device_finded) {
-              setTimeout(() => {
-                $this.$bluetooth.SendOrder("02")
-                setDeviceInDb($this, $this.device.deviceId)
-              }, 3000)
+          $this.$bluetooth.getBLEDeviceServices(deviceId)
+          let $checkServices = setInterval(() => {
+            let $serviceId = game_store.getters.getServiceId();
+            if ($serviceId) {
+              clearInterval($checkServices)
+              $this.$bluetooth.openNotify($this)
+              $this.$bluetooth.watchingDevice($this)
+              $this.$bluetooth.watch_bluetooth_status($this);
+              //录入库
+              //读取信息逐个写入
+              if (!$this.device_finded) {
+                setTimeout(() => {
+                  $this.$bluetooth.SendOrder("02")
+                }, 3000)
+              }
             }
-          }
-
+          }, 1500)
 
         },
         fail(err) {
@@ -624,7 +616,6 @@ export default {
         $this.connect_show = false;
         $this.device_status = 0;
         $this.$bluetooth.watch_bluetooth_status($this);
-        $this.device.deviceId = "";
         $this.toy_UUID = "";
         $this.$forceUpdate();
         // 清空链接的设备

+ 0 - 1
src/components/device/toy/connecting.vue

@@ -291,7 +291,6 @@ export default {
   name: "connected",
   props: [
     "connect_toy",
-    "deviceId",
     "toy_id",
     "toy",
     "device_bg",

+ 7 - 7
src/store/game.js

@@ -2,7 +2,7 @@ const setters = {
   clearDeviceToy() {
     wx.removeStorageSync('deviceId')
     wx.removeStorageSync('deviceSn')
-    // wx.removeStorageSync('serviceId')
+    wx.removeStorageSync('serviceId')
     // wx.removeStorageSync('characteristicId')
   },
   //setter模块
@@ -12,9 +12,9 @@ const setters = {
   setDeviceSn(deviceSn) {
     wx.setStorageSync('deviceSn', deviceSn)
   },
-  // setServiceId(serviceId) {
-  //   wx.setStorageSync('serviceId', serviceId)
-  // },
+  setServiceId(serviceId) {
+    wx.setStorageSync('serviceId', serviceId)
+  },
   // setCid(characteristicId) {
   //   wx.setStorageSync('characteristicId', characteristicId)
   // },
@@ -73,9 +73,9 @@ const getters = {
   getDeviceSn() {
     return wx.getStorageSync('deviceSn')
   },
-  // getServiceId() {
-  //   return wx.getStorageSync('serviceId')
-  // },
+  getServiceId() {
+    return wx.getStorageSync('serviceId')
+  },
   // getCharacterId() {
   //   return wx.getStorageSync('characteristicId')
   // },

+ 8 - 4
src/utils/bluetooth.js

@@ -40,9 +40,11 @@ var charateristics = [
 
 function getServicesAndCharateristc() {
   //获取当前设备名称
-  let $brainSn = game_store.getters.getDeviceSn();
+  // let $brainSn = game_store.getters.getDeviceSn();
   let $deviceId = game_store.getters.getDeviceId();
-  let $index = $brainSn.toUpperCase().indexOf("JELLYFISH") != -1 ? 0 : 1;
+  let $serviceId = game_store.getters.getServiceId();
+  let $index = services.indexOf($serviceId.toUpperCase())
+  // let $index = $brainSn.toUpperCase().indexOf("JELLYFISH") != -1 ? 0 : 1;
   return {
     deviceId: $deviceId,
     service: services[$index],
@@ -307,12 +309,13 @@ export default {
           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);
+            game_store.setters.setServiceId(res.services[i].uuid)
             return;
           }
         }
       },
       fail(res) {
-        console.log("连接蓝牙成功,获取服务失败");
+        console.log("连接蓝牙成功,获取服务失败",res);
       },
     });
   },
@@ -383,6 +386,7 @@ export default {
   // 监听脑环数据
   watchingDevice($this) {
     const that = this;
+    let DeviceId = game_store.getters.getDeviceId();
     wx.onBLECharacteristicValueChange((characteristic) => {
       let hexStr = that.ab2hex(characteristic.value);
 
@@ -531,7 +535,7 @@ export default {
         let $version_data = $_data.substr(0, 2).split('')
         $this.device_data.hardware_version = `${$version_data[0]}.${$version_data[1]}`
 
-        setDeviceInDb($this, $this.device.deviceId)
+        setDeviceInDb($this, DeviceId)
       }
 
       if (hexStr.toUpperCase().indexOf("AADD09") != -1) {