export default { // 发送开启脑电数据包指令 sendOpen(deviceId, serviceId, Cid) { // 向蓝牙设备发送一个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: 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) { 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: 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(index, deviceId, serviceId, Cid) { 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 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: deviceId, serviceId: serviceId, characteristicId: Cid, 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) } }) }, //发送断开玩具的指令 sendEnd(deviceId, serviceId, Cid) { //AA CC 03 00 00 00 09 F3 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, '0xf3') // 向低功耗蓝牙设备特征值中写入二进制数据 console.log(buffer) 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 // 向蓝牙设备发送一个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: 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) { 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: "已连接到小车(大)", 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: true }, ] $toys.forEach(($val, $index) => { if (hexStr.substring(0, 10) == $val['hex']) { if ($val['flag']) { wx.hideLoading() wx.showToast({ title: $val['title'], icon: 'success', duration: 1500 }) console.log($val['title']) } else { wx.hideLoading() wx.showToast({ title: $val['title'], icon: 'nones', duration: 1500 }) } return $val['flag'] } }) }, // 获取大包数据 进行绘制图表 get_big_data(hex) { //当s1为 00时 数据有效 let $s1 = hex.substr(8, 2) //专注度数据 let $att = parseInt("0x" + hex.substr(12, 2)) console.log('att', $att) //放松度数据 let $med = parseInt("0x" + hex.substr(16, 2)) console.log('$med', $med) //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 = "0x" + $delta_1 << 16 | "0x" + $delta_2 << 8 | "0x" + $delta_3 console.log('Delta', parseInt($delta, 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 = "0x" + $theta_1 << 16 | "0x" + $theta_2 << 8 | "0x" + $theta_3 console.log('Theta', parseInt($theta, 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 = ($high_alpha << 8) | $low_alpha console.log('Alpha', parseInt($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 = ($high_beta << 8) | $low_beta console.log('Beta', parseInt($beta, 16)) return { att: $att, med: $med, delta: $delta, theta: $theta, alpha: $alpha, beta: $beta } } }