|
@@ -1,6 +1,7 @@
|
|
|
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"
|
|
@@ -8,28 +9,28 @@ export default {
|
|
|
|
|
|
// 打开大包数据
|
|
|
sendOpenBigData() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 ff");
|
|
|
+ this.SendOrder('ff')
|
|
|
},
|
|
|
// 关闭大包数据
|
|
|
sendCloseBigData() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 00");
|
|
|
+ this.SendOrder('00')
|
|
|
},
|
|
|
//获取当前连接的教具类型
|
|
|
sendGetToyType() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 06");
|
|
|
+ this.SendOrder('06')
|
|
|
},
|
|
|
// 开启脑控
|
|
|
sendControl() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 07");
|
|
|
+ this.SendOrder('07')
|
|
|
},
|
|
|
//暂停脑控
|
|
|
sendControlPause() {
|
|
|
//AA CC 03 00 00 00 09 F3
|
|
|
- this.WriteBufferInBle("03 00 00 00 08");
|
|
|
+ this.SendOrder('08')
|
|
|
},
|
|
|
// 关闭脑控
|
|
|
sendControlClose() {
|
|
|
- this.WriteBufferInBle("03 00 00 00 09");
|
|
|
+ this.SendOrder('09')
|
|
|
},
|
|
|
//发送一对多连接
|
|
|
sendConnectOneToMore(id) {
|
|
@@ -114,22 +115,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) {
|
|
@@ -138,18 +130,69 @@ 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);
|
|
|
-
|
|
|
|
|
|
+ } else {
|
|
|
+ //断开玩具连接
|
|
|
+ try {
|
|
|
+ $this.change_toy_connect_status(0);
|
|
|
+ //断开蓝牙连接
|
|
|
+ $this.change_device_status(0);
|
|
|
+ } catch (e) {
|
|
|
+ console.log("方法不存在");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+ //重新连接蓝牙
|
|
|
+ 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;
|
|
@@ -208,6 +251,7 @@ export default {
|
|
|
characteristicId: item.uuid,
|
|
|
state: true,
|
|
|
});
|
|
|
+ game_store.setters.setReadCid(item.uuid);
|
|
|
}
|
|
|
}
|
|
|
},
|
|
@@ -217,7 +261,6 @@ export default {
|
|
|
});
|
|
|
},
|
|
|
|
|
|
-
|
|
|
// ArrayBuffer转16进度字符串示例
|
|
|
ab2hex(buffer) {
|
|
|
var hexArr = Array.prototype.map.call(
|
|
@@ -233,9 +276,9 @@ export default {
|
|
|
const that = this;
|
|
|
wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
let hexStr = that.ab2hex(characteristic.value);
|
|
|
- if (hexStr.toUpperCase().indexOf("AADD") != -1) {
|
|
|
- console.log(hexStr);
|
|
|
- }
|
|
|
+ // if (hexStr.toUpperCase().indexOf("AADD") != -1) {
|
|
|
+ console.log(hexStr);
|
|
|
+ // }
|
|
|
let $data = that.get_big_data(hexStr);
|
|
|
let $game_status = game_store.getters.getGameStatus();
|
|
|
|
|
@@ -306,6 +349,51 @@ export default {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ //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)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 监听脑环电量
|
|
|
let $power = that.get_device_elc(hexStr);
|
|
|
if ($power) {
|
|
@@ -461,5 +549,16 @@ export default {
|
|
|
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;
|
|
|
+ },
|
|
|
};
|