import game_store from "../store/game"; import ble_store from "../store/bluetooth" export default { test_name: 'hello world', deviceId: "", serviceId: "", cid: "", // 发送开启脑电数据包指令 sendOpen(deviceId, serviceId, Cid) { // 向蓝牙设备发送一个0x00的16进制数据 // 玩具对应指令数组 let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0xff) ^ 0xFFFFFFFF) & 0xFF let $CheckSum = "0x" + CheckSum.toString(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, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 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) } }) }, // 发送开启教具脑控指令 sendControl(deviceId, serviceId, Cid) { console.log("链接玩具,3参数:", deviceId, serviceId, Cid) const that = this // 玩具对应指令数组 let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x07) ^ 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, '0x07') dataView.setUint8(7, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: Cid, value: buffer, success: function (res) { console.log('开启教具脑控成功') // that.sendOpen() console.log(res) }, fail: function (err) { console.log('开启教具脑控失败') console.log(err) } }) }, // 发送连接玩具指令 sendConnect($hex, deviceId, serviceId, Cid, Toast) { //获取最后两个字节 const that = this // 玩具对应指令数组 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 $index = arr.indexOf($hex) let CheckSum = ((0x03 + 0x00 + arr1[$index] + 0x00 + 0x0a) ^ 0xFFFFFFFF) & 0xFF let $CheckSum = "0x" + CheckSum.toString(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, arr[$index]) dataView.setUint8(5, '0x00') dataView.setUint8(6, '0x0a') dataView.setUint8(7, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: Cid, value: buffer, success: function (res) { console.log('发送连接玩具指令成功') // wx.showLoading({ // title: '正在创建连接', // }) let $game_status = game_store.getters.getGameStatus(); if ($game_status == 1) { Toast.loading({ duration: 0, message: "正在创建连接", forbidClick: true }) } }, fail: function (err) { console.log('发送连接玩具指令失败') Toast.fail({ message: "玩具连接失败" }) console.log(err) } }) }, //发送断开玩具的指令 sendEnd(deviceId, serviceId, Cid) { //AA CC 03 00 00 00 09 F3 // 玩具对应指令数组 let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x09) ^ 0xFFFFFFFF) & 0xFF let $CheckSum = "0x" + CheckSum.toString(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, '0x09') dataView.setUint8(7, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: Cid, value: buffer, success: function (res) { console.log('断开玩具成功') // that.sendOpen() console.log(res) }, fail: function (err) { console.log('断开玩具失败') console.log(err) } }) }, // 发送停止发送脑电数据包指令 sendPause(deviceId, serviceId, Cid) { let that = this // 玩具对应指令数组 let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x08) ^ 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, '0x00') dataView.setUint8(7, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 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) } }) }, // 通过字节码获取玩具 connect_toy(hexStr, deviceId, serviceId, Cid, Toast) { console.log('开始连接玩具') let $flag = "" let $toys = [ { id: 1, hex: 'aadd0a0000', msg: "无连接设备", flag: false }, { id: 1, hex: 'aadd0a0001', msg: "已连接到智脑水舞", flag: true }, { id: 2, hex: 'aadd0a0002', msg: "已连接到智脑恐龙", flag: true }, { id: 3, hex: 'aadd0a0003', msg: "已连接到喷雾恐龙(小)", flag: true }, { id: 4, hex: 'aadd0a0004', msg: "已连接到智脑赛车", flag: true }, { id: 5, hex: 'aadd0a0005', msg: "已连接到智脑碰碰车", flag: true }, { id: 6, hex: 'aadd0a0006', msg: "已连接到智脑SUV赛车", flag: true }, { id: 7, hex: 'aadd0a0007', msg: "已连接到小车(中)", flag: true }, { id: 8, hex: 'aadd0a0008', msg: "已连接到小车(小)", flag: true }, { id: 9, hex: 'aadd0a0009', msg: '已连接到智脑无人机', flag: true }, { id: 10, hex: 'aadd0a000a', msg: "已连接到飞行器(小)", flag: true }, { id: 11, hex: 'aadd0a000b', msg: "已连接到水母灯", flag: true }, { id: 12, hex: 'aadd070000', msg: "无连接设备", flag: false }, ] $toys.forEach(($val, $index) => { if (hexStr.substring(0, 10) == $val['hex']) { Toast.clear() if ($val['flag']) { Toast.success($val['msg']) let $game_status = game_store.getters.getGameStatus(); if ($game_status == 1) { this.sendControl(deviceId, serviceId, Cid) } } else { if ($val['hex'] == 'aadd070000') { this.sendOpen(deviceId, serviceId, Cid) } else { Toast.fail($val['msg']) } } $flag = $val['flag'] } }) return $flag }, // 获取大包数据 进行绘制图表 get_big_data(hex) { if (hex.substr(0, 6) != '555520') { return false } //当s1为 00时 数据有效 let $s1 = hex.substr(8, 2) if ($s1 != '00') { return false } //专注度数据 let $att = parseInt("0x" + hex.substr(12, 2)) //放松度数据 let $med = parseInt("0x" + hex.substr(16, 2)) //Delta数据 let $delta_1 = hex.substr(hex.indexOf('0418') + 4, 2) let $delta_2 = hex.substr(hex.indexOf('0418') + 6, 2) let $delta_3 = hex.substr(hex.indexOf('0418') + 8, 2) let $delta = parseInt("0x" + $delta_1 << 16 | "0x" + $delta_2 << 8 | "0x" + $delta_3, 16) //Theta数据 let $theta_1 = hex.substr(hex.indexOf('0418') + 10, 2) let $theta_2 = hex.substr(hex.indexOf('0418') + 12, 2) let $theta_3 = hex.substr(hex.indexOf('0418') + 14, 2) let $theta = parseInt("0x" + $theta_1 << 16 | "0x" + $theta_2 << 8 | "0x" + $theta_3, 16) //low_Alpha let $low_alpha_1 = hex.substr(hex.indexOf('0418') + 16, 2) let $low_alpha_2 = hex.substr(hex.indexOf('0418') + 18, 2) let $low_alpha_3 = hex.substr(hex.indexOf('0418') + 20, 2) let $low_alpha = "0x" + $low_alpha_1 << 16 | "0x" + $low_alpha_2 << 8 | "0x" + $low_alpha_3 //high_Alpha let $high_alpha_1 = hex.substr(hex.indexOf('0418') + 22, 2) let $high_alpha_2 = hex.substr(hex.indexOf('0418') + 24, 2) let $high_alpha_3 = hex.substr(hex.indexOf('0418') + 26, 2) let $high_alpha = "0x" + $high_alpha_1 << 16 | "0x" + $high_alpha_2 << 8 | "0x" + $high_alpha_3 let $alpha = parseInt(($high_alpha << 8) | $low_alpha, 16) //low_beta let $low_beta_1 = hex.substr(hex.indexOf('0418') + 28, 2) let $low_beta_2 = hex.substr(hex.indexOf('0418') + 30, 2) let $low_beta_3 = hex.substr(hex.indexOf('0418') + 32, 2) let $low_beta = "0x" + $low_beta_1 << 16 | "0x" + $low_beta_2 << 8 | "0x" + $low_beta_3 //high_beta let $high_beta_1 = hex.substr(hex.indexOf('0418') + 34, 2) let $high_beta_2 = hex.substr(hex.indexOf('0418') + 36, 2) let $high_beta_3 = hex.substr(hex.indexOf('0418') + 38, 2) let $high_beta = "0x" + $low_beta_1 << 16 | "0x" + $low_beta_2 << 8 | "0x" + $low_beta_3 //Beta数据 let $beta = parseInt(($high_beta << 8) | $low_beta, 16) return { att: $att, med: $med, delta: $delta, theta: $theta, alpha: $alpha, beta: $beta } }, //获取设备电量 get_device_elc(hex) { if (hex.substr(0, 8) != '55550203') { return false } let $power = parseInt("0x" + hex.substr(8, 2)) return $power }, // 监听蓝牙连接状态 watch_bluetooth_status($that) { let that = this wx.onBLEConnectionStateChange((res) => { // 该方法回调中可以用于处理连接意外断开等异常情况 ble_store.setters.setBluetoothLinkStatus(res.connected) if (res.connected == false) { //断开玩具连接 try { $that.change_toy_connect_status(0) //断开蓝牙连接 $that.change_device_status(0) } catch (e) { console.log('方法不存在') } //判断游戏是否游戏中 let $game_status = game_store.getters.getGameStatus(); if ($game_status == 1) { $that.game_finished() } } }) }, //获取蓝牙设备服务 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 } } }, fail(res) { console.log('连接蓝牙成功,获取服务失败') } }) }, //获取蓝牙设备某个服务中所有特征值 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.setters.setDeviceId(deviceId) game_store.setters.setServiceId(serviceId) game_store.setters.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) } }) }, //监听玩具得状态 watching_toy($this, deviceId, serviceId, Cid, Toast) { Toast.clear() Toast.loading({ duration: 0, forbidClick: false, message: "检测连接中" }) let that = this let $count = 0 setTimeout(() => { Toast.clear() }, 10000) //关闭核销选项窗口 // $this.pay_window = false wx.onBLECharacteristicValueChange((characteristic) => { try { console.log("收到" + that.ab2hex(characteristic.value)); let hexStr = that.ab2hex(characteristic.value) //连接玩具 let $flag = that.connect_toy(hexStr, deviceId, serviceId, Cid, Toast) //连接不上设备 if ($flag) { Toast.clear() //连接成功 打开核销窗口 // $this.pay_window = true $this.change_toy_connect_status(2) let $game_status = game_store.getters.getGameStatus(); if ($game_status == 1) { $this.change_toy_connect_status(4) } wx.offBLECharacteristicValueChange() that.watchingDevice($this) } else { $count++ if ($count == 15) { Toast.clear() $this.change_toy_connect_status(3) wx.offBLECharacteristicValueChange() that.watchingDevice($this) } } // 取消监听低功耗蓝牙设备 } catch (e) { wx.offBLECharacteristicValueChange() that.watchingDevice($this) } }) }, // 获取当前连接得玩具 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) // 向低功耗蓝牙设备特征值中写入二进制数据 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) } }) }, // 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(''); }, //断开发送数据 shutdownSendControl(deviceId, serviceId, Cid) { let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x0a) ^ 0xFFFFFFFF) & 0xFF let $CheckSum = "0x" + CheckSum.toString(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, '0x0a') dataView.setUint8(7, $CheckSum) // 向低功耗蓝牙设备特征值中写入二进制数据 wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: Cid, value: buffer, success: function (res) { console.log('断开连接玩具指令成功') // wx.showLoading({ // title: '正在创建连接', // }) let $game_status = game_store.getters.getGameStatus(); if ($game_status == 1) { Toast.loading({ duration: 0, message: "正在创建连接", forbidClick: true }) } }, fail: function (err) { console.log('发送连接玩具指令失败') // wx.showModal({ // showCancel: false, // title: '提示', // content: '指令发送失败', // }) console.log(err) } }) }, // 监听脑环是否带正 watchingDevice($this) { const that = this wx.onBLECharacteristicValueChange( (characteristic) => { let hexStr = that.ab2hex(characteristic.value) if (hexStr.substr(0, 6) == '555520') { //当s1为 00时 数据有效 let $s1 = hexStr.substr(8, 2) console.log("监听脑环是否带正:", $s1 == '00') $this.device_bg = ($s1 == '00') } // 监听脑环电量 let $power = that.get_device_elc(hexStr) if ($power) { console.log("当前脑环电量:", $power) $this.device_power = $power } if ($power < 20 && $power > 0) { wx.showToast({ title: "脑环电量不足", icon: 'none', success() { $this.change_device_status(0) } }) } } ) } }