|
@@ -2,7 +2,7 @@ import ble_store from "../store/bluetooth";
|
|
|
import game_store from "../store/game";
|
|
|
import Toast from "../../static/vant/toast/toast";
|
|
|
import Notify from "../../static/vant/notify/notify";
|
|
|
-import WechatLog from "../utils/wechat_log";
|
|
|
+import {LOG_DEBUG, LOG_WECHAT, LOG_WECHAT_ERROR} from "./log";
|
|
|
|
|
|
function getDeviceWriteInfo() {
|
|
|
let deviceId = ble_store.getters.getDeviceId();
|
|
@@ -20,7 +20,6 @@ function isJELLYFISH() {
|
|
|
return $code.indexOf("JELLYFISH") >= 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* ArrayBuffer转16进度字符串示例
|
|
|
* @param buffer
|
|
@@ -64,14 +63,14 @@ export default {
|
|
|
wx.getBLEDeviceServices({
|
|
|
deviceId,
|
|
|
success: (res) => {
|
|
|
- console.log("获取蓝牙设备服务service:\n", JSON.stringify(res.services));
|
|
|
+ LOG_DEBUG("获取蓝牙设备服务service:\n", JSON.stringify(res.services));
|
|
|
for (let i = 0; i < res.services.length; i++) {
|
|
|
- console.log("第" + (i + 1) + "个UUID:" + res.services[i].uuid + "\n");
|
|
|
+ LOG_DEBUG("第" + (i + 1) + "个UUID:" + res.services[i].uuid + "\n");
|
|
|
if (res.services[i].uuid.indexOf('6E') !== -1 || res.services[i].uuid.indexOf('0000FFF0') !== -1) {
|
|
|
// 获取蓝牙设备某个服务中所有特征值
|
|
|
that.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
|
|
|
ble_store.setters.setServiceId(res.services[i].uuid);
|
|
|
- console.log("脑机deviceId(mac)", deviceId, "notifyServicesId:" + res.services[i].uuid);
|
|
|
+ LOG_DEBUG("脑机deviceId(mac)", deviceId, "notifyServicesId:" + res.services[i].uuid);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
@@ -95,22 +94,22 @@ export default {
|
|
|
deviceId,
|
|
|
serviceId,
|
|
|
success: (res) => {
|
|
|
- console.log("获取服务", serviceId, "的特征值:\n", JSON.stringify(res));
|
|
|
+ LOG_DEBUG("获取服务", serviceId, "的特征值:\n", JSON.stringify(res));
|
|
|
for (let i = 0; i < res.characteristics.length; i++) {
|
|
|
let item = res.characteristics[i]
|
|
|
if (item.properties.read) {
|
|
|
- console.log("第" + (i + 1) + ",该特征值可读:" + item.uuid);
|
|
|
+ LOG_DEBUG("第" + (i + 1) + ",该特征值可读:" + item.uuid);
|
|
|
}
|
|
|
if (item.properties.write) {
|
|
|
- console.log("第" + (i + 1) + ",该特征值可写:" + item.uuid);
|
|
|
+ LOG_DEBUG("第" + (i + 1) + ",该特征值可写:" + item.uuid);
|
|
|
if(item.uuid.indexOf("0002") !== -1){
|
|
|
ble_store.setters.setCharacteristicWriteId(item.uuid);
|
|
|
//打开数据帧 (打开大包数据)
|
|
|
- that.SendOrder('ff');
|
|
|
+ that.SendOrder("ff", "打开数据帧");
|
|
|
}
|
|
|
}
|
|
|
if (item.properties.notify || item.properties.indicate) {
|
|
|
- console.log("第" + (i + 1) + ",该特征值可监听:" + item.uuid);
|
|
|
+ LOG_DEBUG("第" + (i + 1) + ",该特征值可监听:" + item.uuid);
|
|
|
if(item.uuid.indexOf("0003") !== -1){
|
|
|
ble_store.setters.setCharacteristicNotifyId(item.uuid);
|
|
|
}
|
|
@@ -122,7 +121,7 @@ export default {
|
|
|
state: true,
|
|
|
success() {
|
|
|
that.notifyDatas(null);
|
|
|
- console.log("init正在监听特征值:", item.uuid);
|
|
|
+ LOG_DEBUG("init正在监听特征值:", item.uuid);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -150,7 +149,7 @@ export default {
|
|
|
} else {
|
|
|
wx.setStorageSync("report_mode", 1)
|
|
|
}
|
|
|
- console.log("连接教具(获取连接ID):", `03 00 ${$hex} 00 0A`, JSON.stringify(toyItem));
|
|
|
+ LOG_DEBUG("连接教具(获取连接ID):", `03 00 ${$hex} 00 0A`, JSON.stringify(toyItem));
|
|
|
// 连接教具: 03 00 ${$hex} 00 0a
|
|
|
that.sendConnectOneToMore($hex);
|
|
|
}
|
|
@@ -160,7 +159,7 @@ export default {
|
|
|
* 连接教具(获取连接ID)
|
|
|
*/
|
|
|
sendConnectOneToMore(id) {
|
|
|
- this.WriteBufferInBle(`03 00 ${id} 00 0A`);
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 00 0A`, "一对多教具连接");
|
|
|
},
|
|
|
/**
|
|
|
* 发送一对一连接
|
|
@@ -168,23 +167,24 @@ export default {
|
|
|
*/
|
|
|
sendConnectOneToOne(id) {
|
|
|
ble_store.setters.setCurrentToyId(id);
|
|
|
- this.WriteBufferInBle(`03 00 ${id} 01 0A`)
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 01 0A`, "连接教具(使用获取的ID)")
|
|
|
},
|
|
|
/**
|
|
|
* 连接教具(使用下发的ID)
|
|
|
*/
|
|
|
sendConnectOneToToy(id) {
|
|
|
ble_store.setters.setCurrentToyId(id);
|
|
|
- this.WriteBufferInBle(`03 00 ${id} 02 0A`)
|
|
|
+ this.WriteBufferInBle(`03 00 ${id} 02 0A`, "连接教具(使用下发的ID)")
|
|
|
},
|
|
|
/**
|
|
|
* 写入8位指令
|
|
|
* @param id 末尾id
|
|
|
+ * @param $comments
|
|
|
* @constructor
|
|
|
*/
|
|
|
- SendOrder(id) {
|
|
|
+ SendOrder(id, $comments = "") {
|
|
|
let $hexStr = `03 00 00 00 ${id}`;
|
|
|
- this.WriteBufferInBle($hexStr)
|
|
|
+ this.WriteBufferInBle($hexStr, $comments)
|
|
|
},
|
|
|
/**
|
|
|
* 打开或关闭LED灯 AA CC 03 00 00 ctrl EC CKS
|
|
@@ -192,7 +192,7 @@ export default {
|
|
|
*/
|
|
|
SendLedOrder(id) {
|
|
|
let $hexStr = `03 00 00 ${id} ec`;
|
|
|
- this.WriteBufferInBle($hexStr)
|
|
|
+ this.WriteBufferInBle($hexStr, "控制脑机LED灯")
|
|
|
},
|
|
|
|
|
|
/**
|
|
@@ -201,7 +201,7 @@ export default {
|
|
|
*/
|
|
|
SendMotionOrder(id) {
|
|
|
let $hexStr = `03 00 00 ${id} 34`;
|
|
|
- this.WriteBufferInBle($hexStr)
|
|
|
+ this.WriteBufferInBle($hexStr, "设置教具为无运动状态")
|
|
|
},
|
|
|
|
|
|
/**
|
|
@@ -217,7 +217,7 @@ export default {
|
|
|
mTimeOut = `0${mTimeOut}`;
|
|
|
}
|
|
|
let $hexStr = `03 00 ${onVal} ${mTimeOut} d0`;
|
|
|
- this.WriteBufferInBle($hexStr);
|
|
|
+ this.WriteBufferInBle($hexStr, "RF重连");
|
|
|
},
|
|
|
/**
|
|
|
* 开启脑控
|
|
@@ -228,7 +228,7 @@ export default {
|
|
|
title: "正在启动"
|
|
|
})
|
|
|
setTimeout(()=>{
|
|
|
- that.SendOrder('07');
|
|
|
+ that.SendOrder("07", "开启脑控");
|
|
|
},500)
|
|
|
},
|
|
|
/**
|
|
@@ -242,7 +242,7 @@ export default {
|
|
|
},500);
|
|
|
setTimeout(()=>{
|
|
|
// 关闭脑控
|
|
|
- that.SendOrder('09');
|
|
|
+ that.SendOrder("09", "关闭脑控");
|
|
|
ble_store.setters.setCurrentToyId("00");
|
|
|
},1000);
|
|
|
//如果是一代脑机则发送00教具
|
|
@@ -256,10 +256,12 @@ export default {
|
|
|
* todo 写入buffer
|
|
|
* @param $hex
|
|
|
* @param $buffer_len
|
|
|
+ * @param $comments
|
|
|
* @constructor
|
|
|
*/
|
|
|
- WriteBufferInBle($hex, $buffer_len = 8) {
|
|
|
+ WriteBufferInBle($hex, $comments = "", $buffer_len = 8) {
|
|
|
let { deviceId, serviceId, characteristicWriteId } = getDeviceWriteInfo();
|
|
|
+ let $code = ble_store.getters.getDeviceSn();
|
|
|
if (deviceId && serviceId && characteristicWriteId) {
|
|
|
let that = this;
|
|
|
let $hex_header = "aa cc ";
|
|
@@ -282,11 +284,10 @@ export default {
|
|
|
characteristicId: characteristicWriteId,
|
|
|
value: buffer,
|
|
|
success: function (res) {
|
|
|
- WechatLog.info(process.env.NODE_ENV, "写入指令失败:",$hex, that.getNowTime());
|
|
|
+ LOG_WECHAT($code, "写入指令:", $hex, $comments);
|
|
|
},
|
|
|
fail: function (err) {
|
|
|
- console.log("写入指令失败:", $hex, err);
|
|
|
- WechatLog.error(process.env.NODE_ENV, "写入指令失败:", $hex, err);
|
|
|
+ LOG_WECHAT_ERROR($code, "写入指令失败:", $hex, $comments, JSON.stringify(err));
|
|
|
},
|
|
|
});
|
|
|
}
|
|
@@ -302,31 +303,36 @@ export default {
|
|
|
// 监听蓝牙低功耗设备的特征值变化事件
|
|
|
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
let hexStr = ab2hex(characteristic.value);
|
|
|
- console.log("监听脑机数据:", hexStr);
|
|
|
+ let $code = ble_store.getters.getDeviceSn();
|
|
|
+ let $comments = "";
|
|
|
// 处理打开脑控的应答
|
|
|
if (hexStr.toUpperCase().indexOf("AADD07") >= 0) {
|
|
|
- ble_store.setters.setBluetoothLinkStatus(true)
|
|
|
+ ble_store.setters.setBluetoothLinkStatus(true);
|
|
|
+ $comments = "打开脑控的应答";
|
|
|
}
|
|
|
// 收到发送UUID的应答立马发送连接教具的指令
|
|
|
if (hexStr.toUpperCase().indexOf("AADD8E") >= 0) {
|
|
|
let $currentToyId = ble_store.getters.getCurrentToyId();
|
|
|
//发送教具连接(连接教具(使用下发的ID))
|
|
|
- that.sendConnectOneToToy($currentToyId)
|
|
|
+ that.sendConnectOneToToy($currentToyId);
|
|
|
+ $comments = "连接教具(使用下发的ID)的应答";
|
|
|
}
|
|
|
if (hexStr.toUpperCase().indexOf("AAEE87") >= 0) {
|
|
|
let $currentToyId = ble_store.getters.getCurrentToyId();
|
|
|
- //获取教具电量
|
|
|
+ //获取教具设备编号
|
|
|
if ($currentToyId !== '80') {
|
|
|
- that.SendOrder('87')
|
|
|
+ that.SendOrder('87', "获取教具编号")
|
|
|
}
|
|
|
+ $comments = "获取教具编号异常";
|
|
|
}
|
|
|
//let $game_status = game_store.getters.getGameStatus();
|
|
|
//let $currentToyId = ble_store.getters.getCurrentToyId();
|
|
|
// 连接页面
|
|
|
if($this && $this.$options.name){
|
|
|
- console.log("当前页面名称:", $this?$this.$options.name:"");
|
|
|
+ LOG_DEBUG("当前页面名称:", $this?$this.$options.name:"");
|
|
|
// 监听脑机电量
|
|
|
if (hexStr.substring(0, 8) === "55550203") {
|
|
|
+ $comments = "监听脑机电量";
|
|
|
let $power = parseInt(hexStr.substring(8, 10), 16);
|
|
|
// let $voltage = parseInt(hexStr.substring(10, 12), 16);
|
|
|
// // 监听是否插入USB
|
|
@@ -340,6 +346,7 @@ export default {
|
|
|
}
|
|
|
// 判断教具连接
|
|
|
if (hexStr.toUpperCase().indexOf("AADD0A") >= 0) {
|
|
|
+ $comments = "连接教具的应答";
|
|
|
//没连接上教具
|
|
|
if (hexStr.toUpperCase().indexOf("AADD0A0000") >= 0) {
|
|
|
if(!isJELLYFISH()){
|
|
@@ -350,8 +357,7 @@ export default {
|
|
|
let $baseIndex = hexStr.toUpperCase().indexOf("AADD0A");
|
|
|
let $hex_index = hexStr.substring($baseIndex + 28, 30)
|
|
|
let $toy_id = hexStr.substring($baseIndex + 8, 10)
|
|
|
- console.log("连接", $hex_index)
|
|
|
- console.log("教具", $toy_id)
|
|
|
+ LOG_DEBUG("连接HEX:", $hex_index, "教具ID:", $toy_id)
|
|
|
// 连接上教具
|
|
|
if (new RegExp("00").test($hex_index) === true) {
|
|
|
if (isJELLYFISH()){
|
|
@@ -361,7 +367,7 @@ export default {
|
|
|
wx.showToast({title: "已连接到" + $this.toy_item.name });
|
|
|
}
|
|
|
} else {
|
|
|
- console.log("一对多")
|
|
|
+ LOG_DEBUG("一对多")
|
|
|
// 发送一对一连接 03 00 ${$toy_id} 01 0A
|
|
|
that.sendConnectOneToOne($toy_id);
|
|
|
ble_store.setters.setCurrentToyId($toy_id);
|
|
@@ -369,7 +375,7 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
if (new RegExp("01").test($hex_index) === true) {
|
|
|
- console.log("一对一")
|
|
|
+ LOG_DEBUG("一对一")
|
|
|
if($this.toy_item && $this.toy_item.name){
|
|
|
wx.showToast({title: "已连接到" + $this.toy_item.name });
|
|
|
}
|
|
@@ -379,7 +385,7 @@ export default {
|
|
|
if ($toy_id !== "80") {
|
|
|
// 获取一次教具名称
|
|
|
setTimeout(() => {
|
|
|
- that.SendOrder('87')
|
|
|
+ that.SendOrder('87', "获取教具编号")
|
|
|
}, 3000)
|
|
|
// 不断获取教具电量
|
|
|
let toy_interval = setInterval(() => {
|
|
@@ -387,7 +393,7 @@ export default {
|
|
|
if($game_status !== 1){
|
|
|
clearInterval(toy_interval);
|
|
|
} else{
|
|
|
- that.SendOrder('8a');
|
|
|
+ that.SendOrder('8a', "获取教具电量");
|
|
|
}
|
|
|
}, 10000);
|
|
|
}
|
|
@@ -404,7 +410,8 @@ export default {
|
|
|
$this.toy_sn = $sn;
|
|
|
//保存教具sn
|
|
|
ble_store.setters.setToySn($sn);
|
|
|
- console.log("获取教具名称hexStr:",hexStr,",获取教具名称$sn",$sn);
|
|
|
+ LOG_DEBUG("获取教具名称hexStr:", hexStr, ",获取教具名称$sn", $sn);
|
|
|
+ $comments = "获取教具名称";
|
|
|
}
|
|
|
// 获取教具电量
|
|
|
if (hexStr.toUpperCase().indexOf("AADD8A") >= 0) {//接收教具电量状态
|
|
@@ -413,6 +420,7 @@ export default {
|
|
|
if ($power > 0) {
|
|
|
$this.toy_power = $power
|
|
|
}
|
|
|
+ $comments = "获取教具电量的应答";
|
|
|
}
|
|
|
// 教具断链(连续多次到教具的命令没有响应)
|
|
|
if (hexStr.toUpperCase().indexOf("AAEE70") >= 0) {
|
|
@@ -429,12 +437,15 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
+ $comments = "连续多次到教具的命令没有响应";
|
|
|
}
|
|
|
|
|
|
- // 监听佩戴正确, 当s1为 00时 数据有效
|
|
|
+ // 监听数据
|
|
|
if (hexStr.substring(0, 6) === "555520") {
|
|
|
+ $comments = "监听数据";
|
|
|
+ // 监听佩戴正确, 当s1为 00时 数据有效
|
|
|
$this.device_bg = (hexStr.substring(8, 10) === "00");
|
|
|
- console.log("监听佩戴正确:", hexStr.substring(0, 10), $this.device_bg);
|
|
|
+ LOG_DEBUG("监听佩戴正确:", hexStr.substring(0, 10), $this.device_bg);
|
|
|
//游戏中模块
|
|
|
let $game_status = game_store.getters.getGameStatus();
|
|
|
if ($game_status === 1) {
|
|
@@ -463,8 +474,17 @@ export default {
|
|
|
}, 2000)
|
|
|
}
|
|
|
});
|
|
|
+ $comments = "脑环关机的应答";
|
|
|
}
|
|
|
}
|
|
|
+ // 日志DEBUG: 脑机电量 || 教具电量 || 数据
|
|
|
+ let logFlag = (hexStr.substring(0, 8) === "55550203") || (hexStr.toUpperCase().indexOf("AADD8A") >= 0) || (hexStr.substring(0, 6) === "555520");
|
|
|
+ if (logFlag) {
|
|
|
+ LOG_DEBUG($code, "电量及数据应答:", hexStr, $comments);
|
|
|
+ } else {
|
|
|
+ // 日志-推送到微信
|
|
|
+ LOG_WECHAT($code, "指令应答:", hexStr, $comments);
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
|
|
@@ -478,7 +498,7 @@ export default {
|
|
|
wx.onBLEConnectionStateChange((res) => {
|
|
|
// 该方法回调中可以用于处理连接意外断开等异常情况
|
|
|
ble_store.setters.setBluetoothLinkStatus(res.connected);
|
|
|
- console.log(`${that.getNowTime()} 监听脑机是否断连:`, res.connected);
|
|
|
+ LOG_DEBUG("监听脑机连接状态:", res.connected);
|
|
|
if (res.connected === false) {
|
|
|
//判断游戏是否游戏中
|
|
|
let $game_status = game_store.getters.getGameStatus();
|
|
@@ -525,12 +545,13 @@ export default {
|
|
|
let $rec = setInterval(() => {
|
|
|
let $game_status = game_store.getters.getGameStatus();
|
|
|
if ($game_status === 1) {
|
|
|
+ let $code = ble_store.getters.getDeviceSn();
|
|
|
wx.createBLEConnection({
|
|
|
deviceId: $deviceInfo.deviceId,
|
|
|
success() {
|
|
|
clearInterval($rec)
|
|
|
Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
|
|
|
- LogInDb(`${that.getNowTime()} 第${$connect_count}次重新连接成功`)
|
|
|
+ LOG_WECHAT($code, `第${$connect_count}次重新连接成功`);
|
|
|
let $system = wx.getSystemInfoSync()
|
|
|
if ($system.platform === 'ios') {
|
|
|
that.getBLEDeviceServices($deviceInfo.deviceId)
|
|
@@ -563,8 +584,9 @@ export default {
|
|
|
*/
|
|
|
closeConnection($this) {
|
|
|
const that = this;
|
|
|
+ let $code = ble_store.getters.getDeviceSn();
|
|
|
// 关闭脑控
|
|
|
- $this.$connection.SendOrder("09");
|
|
|
+ that.SendOrder("09", "关闭脑控");
|
|
|
game_store.setters.setGameStatus(0);
|
|
|
// todo 清空链接的设备
|
|
|
$this.connect_toy = 0;
|
|
@@ -573,9 +595,9 @@ export default {
|
|
|
//$this.connect_status = false;
|
|
|
|
|
|
// 断开教具及蓝牙连接
|
|
|
- that.SendOrder("31");
|
|
|
+ that.SendOrder("31", "断开教具及蓝牙连接");
|
|
|
setTimeout(()=>{
|
|
|
- that.SendOrder("09");
|
|
|
+ that.SendOrder("09", "关闭脑控");
|
|
|
// 移除搜索到新设备的事件的全部监听函数
|
|
|
wx.offBluetoothDeviceFound();
|
|
|
// 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
|
|
@@ -591,10 +613,10 @@ export default {
|
|
|
});
|
|
|
ble_store.setters.clearDeviceToy();
|
|
|
wx.closeBluetoothAdapter();
|
|
|
- console.log("断开蓝牙连接成功", deviceId);
|
|
|
+ LOG_WECHAT($code, "断开蓝牙连接成功", deviceId);
|
|
|
},
|
|
|
fail(err) {
|
|
|
- console.log("断开蓝牙连接"+deviceId+"失败error:", err);
|
|
|
+ LOG_WECHAT_ERROR($code, "断开蓝牙连接"+deviceId+"失败error:", JSON.stringify(err));
|
|
|
},
|
|
|
complete() {
|
|
|
//$this.device = {};
|
|
@@ -608,20 +630,6 @@ export default {
|
|
|
});
|
|
|
},500);
|
|
|
},
|
|
|
- /**
|
|
|
- * 获取当前时间
|
|
|
- */
|
|
|
- getNowTime() {
|
|
|
- const date = new Date();
|
|
|
- const year = date.getFullYear();
|
|
|
- const month = date.getMonth() + 1;
|
|
|
- const day = date.getDate();
|
|
|
- const hour = date.getHours();
|
|
|
- const minutes = date.getMinutes();
|
|
|
- const seconds = date.getSeconds();
|
|
|
- const millSeconds = date.getMilliseconds();
|
|
|
- return `${year}/${month}/${day} ${hour}:${minutes}:${seconds}.${millSeconds}`;
|
|
|
- },
|
|
|
connectionError(errCode) {
|
|
|
if (errCode === 10000) {
|
|
|
return "未初始化蓝牙适配器";
|