소스 검색

修改蓝牙连接,尝试获得玩具连接状态,失败

yerong 4 년 전
부모
커밋
caaf7ee5fe
4개의 변경된 파일111개의 추가작업 그리고 5개의 파일을 삭제
  1. 1 1
      dist/wx
  2. 10 0
      src/components/device/device.vue
  3. 1 0
      src/pages/user_center/recharge/index.vue
  4. 99 4
      src/utils/bluetooth.js

+ 1 - 1
dist/wx

@@ -1 +1 @@
-Subproject commit 7d7a14a079317519a9749acd98ca32babcfe0d38
+Subproject commit 1fbb004619cfa489833e1c5b2b779420d652d9af

+ 10 - 0
src/components/device/device.vue

@@ -209,10 +209,20 @@ export default {
       $this.on_close();
       $this.change_toy_connect_status(1);
       $this._device_index = $this.toy_action - 1;
+      //获取玩具
       let $toy = $this.toy_list[$this.toy_action - 1];
       let $hex = $toy["hex"].substr($toy["hex"].length - 2, 2);
       let toy_hex = "0x" + $hex;
       game_store.commit("setToyHex", toy_hex);
+
+      //连接玩具
+      bluetooth.getBLEDeviceServices($this._deviceId)
+      setTimeout(() => {
+        console.log($this._deviceId, bluetooth.serviceId, bluetooth.cid)
+        bluetooth.getConnectedToy($this._deviceId, bluetooth.serviceId, bluetooth.cid)
+      }, 1000)
+
+
       //判断是否游戏中
       let $game_status = game_store.getters.getGameStatus;
       console.log("choose_ok_game_status", $game_status);

+ 1 - 0
src/pages/user_center/recharge/index.vue

@@ -375,6 +375,7 @@ export default {
   },
   onUnload() {
     $this.close_success_pop()
+    $this.close_err_pop()
   }
 }
 </script>

+ 99 - 4
src/utils/bluetooth.js

@@ -1,4 +1,5 @@
-var $this = this
+import game_store from "../store/game";
+
 export default {
   test_name: 'hello world',
   deviceId: "",
@@ -111,7 +112,6 @@ export default {
         wx.showLoading({
           title: '正在创建连接',
         })
-        console.log(res)
       },
       fail: function (err) {
         console.log('发送连接玩具指令失败')
@@ -165,8 +165,6 @@ export default {
   // 发送停止发送脑电数据包指令
   sendPause(deviceId, serviceId, Cid) {
     let that = this
-
-
     // 玩具对应指令数组
     let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x08) ^ 0xFFFFFFFF) & 0xFF
     let $CheckSum = "0x" + CheckSum.toString(16)
@@ -391,5 +389,102 @@ export default {
         $that.connect_toy = 0
       }
     })
+  },
+
+  //获取蓝牙设备服务
+  getBLEDeviceServices(deviceId) {
+    const $this = this
+
+    wx.getBLEDeviceServices({
+      deviceId,
+      success: (res) => {
+        for (let i = 0; i < res.services.length; i++) {
+          if (res.services[i].isPrimary) {
+            $this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
+            return
+          }
+        }
+      }
+    })
+  },
+
+  //获取蓝牙设备某个服务中所有特征值
+  getBLEDeviceCharacteristics(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.commit('setDeviceId', deviceId)
+            game_store.commit('setServiceId', serviceId)
+            game_store.commit('setCid', item.uuid)
+            $this.sendPause(deviceId, serviceId, item.uuid)
+            //获取玩具的值
+            // bluetooth.sendConnect($this.toy_hex, deviceId, serviceId, item.uuid)
+          }
+
+          if (item.properties.notify || item.properties.indicate) {
+            wx.notifyBLECharacteristicValueChange({
+              deviceId,
+              serviceId,
+              characteristicId: item.uuid,
+              state: true,
+            })
+          }
+        }
+      },
+      fail(res) {
+        console.error('getBLEDeviceCharacteristics', res)
+      }
+    })
+  },
+
+//  获取当前连接得玩具
+  getConnectedToy(deviceId, serviceId, Cid) {
+    let that = this
+    // 玩具对应指令数组
+    let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x06) ^ 0xFFFFFFFF) & 0xFF
+    let $CheckSum = "0x" + CheckSum.toString(16)
+    // 向蓝牙设备发送一个0x00的16进制数据
+    let buffer = new ArrayBuffer(8)
+    let dataView = new DataView(buffer)
+    dataView.setUint8(0, '0xaa')
+    dataView.setUint8(1, '0xcc')
+    dataView.setUint8(2, '0x03')
+    dataView.setUint8(3, '0x00')
+    dataView.setUint8(4, '0x00')
+    dataView.setUint8(5, '0x00')
+    dataView.setUint8(6, '0x06')
+    dataView.setUint8(7, $CheckSum)
+    // 向低功耗蓝牙设备特征值中写入二进制数据
+    console.log(buffer)
+    wx.writeBLECharacteristicValue({
+      deviceId: deviceId,
+      serviceId: serviceId,
+      characteristicId: Cid,
+      value: buffer,
+      success: function (res) {
+        console.log('获取当前连接的玩具类型')
+        console.log(res)
+      },
+      fail: function (err) {
+        console.log('获取当前连接的玩具类型失败')
+        console.log(err)
+      }
+    })
   }
 }