123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655 |
- 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)
- }
- })
- }
- }
- )
- }
- }
|