|
@@ -34,9 +34,12 @@
|
|
|
<!-- 玩具模块-->
|
|
|
<!-- 玩具连接中-->
|
|
|
<div class="connecting_toy">
|
|
|
- <toy_connecting :connect_toy="connect_toy" @open_choose_toy="open_choose_toy"
|
|
|
+ <toy_connecting :connect_toy="connect_toy"
|
|
|
+ :deviceId="_deviceId"
|
|
|
+ @open_choose_toy="open_choose_toy"
|
|
|
@change_toy_connect_status="change_toy_connect_status"
|
|
|
@change_status="change_device_status"
|
|
|
+ @game_start="game_start"
|
|
|
></toy_connecting>
|
|
|
</div>
|
|
|
</div>
|
|
@@ -87,9 +90,10 @@ import device_connected from '@/components/device/connected';
|
|
|
//连接玩具
|
|
|
import toy_connecting from '@/components/device/toy/connecting'
|
|
|
//获取个人信息
|
|
|
-import {userDetail} from "../../requests/user";
|
|
|
import Toast from '../../../static/vant/toast/toast';
|
|
|
import {game_devices} from "../../requests/game";
|
|
|
+import game_store from '@/store/game'
|
|
|
+import bluetooth from "@/utils/bluetooth";
|
|
|
|
|
|
|
|
|
let $this;
|
|
@@ -288,6 +292,8 @@ export default {
|
|
|
if (device['name'] == $this.code) {
|
|
|
$this.stopBluetoothDevicesDiscovery()
|
|
|
$this.device = device
|
|
|
+ game_store.commit('setDeviceId', device.deviceId)
|
|
|
+ $this._deviceId = device.deviceId
|
|
|
$this.createBLEConnection()
|
|
|
}
|
|
|
})
|
|
@@ -314,396 +320,28 @@ export default {
|
|
|
console.log('成功连接')
|
|
|
//成功连接脑环蓝牙
|
|
|
$this.change_device_status(2)
|
|
|
- $this.getBLEDeviceServices($this.device.deviceId)
|
|
|
+ // $this.getBLEDeviceServices($this.device.deviceId)
|
|
|
},
|
|
|
fail(err) {
|
|
|
console.log('err', err)
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
- //获取蓝牙设备服务
|
|
|
- getBLEDeviceServices(deviceId) {
|
|
|
- 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) {
|
|
|
- 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.canWrite = true
|
|
|
- $this._deviceId = deviceId
|
|
|
- $this._serviceId = serviceId
|
|
|
- $this._characteristicId = item.uuid
|
|
|
- $this.sendPause()
|
|
|
- }
|
|
|
- if (item.properties.notify || item.properties.indicate) {
|
|
|
- wx.notifyBLECharacteristicValueChange({
|
|
|
- deviceId,
|
|
|
- serviceId,
|
|
|
- characteristicId: item.uuid,
|
|
|
- state: true,
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- fail(res) {
|
|
|
- console.error('getBLEDeviceCharacteristics', res)
|
|
|
- }
|
|
|
- })
|
|
|
- // 操作之前先监听,保证第一时间获取数据
|
|
|
- wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
- console.log("收到" + $this.ab2hex(characteristic.value));
|
|
|
- let hexStr = $this.ab2hex(characteristic.value)
|
|
|
-
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0000') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '未找到教具',
|
|
|
- icon: 'none',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- console.log("无连接设备")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0001') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到水柱音箱',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
- console.log("水柱音箱")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0002') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到喷雾恐龙(大)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("喷雾恐龙(大)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0003') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到喷雾恐龙(小)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("喷雾恐龙(小)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0004') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到轨道车',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("轨道车")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0005') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到碰碰车',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("碰碰车")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0006') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到小车(大)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("小车(大)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0007') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到小车(中)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("小车(中)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0008') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到小车(小)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("小车(小)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a0009') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到飞行器(大)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("飞行器(大)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a000a') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到飞行器(小)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("飞行器(小)")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd0a000b') {
|
|
|
- wx.hideLoading()
|
|
|
- wx.showToast({
|
|
|
- title: '已连接到水母灯)',
|
|
|
- icon: 'success',
|
|
|
- duration: 1500
|
|
|
- })
|
|
|
- $this.sendControl()
|
|
|
- $this.connect_toy = 2
|
|
|
-
|
|
|
- console.log("水母灯")
|
|
|
- }
|
|
|
- if (hexStr.substring(0, 10) == 'aadd070000') {
|
|
|
- $this.sendOpen()
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
- //面向蓝牙设备成员发送数据包
|
|
|
- writeBLECharacteristicValue() {
|
|
|
- // 向蓝牙设备发送一个0x00的16进制数据
|
|
|
- let buffer = new ArrayBuffer(1)
|
|
|
- let dataView = new DataView(buffer)
|
|
|
- dataView.setUint8(0, Math.random() * 255 | 0)
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: this._deviceId,
|
|
|
- serviceId: this._deviceId,
|
|
|
- characteristicId: this._characteristicId,
|
|
|
- value: buffer,
|
|
|
- })
|
|
|
- },
|
|
|
- // 发送停止发送脑电数据包指令
|
|
|
- sendPause() {
|
|
|
- let that = this
|
|
|
- // 向蓝牙设备发送一个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, '0x00')
|
|
|
- dataView.setUint8(7, '0xfc')
|
|
|
- // 向低功耗蓝牙设备特征值中写入二进制数据
|
|
|
- console.log(buffer)
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: that._deviceId,
|
|
|
- serviceId: that._serviceId,
|
|
|
- characteristicId: that._characteristicId,
|
|
|
- value: buffer,
|
|
|
- success: function (res) {
|
|
|
- console.log('停止数据发送成功')
|
|
|
- console.log(res)
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('停止数据发送失败')
|
|
|
- console.log(err)
|
|
|
- }
|
|
|
- })
|
|
|
- },
|
|
|
- // 发送开启脑电数据包指令
|
|
|
- sendOpen() {
|
|
|
- const that = this
|
|
|
- // 向蓝牙设备发送一个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, '0xff')
|
|
|
- dataView.setUint8(7, '0xfd')
|
|
|
- // 向低功耗蓝牙设备特征值中写入二进制数据
|
|
|
- console.log(buffer)
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: that._deviceId,
|
|
|
- serviceId: that._serviceId,
|
|
|
- characteristicId: that._characteristicId,
|
|
|
- value: buffer,
|
|
|
- success: function (res) {
|
|
|
- console.log('开启脑电数据包成功')
|
|
|
- console.log(res)
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('开启脑电数据包失败')
|
|
|
- console.log(err)
|
|
|
- }
|
|
|
-
|
|
|
- })
|
|
|
- },
|
|
|
- // 发送开启教具脑控指令
|
|
|
- sendControl() {
|
|
|
- const that = this
|
|
|
- // 向蓝牙设备发送一个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, '0x07')
|
|
|
- dataView.setUint8(7, '0xf5')
|
|
|
- // 向低功耗蓝牙设备特征值中写入二进制数据
|
|
|
- console.log(buffer)
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: that._deviceId,
|
|
|
- serviceId: that._serviceId,
|
|
|
- characteristicId: that._characteristicId,
|
|
|
- value: buffer,
|
|
|
- success: function (res) {
|
|
|
- console.log('开启教具脑控成功')
|
|
|
- // that.sendOpen()
|
|
|
- console.log(res)
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('开启教具脑控失败')
|
|
|
- console.log(err)
|
|
|
- }
|
|
|
-
|
|
|
- })
|
|
|
- },
|
|
|
- // 发送连接玩具指令
|
|
|
- sendConnect(index) {
|
|
|
- const that = this
|
|
|
- // 玩具对应指令数组
|
|
|
- console.log(index)
|
|
|
- let arr = ['0x01', '0x02', '0x03', '0x04', '0x05', '0x06', '0x07', '0x08', '0x09', '0x0a', '0x0b']
|
|
|
- let arr1 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b]
|
|
|
- let CheckSum = ((0x03 + 0x00 + arr1[index] + 0x00 + 0x0a) ^ 0xFFFFFFFF) & 0xFF
|
|
|
- // 向蓝牙设备发送指令
|
|
|
- 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, arr[index])
|
|
|
- dataView.setUint8(5, '0x00')
|
|
|
- dataView.setUint8(6, '0x0a')
|
|
|
- dataView.setUint8(7, '0xf1')
|
|
|
- // 向低功耗蓝牙设备特征值中写入二进制数据
|
|
|
- console.log(buffer)
|
|
|
- wx.writeBLECharacteristicValue({
|
|
|
- deviceId: that._deviceId,
|
|
|
- serviceId: that._serviceId,
|
|
|
- characteristicId: that._characteristicId,
|
|
|
- value: buffer,
|
|
|
- success: function (res) {
|
|
|
- console.log('发送连接玩具指令成功')
|
|
|
- wx.showLoading({
|
|
|
- title: '正在创建连接',
|
|
|
- })
|
|
|
- console.log(res)
|
|
|
- },
|
|
|
- fail: function (err) {
|
|
|
- console.log('发送连接玩具指令失败')
|
|
|
- wx.showModal({
|
|
|
- showCancel: false,
|
|
|
- title: '提示',
|
|
|
- content: '指令发送失败',
|
|
|
- })
|
|
|
- console.log(err)
|
|
|
- }
|
|
|
-
|
|
|
- })
|
|
|
- },
|
|
|
- inArray(arr, key, val) {
|
|
|
- for (let i = 0; i < arr.length; i++) {
|
|
|
- if (arr[i][key] === val) {
|
|
|
- return i;
|
|
|
- }
|
|
|
- }
|
|
|
- return -1;
|
|
|
- },
|
|
|
- // ArrayBuffer转16进度字符串示例
|
|
|
- ab2hex(buffer) {
|
|
|
- var hexArr = Array.prototype.map.call(
|
|
|
- new Uint8Array(buffer),
|
|
|
- function (bit) {
|
|
|
- return ('00' + bit.toString(16)).slice(-2)
|
|
|
- }
|
|
|
- )
|
|
|
- return hexArr.join('');
|
|
|
- },
|
|
|
-
|
|
|
// 获取游戏设备玩具
|
|
|
- get_toy_list(){
|
|
|
- game_devices().then((res)=>{
|
|
|
+ get_toy_list() {
|
|
|
+ game_devices().then((res) => {
|
|
|
let $data = res.data
|
|
|
})
|
|
|
- }
|
|
|
+ },
|
|
|
|
|
|
+ // 开始游戏的方法
|
|
|
+ game_start() {
|
|
|
+ game_store.commit('setToyIndex', $this.toy_action - 1)
|
|
|
+ // bluetooth.sendConnect($this.toy_action - 1, $this._deviceId, $this._serviceId, $this._characteristicId)
|
|
|
+ console.log('device.vue', $this._deviceId)
|
|
|
+ }
|
|
|
},
|
|
|
- mounted(){
|
|
|
+ mounted() {
|
|
|
$this.get_toy_list()
|
|
|
},
|
|
|
created() {
|