|
@@ -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)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
}
|