|
@@ -4,27 +4,147 @@ import Notify from "../../static/vant/notify/notify";
|
|
|
|
|
|
var $ff = "ff";
|
|
|
var $8f = "ffffffff"
|
|
|
-export default {
|
|
|
+var control_close = false
|
|
|
+var current_device_sn = ""
|
|
|
+//记录当前脑环的mac地址
|
|
|
+var current_device_mac = "";
|
|
|
+//记录教具连接状态
|
|
|
+var connect_toy = true;
|
|
|
+//记录教具连接的id
|
|
|
+var current_toy_id = "00";
|
|
|
+//记录教具的UUID
|
|
|
+var current_toy_UUID = "";
|
|
|
+
|
|
|
+//标记是否打开脑控
|
|
|
+var FlagOpenControl = false;
|
|
|
+
|
|
|
+var ToyPower = 0;
|
|
|
+
|
|
|
+//Math.round(new Date() / 1000)
|
|
|
+var logTime = "";
|
|
|
+
|
|
|
+var current_hex = "";
|
|
|
+
|
|
|
+var is_new = game_store.getters.getIsNew();
|
|
|
+
|
|
|
+//2021年11月10日14:59:38
|
|
|
+// 0为旧 1为新
|
|
|
+var services = [
|
|
|
+ "6e400001-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase(),
|
|
|
+ "0000fff0-0000-1000-8000-00805f9b34fb".toUpperCase()
|
|
|
+]
|
|
|
+var charateristics = [
|
|
|
+ {
|
|
|
+ notify: "6e400003-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase(),
|
|
|
+ write: "6e400002-b5a3-f393-e0a9-e50e24dcca9e".toUpperCase()
|
|
|
+ },
|
|
|
+ {
|
|
|
+ notify: "0000fff1-0000-1000-8000-00805f9b34fb".toUpperCase(),
|
|
|
+ write: "0000fff2-0000-1000-8000-00805f9b34fb".toUpperCase()
|
|
|
+ },
|
|
|
+]
|
|
|
+
|
|
|
+function getServicesAndCharateristc() {
|
|
|
+ //获取当前设备名称
|
|
|
+ // let $brainSn = game_store.getters.getDeviceSn();
|
|
|
+ let $deviceId = game_store.getters.getDeviceId();
|
|
|
+ let $serviceId = game_store.getters.getServiceId();
|
|
|
+ let $index = services.indexOf($serviceId.toUpperCase())
|
|
|
+ // let $index = $brainSn.toUpperCase().indexOf("JELLYFISH") != -1 ? 0 : 1;
|
|
|
+ return {
|
|
|
+ deviceId: $deviceId,
|
|
|
+ service: services[$index],
|
|
|
+ charateristic: charateristics[$index]
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- // 发送开启脑电数据包指令
|
|
|
- sendOpen() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 ff");
|
|
|
+export default {
|
|
|
+ //变量
|
|
|
+ connect_toy, control_close, current_toy_id, current_toy_UUID, ToyPower, current_device_sn, is_new,
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo:打开大包数据
|
|
|
+ */
|
|
|
+ sendOpenBigData() {
|
|
|
+ this.SendOrder('ff')
|
|
|
},
|
|
|
- // 发送开启教具脑控指令
|
|
|
+ /**
|
|
|
+ * todo:关闭大包数据
|
|
|
+ */
|
|
|
+ sendCloseBigData() {
|
|
|
+ this.SendOrder('00')
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * todo:获取当前连接的教具类型
|
|
|
+ */
|
|
|
+ sendGetToyType() {
|
|
|
+ this.SendOrder('06')
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * todo:开启脑控
|
|
|
+ */
|
|
|
sendControl() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 07");
|
|
|
+ let that = this;
|
|
|
+ that.SendOrder('07')
|
|
|
},
|
|
|
- //发送断开玩具的指令
|
|
|
- sendEnd() {
|
|
|
- //AA CC 03 00 00 00 09 F3
|
|
|
- this.WriteBufferInBle("03 00 00 00 09");
|
|
|
+ /**
|
|
|
+ * todo:关闭脑控
|
|
|
+ */
|
|
|
+ sendControlClose() {
|
|
|
+ let that = this
|
|
|
+ control_close = false;
|
|
|
+ let $intv = setInterval(() => {
|
|
|
+ if (!control_close) {
|
|
|
+ that.SendOrder('09')
|
|
|
+ //如果是jellyfish则发送00教具
|
|
|
+ if(!is_new){
|
|
|
+ that.sendConnectOneToMore('00')
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //设置打开脑控为false
|
|
|
+ FlagOpenControl = false;
|
|
|
+ clearInterval($intv)
|
|
|
+ //清空当前数据
|
|
|
+ that.clearLocalDatas()
|
|
|
+ }
|
|
|
+ }, 1000)
|
|
|
},
|
|
|
- // 发送停止发送脑电数据包指令
|
|
|
- sendPause() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 08");
|
|
|
+ /**
|
|
|
+ * todo:发送一对多连接
|
|
|
+ */
|
|
|
+ sendConnectOneToMore(id) {
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 00 0A`);
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * todo:发送一对一连接
|
|
|
+ */
|
|
|
+ sendConnectOneToOne(id) {
|
|
|
+ current_toy_id = id;
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 01 0A`)
|
|
|
},
|
|
|
|
|
|
- // 获取大包数据 进行绘制图表
|
|
|
+ /**
|
|
|
+ * todo:获取教具电量
|
|
|
+ */
|
|
|
+ sendToyPower() {
|
|
|
+ let that = this;
|
|
|
+ if (is_new) {
|
|
|
+ let $connect_false_count = 0;
|
|
|
+ let toy_intv = setInterval(() => {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+ that.SendOrder('8a')
|
|
|
+ }
|
|
|
+ }, 3000)
|
|
|
+ }else{
|
|
|
+ console.log("旧脑环不支持获取教具电量")
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo:获取大包数据进行绘制图表
|
|
|
+ */
|
|
|
get_big_data(hex) {
|
|
|
if (hex.substr(0, 6) != "555520") {
|
|
|
return false;
|
|
@@ -76,16 +196,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,
|
|
@@ -98,7 +208,9 @@ export default {
|
|
|
high_beta: $high_beta >= $max_num ? $max_num : $high_beta
|
|
|
};
|
|
|
},
|
|
|
- //获取设备电量
|
|
|
+ /**
|
|
|
+ * todo:获取设备电量
|
|
|
+ */
|
|
|
get_device_elc(hex) {
|
|
|
if (hex.substr(0, 8) != "55550203") {
|
|
|
return false;
|
|
@@ -107,23 +219,16 @@ export default {
|
|
|
return $power;
|
|
|
},
|
|
|
|
|
|
- // 监听蓝牙连接状态
|
|
|
- watch_bluetooth_status($that) {
|
|
|
+ /**
|
|
|
+ * todo:监听蓝牙连接状态
|
|
|
+ */
|
|
|
+ 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,22 +237,84 @@ export default {
|
|
|
Notify({
|
|
|
type: 'danger',
|
|
|
duration: 0,
|
|
|
- message: '智脑环已断开连接,请重新扫码智脑环'
|
|
|
+ message: '智脑环已断开连接,正在尝试重新连接',
|
|
|
+ onOpened() {
|
|
|
+ that.reconnect(res.deviceId, $this)
|
|
|
+ }
|
|
|
});
|
|
|
+ control_close = true
|
|
|
// game_store.setters.setGameStatus(0);
|
|
|
// 结束状态更改为1
|
|
|
- game_store.setters.setGameCloseStatus(1);
|
|
|
+ } else {
|
|
|
+ //关闭脑控
|
|
|
+ // that.sendControlClose();
|
|
|
+ game_store.setters.setGameStatus(0);
|
|
|
+ // 清空链接得设备 三值
|
|
|
+ game_store.setters.clearDeviceToy();
|
|
|
+ $this.connect_toy = 0;
|
|
|
+ $this.connect_show = false;
|
|
|
+ $this.device_bg = false;
|
|
|
+ $this.change_toy_connect_status(0);
|
|
|
+ $this.device_status = 0;
|
|
|
+ $this.device = {}
|
|
|
+ $this.toy_UUID = "";
|
|
|
+ $this.$forceUpdate();
|
|
|
+ wx.closeBluetoothAdapter();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
|
|
|
+ /**
|
|
|
+ * todo:重新连接蓝牙
|
|
|
+ */
|
|
|
+ reconnect($deviceId, $this) {
|
|
|
+ let that = this;
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
+ //重连的次数
|
|
|
+ let $connect_count = 0;
|
|
|
+ let $rec = setInterval(() => {
|
|
|
+ $connect_count += 1;
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.createBLEConnection({
|
|
|
+ deviceId: $deviceInfo.deviceId,
|
|
|
+ success(res) {
|
|
|
+ Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
|
|
|
+ let $system = wx.getSystemInfoSync()
|
|
|
+ if ($system.platform == 'ios') {
|
|
|
+ that.getBLEDeviceServices($deviceInfo.deviceId)
|
|
|
+ that.watchingDevice($this)
|
|
|
+ } else {
|
|
|
+ that.openNotify($this)
|
|
|
+ that.watch_bluetooth_status($this);
|
|
|
+ }
|
|
|
|
|
|
+ clearInterval($rec)
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
|
|
|
+ game_store.setters.setGameCloseStatus(1);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if ($connect_count >= 3) {
|
|
|
+ $this.game_finished();
|
|
|
+ clearInterval($rec)
|
|
|
}
|
|
|
+ } else {
|
|
|
+ clearInterval($rec)
|
|
|
}
|
|
|
- });
|
|
|
+ }, 5000)
|
|
|
+
|
|
|
},
|
|
|
|
|
|
- //获取蓝牙设备服务
|
|
|
+ /**
|
|
|
+ * todo 获取蓝牙设备服务
|
|
|
+ * @param deviceId
|
|
|
+ */
|
|
|
getBLEDeviceServices(deviceId) {
|
|
|
- const $this = this;
|
|
|
-
|
|
|
+ const that = this;
|
|
|
+ current_device_mac = deviceId
|
|
|
wx.getBLEDeviceServices({
|
|
|
deviceId,
|
|
|
success: (res) => {
|
|
@@ -155,18 +322,22 @@ export default {
|
|
|
console.log("serviceItem:" + res.services[i].uuid);
|
|
|
if (res.services[i].uuid.indexOf('6E') != -1 || res.services[i].uuid.indexOf('0000FFF0') != -1) {
|
|
|
console.log("SelectedServiceItem:" + res.services[i].uuid);
|
|
|
- $this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
|
|
|
+ that.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
|
|
|
+ game_store.setters.setServiceId(res.services[i].uuid)
|
|
|
+ is_new = game_store.getters.getIsNew()
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
fail(res) {
|
|
|
- console.log("连接蓝牙成功,获取服务失败");
|
|
|
+ console.log("连接蓝牙成功,获取服务失败", res);
|
|
|
},
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- //获取蓝牙设备某个服务中所有特征值
|
|
|
+ /**
|
|
|
+ * todo 获取蓝牙设备某个服务中所有特征值
|
|
|
+ */
|
|
|
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
|
|
console.log(deviceId, serviceId);
|
|
|
const $this = this;
|
|
@@ -177,25 +348,12 @@ export default {
|
|
|
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.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) {
|
|
@@ -214,17 +372,27 @@ export default {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
-
|
|
|
- // 发送连接玩具指令
|
|
|
- sendConnect($hex) {
|
|
|
- //获取最后两个字节
|
|
|
- this.WriteBufferInBle("03 00 " + $hex + " 00 0a");
|
|
|
- },
|
|
|
- // 获取当前连接得玩具
|
|
|
- getConnectedToy() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 06");
|
|
|
+ openNotify($this) {
|
|
|
+ let that = this;
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
+ console.log($deviceInfo)
|
|
|
+ wx.notifyBLECharacteristicValueChange({
|
|
|
+ deviceId: $deviceInfo.deviceId,
|
|
|
+ serviceId: $deviceInfo.service,
|
|
|
+ characteristicId: $deviceInfo.charateristic.notify,
|
|
|
+ state: true,
|
|
|
+ success() {
|
|
|
+ that.watchingDevice($this)
|
|
|
+ that.sendOpenBigData();
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
- // ArrayBuffer转16进度字符串示例
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo ArrayBuffer转16进度字符串示例
|
|
|
+ * @param buffer
|
|
|
+ * @returns {string}
|
|
|
+ */
|
|
|
ab2hex(buffer) {
|
|
|
var hexArr = Array.prototype.map.call(
|
|
|
new Uint8Array(buffer),
|
|
@@ -234,45 +402,140 @@ export default {
|
|
|
);
|
|
|
return hexArr.join("");
|
|
|
},
|
|
|
- //断开发送数据
|
|
|
- shutdownSendControl() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 0a");
|
|
|
- },
|
|
|
|
|
|
- // 监听脑环是否带正
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 监听脑环数据
|
|
|
+ * @param $this
|
|
|
+ */
|
|
|
watchingDevice($this) {
|
|
|
+ console.log("监听数据", "is_new:" + is_new)
|
|
|
const that = this;
|
|
|
+ let DeviceId = game_store.getters.getDeviceId();
|
|
|
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
let hexStr = that.ab2hex(characteristic.value);
|
|
|
- console.log(hexStr);
|
|
|
+
|
|
|
+ // 教具断链
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE70") != -1) {
|
|
|
+ wx.showToast({
|
|
|
+ title: "教具已断开",
|
|
|
+ icon: "error"
|
|
|
+ })
|
|
|
+ //重连机制
|
|
|
+ that.SendOrder("09")
|
|
|
+ setTimeout(() => {
|
|
|
+ that.ReconnectToy()
|
|
|
+ }, 1500)
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE07") != -1) {
|
|
|
+ that.sendControl();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD") != -1 || hexStr.toUpperCase().indexOf("AAEE") != -1) {
|
|
|
+ let nowTime = Math.round(new Date() / 1000);
|
|
|
+ console.log(`应答耗时:${(nowTime - logTime)}秒`)
|
|
|
+ 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 $mHexStrIndex = hexStr.toUpperCase().indexOf("AADD0A");
|
|
|
+ let $hex_index = hexStr.substr($mHexStrIndex + 28, 2)
|
|
|
+ let $toy_id = hexStr.substr($mHexStrIndex + 8, 2)
|
|
|
+ console.log("连接", $hex_index)
|
|
|
+ console.log("玩具", $toy_id)
|
|
|
+ // 连接上教具
|
|
|
+ if (new RegExp("00").test($hex_index) == true) {
|
|
|
+ if (is_new) {
|
|
|
+ that.sendConnectOneToOne($toy_id)
|
|
|
+ $this.current_hex = `03 00 ${$toy_id} 01 0A`
|
|
|
+ console.log("一对多")
|
|
|
+ } else {
|
|
|
+ $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);
|
|
|
}
|
|
|
}
|
|
|
+ if (new RegExp("01").test($hex_index) == true) {
|
|
|
+ console.log("一对一")
|
|
|
+ $this.current_hex = "";
|
|
|
+ wx.showToast({
|
|
|
+ title: "已连接到" + $this.toy_item.name
|
|
|
+ });
|
|
|
+ $this.toy_connected = true;
|
|
|
+ $this.change_toy_connect_status(2);
|
|
|
+ //连接成功后 获取一次教具名称
|
|
|
+ that.SendOrder('87')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (new RegExp("02").test($hex_index) == true) {
|
|
|
+ //发送教具连接
|
|
|
+ connect_toy = true
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.showToast({
|
|
|
+ title: "教具重连成功"
|
|
|
+ })
|
|
|
+ that.sendControl()
|
|
|
+ that.sendToyPower();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //暂停脑控,切断教具连接
|
|
|
- if (hexStr.substr(0, 8).toLowerCase() == "aadd0900") {
|
|
|
- this.shutdownSendControl();
|
|
|
+ //2021年11月23日10:39:49
|
|
|
+ // 获取教具名称
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD87") != -1) {
|
|
|
+ let $mHexStr = hexStr.substr(hexStr.toUpperCase().indexOf("AADD87"))
|
|
|
+ let $datas = that.DoAnalysis($mHexStr, 10)
|
|
|
+ let $number = $datas.match(/\d+/)
|
|
|
+ let toy_list_pre = {'01': "SW", '02': "KL", '04': "SC", '05': "PP", '06': "SU", '09': "UF"}
|
|
|
+ let $sn = toy_list_pre[current_toy_id] + $number;
|
|
|
+ $this.toy_sn = $sn;
|
|
|
+ //获取教具电量
|
|
|
+ that.SendOrder('8a')
|
|
|
}
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE87") != -1) {
|
|
|
+ //获取教具电量
|
|
|
+ that.SendOrder('87')
|
|
|
+ }
|
|
|
+
|
|
|
+ //2021年10月21日16:30:07
|
|
|
+ // 获取教具电量
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD8A") != -1) {//接收教具电量状态
|
|
|
+ let $_hexStr = hexStr.substr(hexStr.toUpperCase().indexOf("AADD8A") + 6);
|
|
|
+ let $power = parseInt($_hexStr.substr(0, 2), 16)
|
|
|
+ let $voltage = parseInt($_hexStr.substr(2, 2), 16)
|
|
|
+
|
|
|
+ // $this.toy_power = Math.round(that.CalBLEPower($voltage));
|
|
|
+ if ($power > 0) {
|
|
|
+ $this.toy_power = $power
|
|
|
+ $this.toy_voltage = $voltage
|
|
|
+ ToyPower = $power;
|
|
|
+ }
|
|
|
|
|
|
+ //连接上教具的标识
|
|
|
+ //FF为无效
|
|
|
+ if ($power != 0) {
|
|
|
+ connect_toy = true;
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("FF") != -1) {
|
|
|
+ connect_toy = false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (current_toy_UUID == "") {
|
|
|
+ //获取教具UUID
|
|
|
+ that.SendOrder('84')
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//监听佩戴正确
|
|
|
if (hexStr.substr(0, 6) == "555520") {
|
|
@@ -283,25 +546,74 @@ export default {
|
|
|
$this.device_bg = $s1 == "00";
|
|
|
}
|
|
|
|
|
|
- // 监听脑环电量
|
|
|
- let $power = that.get_device_elc(hexStr);
|
|
|
- if ($power) {
|
|
|
- // console.log("当前脑环电量:", $power)
|
|
|
- $this.device_power = $power;
|
|
|
+ // 2021年10月25日09:15:50
|
|
|
+ // 收到一次UUID就发送一次获取教具的电量
|
|
|
+ // 读取教具UUID
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD84") != -1) {//接收UUID 5个字节
|
|
|
+ let $hex_index = hexStr.toUpperCase().indexOf("AADD84") + 6;
|
|
|
+ let $datas = hexStr.substr($hex_index, 10);
|
|
|
+ if ($datas != "0000000000") {
|
|
|
+ console.log("以获取UUID:" + $datas)
|
|
|
+ current_toy_UUID = $datas;
|
|
|
+ $this.toy_UUID = $datas;
|
|
|
+ }
|
|
|
}
|
|
|
- if ($power < 10 && $power > 0) {
|
|
|
- wx.showToast({
|
|
|
- title: "脑环电量不足",
|
|
|
- icon: "none",
|
|
|
- duration: 2000,
|
|
|
- success() {
|
|
|
- // $this.change_device_status(0);
|
|
|
- },
|
|
|
- });
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD09") != -1) {
|
|
|
+ control_close = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //收到发送UUID的应答立马发送连接教具的指令
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD8E") != -1) {
|
|
|
+ //发送教具连接
|
|
|
+ that.WriteBufferInBle(`03 00 ${current_toy_id} 02 0A`)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // 2021年11月10日14:34:26
|
|
|
+ // 处理打开脑控的应答
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD07") != -1) {
|
|
|
+ FlagOpenControl = true;
|
|
|
+ console.log($game_status)
|
|
|
+ if ($game_status != 1) {
|
|
|
+ setTimeout(() => {
|
|
|
+ mpvue.navigateTo({
|
|
|
+ url: "/pages/start/main",
|
|
|
+ success() {
|
|
|
+ // 设置游戏状态为游戏中
|
|
|
+ game_store.setters.setGameStatus(1);
|
|
|
+ $this.gameStart();
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }, 800);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.substr(0, 8) == "55550203") {
|
|
|
+ let $power = parseInt(hexStr.substr(8, 2), 16);
|
|
|
+ let $voltage = parseInt(hexStr.substr(10, 2), 16);
|
|
|
+ // 监听脑环电量
|
|
|
+ if ($power) {
|
|
|
+ // console.log("当前脑环电量:", $power)
|
|
|
+ $this.device_power = $power;
|
|
|
+ $this.device_voltage = $voltage;
|
|
|
+ }
|
|
|
+ if ($power < 10 && $power > 0) {
|
|
|
+ wx.showToast({
|
|
|
+ title: "脑环电量不足",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000,
|
|
|
+ success() {
|
|
|
+ // $this.change_device_status(0);
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//游戏中模块
|
|
|
- let $game_status = game_store.getters.getGameStatus();
|
|
|
if ($game_status == 1 && $data) {
|
|
|
try {
|
|
|
if ($this.played_time > 0) {
|
|
@@ -310,14 +622,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.sendToyPower_CheckReconected()
|
|
|
// }
|
|
|
}
|
|
|
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();
|
|
@@ -329,7 +642,12 @@ export default {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- // 游玩时间倒计时
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 游玩时间倒计时
|
|
|
+ * @param value
|
|
|
+ * @returns {string}
|
|
|
+ */
|
|
|
formatPlaySeconds(value) {
|
|
|
// 字符串转数字
|
|
|
var secondTime = parseInt(value); // 秒
|
|
@@ -358,7 +676,12 @@ export default {
|
|
|
return result;
|
|
|
},
|
|
|
|
|
|
- //根据错误代码返回字符串信息
|
|
|
+ /**
|
|
|
+ * todo 根据错误代码返回字符串信息
|
|
|
+ * @param errCode
|
|
|
+ * @returns {string}
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
GetopenBluetoothAdapterError(errCode) {
|
|
|
|
|
|
let $errmsg = "";
|
|
@@ -384,11 +707,16 @@ export default {
|
|
|
|
|
|
return $errmsg;
|
|
|
},
|
|
|
- //写入buffer
|
|
|
- WriteBufferInBle($hex) {
|
|
|
- let $device_id = game_store.getters.getDeviceId();
|
|
|
- let $service_id = game_store.getters.getServiceId()
|
|
|
- let $charateristic = game_store.getters.getCharacterId();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 写入buffer
|
|
|
+ * @param $hex
|
|
|
+ * @param $buffer_len
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ WriteBufferInBle($hex, $buffer_len = 8) {
|
|
|
+ let that = this;
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
let $hex_header = "aa cc ";
|
|
|
// let $hex = "03 00 01 00 0a";
|
|
|
|
|
@@ -401,7 +729,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) => {
|
|
@@ -409,17 +737,161 @@ export default {
|
|
|
})
|
|
|
|
|
|
wx.writeBLECharacteristicValue({
|
|
|
- deviceId: $device_id,
|
|
|
- serviceId: $service_id,
|
|
|
- characteristicId: $charateristic,
|
|
|
+ deviceId: $deviceInfo.deviceId,
|
|
|
+ serviceId: $deviceInfo.service,
|
|
|
+ characteristicId: $deviceInfo.charateristic.write,
|
|
|
value: buffer,
|
|
|
success: function (res) {
|
|
|
console.log($hex + ',写入成功')
|
|
|
+ logTime = Math.round(new Date() / 1000);
|
|
|
},
|
|
|
fail: function (err) {
|
|
|
console.log($hex + "写入失败");
|
|
|
- console.log(err);
|
|
|
+ console.log(err, $deviceInfo);
|
|
|
},
|
|
|
});
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo写入8位指令
|
|
|
+ * @param id 末尾id
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ SendOrder(id) {
|
|
|
+ let $hexStr = `03 00 00 00 ${id}`;
|
|
|
+ this.WriteBufferInBle($hexStr)
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 写入16个字节的指令
|
|
|
+ * @param val
|
|
|
+ * @param id
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ 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)
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 解析hex
|
|
|
+ * @param hexStr
|
|
|
+ * @param byte_count
|
|
|
+ * @param sublen
|
|
|
+ * @returns {string}
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 重连教具
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ ReconnectToy() {
|
|
|
+ current_device_mac = game_store.getters.getDeviceId();
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+
|
|
|
+ let that = this;
|
|
|
+ try {
|
|
|
+ //发送UUID
|
|
|
+ let $uuid_str = "";
|
|
|
+ for (let $i = 0; $i < current_toy_UUID.length; $i += 2) {
|
|
|
+ $uuid_str += current_toy_UUID.substr($i, 2) + " ";
|
|
|
+ }
|
|
|
+
|
|
|
+ let _deviceId = "";
|
|
|
+ let deviceIds = current_device_mac.split(':');
|
|
|
+ for (let i = 0; i < deviceIds.length; i++) {
|
|
|
+ if (i != 2) {
|
|
|
+ _deviceId += deviceIds[i] + " ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let sum = $uuid_str + _deviceId;
|
|
|
+ sum = sum.substr(0, sum.length - 1);
|
|
|
+ let $hexStr = `03 ff ${sum} 8e`;
|
|
|
+ let loop_count = "";
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.showLoading({
|
|
|
+ title: "重新连接教具中"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ let connect_toy_intv = setInterval(() => {
|
|
|
+ $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+
|
|
|
+ if (connect_toy == false) {
|
|
|
+ if (loop_count == 3) {
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.showToast("教具已无法连接")
|
|
|
+ clearInterval(connect_toy_intv)
|
|
|
+ }
|
|
|
+ loop_count += 1;
|
|
|
+ that.WriteBufferInBle($hexStr, 16)
|
|
|
+ } else {
|
|
|
+ wx.hideLoading()
|
|
|
+ loop_count = 0;
|
|
|
+ clearInterval(connect_toy_intv)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ wx.hideLoading()
|
|
|
+ clearInterval(connect_toy_intv)
|
|
|
+ }
|
|
|
+ }, 3000)
|
|
|
+
|
|
|
+
|
|
|
+ } catch (e) {
|
|
|
+ console.log("ReconnectToy:", e)
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 清空当前脚本的变量值
|
|
|
+ */
|
|
|
+ clearLocalDatas() {
|
|
|
+ control_close = false
|
|
|
+ connect_toy = false;
|
|
|
+ current_toy_id = "00";
|
|
|
+ current_toy_UUID = "";
|
|
|
+ },
|
|
|
+
|
|
|
+ /**
|
|
|
+ * todo 计算电量
|
|
|
+ * @param $voltage
|
|
|
+ * @returns {number}
|
|
|
+ * @constructor
|
|
|
+ */
|
|
|
+ CalBLEPower($voltage) {
|
|
|
+ let $max = 4100;
|
|
|
+ let $min = 3500;
|
|
|
+
|
|
|
+ //(当前电压值(mV)-最低电压值(mV)) / (最大电压值(mV)-最低电压值(mV)) *100%
|
|
|
+ //当前电压
|
|
|
+ let $_voltage = $voltage * 100;
|
|
|
+ // console.log(`计算的当前电压:${$_voltage}`)
|
|
|
+
|
|
|
+ let $percent = (($_voltage - $min) / ($max - $min)) * 100;
|
|
|
+ // console.log(`计算的电量:${$percent}`);
|
|
|
+ return $percent;
|
|
|
}
|
|
|
};
|