|
@@ -1,27 +1,66 @@
|
|
|
import game_store from "../store/game";
|
|
|
import ble_store from "../store/bluetooth";
|
|
|
import Notify from "../../static/vant/notify/notify";
|
|
|
+import {setDeviceInDb} from "../requests/game";
|
|
|
|
|
|
var $ff = "ff";
|
|
|
var $8f = "ffffffff"
|
|
|
+var control_close = false
|
|
|
+var connect_toy = true;
|
|
|
+
|
|
|
export default {
|
|
|
|
|
|
- // 发送开启脑电数据包指令
|
|
|
- sendOpen() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 ff");
|
|
|
+ // 打开大包数据
|
|
|
+ sendOpenBigData() {
|
|
|
+ this.SendOrder('ff')
|
|
|
+ },
|
|
|
+ // 关闭大包数据
|
|
|
+ sendCloseBigData() {
|
|
|
+ this.SendOrder('00')
|
|
|
},
|
|
|
- // 发送开启教具脑控指令
|
|
|
+ //获取当前连接的教具类型
|
|
|
+ sendGetToyType() {
|
|
|
+ this.SendOrder('06')
|
|
|
+ },
|
|
|
+ // 开启脑控
|
|
|
sendControl() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 07");
|
|
|
+ this.SendOrder('07')
|
|
|
},
|
|
|
- //发送断开玩具的指令
|
|
|
- sendEnd() {
|
|
|
+ //暂停脑控
|
|
|
+ sendControlPause() {
|
|
|
//AA CC 03 00 00 00 09 F3
|
|
|
- this.WriteBufferInBle("03 00 00 00 09");
|
|
|
+ this.SendOrder('08')
|
|
|
+ },
|
|
|
+ // 关闭脑控
|
|
|
+ sendControlClose() {
|
|
|
+ let that = this
|
|
|
+ let $intv = setInterval(() => {
|
|
|
+ if (!control_close) {
|
|
|
+ that.SendOrder('09')
|
|
|
+ } else {
|
|
|
+ clearInterval($intv)
|
|
|
+ control_close = false
|
|
|
+ }
|
|
|
+ }, 1000)
|
|
|
+ },
|
|
|
+ //发送一对多连接
|
|
|
+ sendConnectOneToMore(id) {
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 00 0A`);
|
|
|
},
|
|
|
- // 发送停止发送脑电数据包指令
|
|
|
- sendPause() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 08");
|
|
|
+ //发送一对一连接
|
|
|
+ sendConnectOneToOne(id) {
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 01 0A`)
|
|
|
+ },
|
|
|
+
|
|
|
+ //获取教具电量
|
|
|
+ sendToyPower() {
|
|
|
+ let that = this;
|
|
|
+ let toy_intv = setInterval(() => {
|
|
|
+ if (connect_toy) {
|
|
|
+ that.SendOrder('8a')
|
|
|
+ connect_toy = false
|
|
|
+ }
|
|
|
+ }, 3000)
|
|
|
},
|
|
|
|
|
|
// 获取大包数据 进行绘制图表
|
|
@@ -76,16 +115,6 @@ export default {
|
|
|
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 = parseInt($high_beta_1 + $high_beta_2 + $high_beta_3, 16);
|
|
|
- console.log(
|
|
|
- "att: " + $att,
|
|
|
- "med: " + $med,
|
|
|
- "delta: " + $delta,
|
|
|
- "theta: " + $theta,
|
|
|
- "low_alpha: " + $low_alpha,
|
|
|
- "high_alpha: " + $high_alpha,
|
|
|
- "low_beta: " + $low_beta,
|
|
|
- "high_beta: " + $high_beta,
|
|
|
- );
|
|
|
let $max_num = 30000;
|
|
|
return {
|
|
|
att: $att,
|
|
@@ -108,22 +137,13 @@ export default {
|
|
|
},
|
|
|
|
|
|
// 监听蓝牙连接状态
|
|
|
- watch_bluetooth_status($that) {
|
|
|
+ watch_bluetooth_status($this) {
|
|
|
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) {
|
|
@@ -132,18 +152,60 @@ export default {
|
|
|
Notify({
|
|
|
type: 'danger',
|
|
|
duration: 0,
|
|
|
- message: '智脑环已断开连接,请重新扫码智脑环'
|
|
|
+ message: '智脑环已断开连接,正在尝试重新连接',
|
|
|
+ onOpened() {
|
|
|
+ that.reconnect(res.deviceId, $this)
|
|
|
+ }
|
|
|
});
|
|
|
// game_store.setters.setGameStatus(0);
|
|
|
// 结束状态更改为1
|
|
|
- game_store.setters.setGameCloseStatus(1);
|
|
|
-
|
|
|
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+ //重新连接蓝牙
|
|
|
+ reconnect($deviceId, $this) {
|
|
|
+ let that = this;
|
|
|
+ let $serviceId = game_store.getters.getServiceId();
|
|
|
+ let $characteristicId = game_store.getters.getReadCharacterId();
|
|
|
+ //重连的次数
|
|
|
+ let $connect_count = 0;
|
|
|
+ let $rec = setInterval(() => {
|
|
|
+ $connect_count += 1;
|
|
|
+ wx.createBLEConnection({
|
|
|
+ deviceId: $deviceId,
|
|
|
+ success(res) {
|
|
|
+ Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
|
|
|
+ wx.notifyBLECharacteristicValueChange({
|
|
|
+ deviceId: $deviceId,
|
|
|
+ serviceId: $serviceId,
|
|
|
+ characteristicId: $characteristicId,
|
|
|
+ state: true,
|
|
|
+ success(res) {
|
|
|
+ console.log("重新连接SUCCESS,log", res)
|
|
|
+ that.watchingDevice($this)
|
|
|
+ }, fail(res) {
|
|
|
+ console.log("重新失败,log", res)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ that.watchingDevice($this)
|
|
|
+ clearInterval($rec)
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
|
|
|
+ game_store.setters.setGameCloseStatus(1);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if ($connect_count == 3) {
|
|
|
+ $this.game_finished();
|
|
|
+ }
|
|
|
+
|
|
|
+ }, 5000)
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
//获取蓝牙设备服务
|
|
|
getBLEDeviceServices(deviceId) {
|
|
|
const $this = this;
|
|
@@ -192,10 +254,7 @@ export default {
|
|
|
game_store.setters.setServiceId(serviceId);
|
|
|
game_store.setters.setCid(item.uuid);
|
|
|
//打开数据帧
|
|
|
- $this.sendOpen();
|
|
|
- // $this.sendPause(deviceId, serviceId, item.uuid)
|
|
|
- //获取玩具的值
|
|
|
- // bluetooth.sendConnect($this.toy_hex, deviceId, serviceId, item.uuid)
|
|
|
+ $this.sendOpenBigData();
|
|
|
}
|
|
|
|
|
|
if (item.properties.notify || item.properties.indicate) {
|
|
@@ -205,6 +264,7 @@ export default {
|
|
|
characteristicId: item.uuid,
|
|
|
state: true,
|
|
|
});
|
|
|
+ game_store.setters.setReadCid(item.uuid);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
@@ -214,16 +274,6 @@ export default {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
-
|
|
|
- // 发送连接玩具指令
|
|
|
- sendConnect($hex) {
|
|
|
- //获取最后两个字节
|
|
|
- this.WriteBufferInBle("03 00 " + $hex + " 00 0a");
|
|
|
- },
|
|
|
- // 获取当前连接得玩具
|
|
|
- getConnectedToy() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 06");
|
|
|
- },
|
|
|
// ArrayBuffer转16进度字符串示例
|
|
|
ab2hex(buffer) {
|
|
|
var hexArr = Array.prototype.map.call(
|
|
@@ -234,45 +284,66 @@ export default {
|
|
|
);
|
|
|
return hexArr.join("");
|
|
|
},
|
|
|
- //断开发送数据
|
|
|
- shutdownSendControl() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 0a");
|
|
|
- },
|
|
|
|
|
|
- // 监听脑环是否带正
|
|
|
+
|
|
|
+ // 监听脑环数据
|
|
|
watchingDevice($this) {
|
|
|
const that = this;
|
|
|
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
let hexStr = that.ab2hex(characteristic.value);
|
|
|
+ // if (hexStr.toUpperCase().indexOf("AADD") != -1) {
|
|
|
console.log(hexStr);
|
|
|
+ // }
|
|
|
let $data = that.get_big_data(hexStr);
|
|
|
- // wx.getBLEDeviceRSSI({
|
|
|
- // deviceId: characteristic.deviceId,
|
|
|
- // success(res) {
|
|
|
- // $this.rssi = Math.abs(res["RSSI"]);
|
|
|
- // },
|
|
|
- // });
|
|
|
-
|
|
|
- //监听连接教具
|
|
|
- if (hexStr.substr(0, 8).toLowerCase() == "aadd0a00") {
|
|
|
- if ($this.toy_item["hex"]) {
|
|
|
- let $hex = $this.toy_item["hex"].substr($this.toy_item["hex"].length - 2, 2);
|
|
|
- if (hexStr.substr(8, 2) == $hex) {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+
|
|
|
+ // 2021年10月20日17:18:13 判断教具 连接
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD0A") != -1) {
|
|
|
+ //没连接上教具
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD0A0000") != -1) {
|
|
|
+ $this.change_toy_connect_status(3);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ let $hexAry = $this.current_hex.split(" ")
|
|
|
+ // 03 00 01 00 0a
|
|
|
+ // [2] [3]
|
|
|
+ // 连接上教具
|
|
|
+ if (new RegExp("00").test($hexAry[2]) == false) {
|
|
|
+ if (new RegExp("00").test($hexAry[3]) == true) {
|
|
|
+ that.sendConnectOneToOne($hexAry[2])
|
|
|
+ $this.current_hex = `03 00 $hexAry[2] 01 0A`
|
|
|
+ console.log("一对多")
|
|
|
+ } else {
|
|
|
+ console.log("一对一")
|
|
|
+ $this.current_hex = "";
|
|
|
wx.showToast({
|
|
|
title: "已连接到" + $this.toy_item.name
|
|
|
});
|
|
|
+ $this.toy_connected = true;
|
|
|
$this.change_toy_connect_status(2);
|
|
|
- } else if (hexStr.substr(8, 2) == "00") {
|
|
|
- $this.change_toy_connect_status(3);
|
|
|
+ //连接成功后 获取一次教具电量信息
|
|
|
+ that.SendOrder('8a')
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //暂停脑控,切断教具连接
|
|
|
- if (hexStr.substr(0, 8).toLowerCase() == "aadd0900") {
|
|
|
- this.shutdownSendControl();
|
|
|
- }
|
|
|
|
|
|
+ //2021年10月21日16:30:07
|
|
|
+ // 获取教具电量
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
|
|
|
+ let $_hexStr = hexStr.substr(6);
|
|
|
+ let $power = parseInt($_hexStr.substr(0, 2), 16)
|
|
|
+ let $voltage = parseInt($_hexStr.substr(2, 2), 16)
|
|
|
+ console.log("电量:" + $power)
|
|
|
+ console.log("电压:" + $voltage / 10)
|
|
|
+ $this.toy_power = $power;
|
|
|
+ //连接上教具的标识
|
|
|
+ connect_toy = true;
|
|
|
+
|
|
|
+ //2021年10月25日09:16:06
|
|
|
+ //获取教具UUID
|
|
|
+ that.SendOrder('84')
|
|
|
+ }
|
|
|
|
|
|
//监听佩戴正确
|
|
|
if (hexStr.substr(0, 6) == "555520") {
|
|
@@ -283,6 +354,67 @@ export default {
|
|
|
$this.device_bg = $s1 == "00";
|
|
|
}
|
|
|
|
|
|
+ // 2021年10月25日09:15:50
|
|
|
+ // 收到一次UUID就发送一次获取教具的电量
|
|
|
+ // 读取教具UUID
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
|
|
|
+ let $_hexStr = hexStr.substr(6);
|
|
|
+ let $datas = $_hexStr.substr(0, 10);
|
|
|
+ if ($datas != "0000000000") {
|
|
|
+ $this.toy_UUID = $datas;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //2021年10月26日11:09:01
|
|
|
+ // 量产结束读取数据
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD02") != -1) {
|
|
|
+ //脑环型号
|
|
|
+ let $ring_type = hexStr.substr(10).substr(0, hexStr.substr(10).length - 2);
|
|
|
+ for (let $i = 0; $i < $ring_type.length; $i += 2) {
|
|
|
+ let $code = parseInt($ring_type.substr($i, 2), 16)
|
|
|
+ $this.device_data.product_model += String.fromCharCode($code)
|
|
|
+ }
|
|
|
+ that.SendOrder("01")
|
|
|
+
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD01") != -1) {
|
|
|
+ let _UUID = ""
|
|
|
+ let $_str4 = hexStr.substr(6);
|
|
|
+ let $UUID = $_str4.substr(0, $_str4.length - 2);
|
|
|
+ for (let $i = 0; $i < $UUID.length; $i += 8) {
|
|
|
+ let $code = $UUID.substr($i, 8);
|
|
|
+ _UUID += `${$code}-`
|
|
|
+ }
|
|
|
+ _UUID = _UUID.substr(0, _UUID.length - 1)
|
|
|
+ $this.device_data.UUID = _UUID
|
|
|
+ that.SendOrder("03")
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD03") != -1) {
|
|
|
+ let $_data = that.DoAnalysis(hexStr, 12);
|
|
|
+ $this.device_data.software_version = $_data.substr(0, 4)
|
|
|
+ // $this.device_data.software_date = $_data.substr(4)
|
|
|
+ that.SendOrder("8d")
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD8D") != -1) {
|
|
|
+ let $_str5 = hexStr.substr(10);
|
|
|
+ let $version_date = $_str5.substr(0, $_str5.length - 2);
|
|
|
+ let $_data = "";
|
|
|
+ for (let $i = 0; $i < $version_date.length; $i += 2) {
|
|
|
+ let $code = parseInt($version_date.substr($i, 2), 16)
|
|
|
+ $_data += String.fromCharCode($code)
|
|
|
+ }
|
|
|
+ let $version_data = $_data.substr(0, 2).split('')
|
|
|
+ $this.device_data.hardware_version = `${$version_data[0]}.${$version_data[1]}`
|
|
|
+
|
|
|
+ setDeviceInDb($this, $this.device.deviceId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD09") != -1) {
|
|
|
+ control_close = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 监听脑环电量
|
|
|
let $power = that.get_device_elc(hexStr);
|
|
|
if ($power) {
|
|
@@ -301,7 +433,6 @@ export default {
|
|
|
}
|
|
|
|
|
|
//游戏中模块
|
|
|
- let $game_status = game_store.getters.getGameStatus();
|
|
|
if ($game_status == 1 && $data) {
|
|
|
try {
|
|
|
if ($this.played_time > 0) {
|
|
@@ -310,14 +441,15 @@ export default {
|
|
|
game_store.setters.setPlayedTime($this.played_time)
|
|
|
$this.played_time_text = that.formatPlaySeconds($this.played_time);
|
|
|
$this.do_datas($data);
|
|
|
- // if ($this.played_time_text.indexOf(":00") != -1 || $this.played_time_text.indexOf(":30") != -1) {
|
|
|
- // $this.post_data();
|
|
|
- // }
|
|
|
+ if ($this.played_time % 10 == 0) {
|
|
|
+ //读取教具电量
|
|
|
+ that.SendOrder('8a')
|
|
|
+ }
|
|
|
}
|
|
|
if ($this.played_time == 0) {
|
|
|
$this.post_data();
|
|
|
//判断是否隐藏 隐藏则不绘画
|
|
|
- that.shutdownSendControl();
|
|
|
+ that.sendControlClose();
|
|
|
let $hide_status = game_store.getters.getHideStatus();
|
|
|
if (!$hide_status) {
|
|
|
$this.game_finished();
|
|
@@ -385,7 +517,7 @@ export default {
|
|
|
return $errmsg;
|
|
|
},
|
|
|
//写入buffer
|
|
|
- WriteBufferInBle($hex) {
|
|
|
+ WriteBufferInBle($hex, $buffer_len = 8) {
|
|
|
let $device_id = game_store.getters.getDeviceId();
|
|
|
let $service_id = game_store.getters.getServiceId()
|
|
|
let $charateristic = game_store.getters.getCharacterId();
|
|
@@ -401,7 +533,7 @@ export default {
|
|
|
let $checksum = ($hex_sum ^ parseInt($8f, 16)) & parseInt($ff, 16);
|
|
|
|
|
|
$hex = $hex_header + $hex + " " + $checksum.toString(16);
|
|
|
- let buffer = new ArrayBuffer(8);
|
|
|
+ let buffer = new ArrayBuffer($buffer_len);
|
|
|
let dataView = new DataView(buffer);
|
|
|
$hex_ary = $hex.split(" ");
|
|
|
$hex_ary.forEach(($val, $index) => {
|
|
@@ -421,5 +553,33 @@ export default {
|
|
|
console.log(err);
|
|
|
},
|
|
|
});
|
|
|
- }
|
|
|
+ },
|
|
|
+ //写入指令
|
|
|
+ SendOrder(id) {
|
|
|
+ let $hexStr = `03 00 00 00 ${id}`;
|
|
|
+ this.WriteBufferInBle($hexStr)
|
|
|
+ },
|
|
|
+ Send16Order(val, id) {
|
|
|
+ let $str = val;
|
|
|
+ let $str_ary = $str.split('');
|
|
|
+ $str = "";
|
|
|
+ $str_ary.forEach(($val, $index) => {
|
|
|
+ $str += $val.charCodeAt().toString(16) + " "
|
|
|
+ });
|
|
|
+ $str = $str.substr(0, $str.length - 1)
|
|
|
+ let $hexStr = "03 ff " + $str + ` ${id}`;
|
|
|
+ this.WriteBufferInBle($hexStr, 16)
|
|
|
+ },
|
|
|
+//解析
|
|
|
+ DoAnalysis(hexStr, byte_count, sublen = 6) {
|
|
|
+ //byte_count
|
|
|
+ let $_str5 = hexStr.substr(sublen);
|
|
|
+ let $datas = $_str5.substr(0, byte_count * 2);
|
|
|
+ let $_data = "";
|
|
|
+ for (let $i = 0; $i < $datas.length; $i += 2) {
|
|
|
+ let $code = parseInt($datas.substr($i, 2), 16)
|
|
|
+ $_data += String.fromCharCode($code)
|
|
|
+ }
|
|
|
+ return $_data;
|
|
|
+ },
|
|
|
};
|