|
@@ -1,4 +1,20 @@
|
|
|
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";
|
|
|
+
|
|
|
+
|
|
|
+let current_toy_id = "00";
|
|
|
+
|
|
|
+let connect_toy = true;
|
|
|
+let ToyPower = 0;
|
|
|
+
|
|
|
+function getDeviceInfo() {
|
|
|
+ let deviceId = ble_store.getters.getDeviceId();
|
|
|
+ let serviceId = ble_store.getters.getServiceId();
|
|
|
+ let characteristicId = ble_store.getters.getCharacteristicId();
|
|
|
+ return { deviceId, serviceId, characteristicId };
|
|
|
+}
|
|
|
|
|
|
export default {
|
|
|
|
|
@@ -22,7 +38,7 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
- fail(res) {
|
|
|
+ fail() {
|
|
|
let deviceId = wx.getStorageSync('deviceId');
|
|
|
|
|
|
wx.closeBLEConnection({
|
|
@@ -36,6 +52,7 @@ export default {
|
|
|
* 获取蓝牙设备某个服务中所有特征值
|
|
|
*/
|
|
|
getBLEDeviceCharacteristics(deviceId, serviceId) {
|
|
|
+ const that = this;
|
|
|
wx.getBLEDeviceCharacteristics({
|
|
|
deviceId,
|
|
|
serviceId,
|
|
@@ -54,12 +71,16 @@ export default {
|
|
|
}
|
|
|
if (item.properties.notify || item.properties.indicate) {
|
|
|
console.log("第" + (i + 1) + ",该特征值可监听:" + item.uuid);
|
|
|
-
|
|
|
+
|
|
|
wx.notifyBLECharacteristicValueChange({
|
|
|
- deviceId,
|
|
|
- serviceId,
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: serviceId,
|
|
|
characteristicId: item.uuid,
|
|
|
state: true,
|
|
|
+ success() {
|
|
|
+ that.notifyDatas(null);
|
|
|
+ console.log("正在监听特征值:", item.uuid);
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
}
|
|
@@ -103,9 +124,7 @@ export default {
|
|
|
* @constructor
|
|
|
*/
|
|
|
WriteBufferInBle($hex, $buffer_len = 8) {
|
|
|
- let deviceId = ble_store.getters.getDeviceId();
|
|
|
- let serviceId = ble_store.getters.getServiceId();
|
|
|
- let characteristicId = ble_store.getters.getCharacteristicId()
|
|
|
+ let { deviceId, serviceId, characteristicId } = getDeviceInfo();
|
|
|
if (deviceId && serviceId && characteristicId) {
|
|
|
let that = this;
|
|
|
let $hex_header = "aa cc ";
|
|
@@ -136,4 +155,391 @@ export default {
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
+
|
|
|
+ * 监听脑机数据
|
|
|
+ * @param $this
|
|
|
+ * @param characteristicId
|
|
|
+ */
|
|
|
+ notifyDatas($this) {
|
|
|
+ const that = this;
|
|
|
+ let deviceId = ble_store.getters.getDeviceId();
|
|
|
+
|
|
|
+ wx.onBLECharacteristicValueChange((characteristic) => {
|
|
|
+ let hexStr = that.ab2hex(characteristic.value);
|
|
|
+ console.log("监听脑机数据:", hexStr, "当前页面名称:", $this?$this.$options.name:"");
|
|
|
+
|
|
|
+ if (hexStr.substring(0, 6) === "555520") {
|
|
|
+ $this.device_bg = (hexStr.substring(8, 10) === "00");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hexStr.substring(0, 8) === "55550203") {
|
|
|
+ let $power = parseInt(hexStr.substr(8, 2), 16);
|
|
|
+ let $voltage = parseInt(hexStr.substr(10, 2), 16);
|
|
|
+
|
|
|
+ $this.hasUsb = $voltage.toString().substr(0, 1) === "1";
|
|
|
+
|
|
|
+ if ($power) {
|
|
|
+ $this.device_power = $power;
|
|
|
+ $this.device_voltage = $voltage;
|
|
|
+ }
|
|
|
+ if ($power < 10 && $power > 0) {
|
|
|
+ wx.showToast({ title: "脑机电量不足", icon: "none", duration: 2000});
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD0A") >= 0) {
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD0A0000") >= 0) {
|
|
|
+ $this.connect_toy = 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) {
|
|
|
+ console.log("一对多")
|
|
|
+
|
|
|
+ that.sendConnectOneToOne($toy_id);
|
|
|
+ current_toy_id = $toy_id;
|
|
|
+ }
|
|
|
+ if (new RegExp("01").test($hex_index) === true) {
|
|
|
+ console.log("一对一")
|
|
|
+ wx.showToast({title: "已连接到" + $this.toy_item.name });
|
|
|
+ $this.toy_connected = true;
|
|
|
+
|
|
|
+ $this.connect_toy = 2;
|
|
|
+
|
|
|
+
|
|
|
+ if ($toy_id !== "80") {
|
|
|
+ setTimeout(() => {
|
|
|
+ that.SendOrder('87')
|
|
|
+ }, 3000)
|
|
|
+
|
|
|
+ let toy_interval = setInterval(() => {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if($game_status === 1){
|
|
|
+ clearInterval(toy_interval);
|
|
|
+ } else{
|
|
|
+ that.SendOrder('8a');
|
|
|
+ }
|
|
|
+ }, 8000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD87") >= 0) {
|
|
|
+ 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", '12': "JM", '13': "QM"}
|
|
|
+ let $sn = toy_list_pre[current_toy_id] + $number;
|
|
|
+ $this.toy_sn = $sn;
|
|
|
+
|
|
|
+ game_store.setters.setToySn($sn);
|
|
|
+ console.log("获取教具名称hexStr:",hexStr,",获取教具名称$sn",$sn);
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE87") >= 0) {
|
|
|
+
|
|
|
+ if (current_toy_id !== '80') {
|
|
|
+ that.SendOrder('87')
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD8A") >= 0) {
|
|
|
+ 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)
|
|
|
+ connect_toy = true;
|
|
|
+
|
|
|
+ if ($power > 0) {
|
|
|
+ $this.toy_power = $power
|
|
|
+ $this.toy_voltage = $voltage
|
|
|
+ ToyPower = $power;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (game_store.getters.getGameStatus() === 1) {
|
|
|
+ wx.getBLEDeviceRSSI({
|
|
|
+ deviceId: deviceId,
|
|
|
+ success(res) {$this.RSSI = res.RSSI;}
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD") >= 0 || hexStr.toUpperCase().indexOf("AAEE") >= 0 && update_state == false) {
|
|
|
+ let nowTime = Math.round(new Date() / 1000);
|
|
|
+ console.log("应答", hexStr, '时间:' + that.getNowTime(), "长度", hexStr.length / 2);
|
|
|
+ LogInDb(`${that.getNowTime()} 应答:${hexStr}`)
|
|
|
+ }
|
|
|
+ let $data = that.get_big_data(hexStr);
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE70") >= 0) {
|
|
|
+ connect_toy = false
|
|
|
+ wx.showModal({
|
|
|
+ content: "教具已断开",
|
|
|
+ success(res) {
|
|
|
+ if (res.confirm) {
|
|
|
+
|
|
|
+ if ($game_status == 1) {
|
|
|
+ $this.game_finished();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE8A0000005A") >= 0) {
|
|
|
+ that.SendOrder("8a")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AAEE07") >= 0) {
|
|
|
+ that.sendControl();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD84") >= 0) {
|
|
|
+ let $hex_index = hexStr.toUpperCase().indexOf("AADD84") + 6;
|
|
|
+ let $datas = hexStr.substr($hex_index, 10);
|
|
|
+ if ($datas != "0000000000") {
|
|
|
+ console.log("以获取UUID:" + $datas)
|
|
|
+
|
|
|
+ $this.toy_UUID = $datas;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD09") >= 0) {
|
|
|
+ control_close = true;
|
|
|
+ connect_toy = false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD07") >= 0) {
|
|
|
+ ble_store.setters.setBluetoothLinkStatus(true)
|
|
|
+ FlagOpenControl = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if ($game_status == 1 && $data) {
|
|
|
+ $this.do_datas($data);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.substr(0, 6) == "5555e2") {
|
|
|
+ let _buffer = that.doOriginBufferToData(hexStr)
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.setStorageSync('origin_buffer', wx.getStorageSync('origin_buffer') + _buffer.join(',') + ",")
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD04") >= 0) {
|
|
|
+ raw_data_open = true;
|
|
|
+ that.Send2BOrder(true)
|
|
|
+ wx.setStorageSync('origin_buffer', "");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD5A00000000A5") >= 0) {
|
|
|
+ Notify({
|
|
|
+ type: 'danger',
|
|
|
+ duration: 0,
|
|
|
+ message: '智脑机已关机,训练结束',
|
|
|
+ onOpened() {
|
|
|
+ setTimeout(() => {
|
|
|
+ $this.game_finished();
|
|
|
+ clearInterval(control_close_intv)
|
|
|
+ }, 2000)
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ * todo:监听蓝牙连接状态
|
|
|
+ * 监听蓝牙低功耗连接状态改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等
|
|
|
+ */
|
|
|
+ watchBLEstatus($this) {
|
|
|
+ let that = this;
|
|
|
+
|
|
|
+ wx.onBLEConnectionStateChange((res) => {
|
|
|
+
|
|
|
+ ble_store.setters.setBluetoothLinkStatus(res.connected);
|
|
|
+ LogInDb(`${that.getNowTime()} 监听脑机断连`)
|
|
|
+ if (res.connected == false) {
|
|
|
+
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+
|
|
|
+ Notify({
|
|
|
+ type: 'danger',
|
|
|
+ duration: 0,
|
|
|
+ message: '智脑机已断开连接,正在尝试重新连接',
|
|
|
+ onOpened() {
|
|
|
+ that.reconnect(res.deviceId, $this)
|
|
|
+ }
|
|
|
+ });
|
|
|
+ control_close = true
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ connect_toy = false;
|
|
|
+ game_store.setters.setGameStatus(0);
|
|
|
+
|
|
|
+ ble_store.setters.clearDeviceToy();
|
|
|
+ $this.connect_toy = 0;
|
|
|
+ $this.connect_show = false;
|
|
|
+ $this.device_bg = false;
|
|
|
+ $this.device_status = 0;
|
|
|
+ $this.device = {}
|
|
|
+ $this.toy_UUID = "";
|
|
|
+ $this.$forceUpdate();
|
|
|
+ wx.closeBluetoothAdapter();
|
|
|
+ version = "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ * todo:重新连接蓝牙
|
|
|
+ */
|
|
|
+ reconnect($deviceId, $this) {
|
|
|
+ let that = this;
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
+
|
|
|
+ let $connect_count = 0;
|
|
|
+ let $rec = setInterval(() => {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.createBLEConnection({
|
|
|
+ deviceId: $deviceInfo.deviceId,
|
|
|
+ success(res) {
|
|
|
+ clearInterval($rec)
|
|
|
+ Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
|
|
|
+ LogInDb(`${that.getNowTime()} 第${$connect_count}次重新连接成功`)
|
|
|
+ let $system = wx.getSystemInfoSync()
|
|
|
+ if ($system.platform == 'ios') {
|
|
|
+ that.getBLEDeviceServices($deviceInfo.deviceId)
|
|
|
+ that.notifyDatas($this)
|
|
|
+ } else {
|
|
|
+ that.openNotify($this)
|
|
|
+ that.watchBLEstatus($this);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
|
|
|
+ game_store.setters.setGameCloseStatus(1);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if ($connect_count >= 3) {
|
|
|
+ $this.game_finished();
|
|
|
+ clearInterval($rec)
|
|
|
+ }
|
|
|
+ $connect_count += 1;
|
|
|
+ } else {
|
|
|
+ clearInterval($rec)
|
|
|
+ }
|
|
|
+ }, 7000)
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ * 关闭脑机蓝牙连接
|
|
|
+ */
|
|
|
+ closeConnection($this) {
|
|
|
+ const that = this;
|
|
|
+
|
|
|
+ that.SendOrder("31");
|
|
|
+ setTimeout(()=>{
|
|
|
+ that.SendOrder("09");
|
|
|
+
|
|
|
+ wx.offBluetoothDeviceFound();
|
|
|
+
|
|
|
+ wx.stopBluetoothDevicesDiscovery();
|
|
|
+
|
|
|
+ let deviceId = ble_store.getters.getDeviceId();
|
|
|
+
|
|
|
+ wx.closeBLEConnection({
|
|
|
+ deviceId: deviceId,
|
|
|
+ success() {
|
|
|
+ Toast.success({
|
|
|
+ message: "断开蓝牙连接成功",
|
|
|
+ });
|
|
|
+ ble_store.setters.clearDeviceToy();
|
|
|
+ wx.closeBluetoothAdapter();
|
|
|
+ console.log("断开蓝牙连接成功", deviceId);
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.log("断开蓝牙连接"+deviceId+"失败error:", err);
|
|
|
+ },
|
|
|
+ complete() {
|
|
|
+ $this.device = {};
|
|
|
+ $this.toy_UUID = "";
|
|
|
+ $this.$forceUpdate();
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },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}`;
|
|
|
+ },
|
|
|
};
|