|
@@ -6,13 +6,48 @@ import {setDeviceInDb} from "../requests/game";
|
|
|
var $ff = "ff";
|
|
|
var $8f = "ffffffff"
|
|
|
var control_close = false
|
|
|
-
|
|
|
+
|
|
|
var current_device_mac = "";
|
|
|
-
|
|
|
-var connect_toy = false;
|
|
|
+
|
|
|
+var connect_toy = true;
|
|
|
+
|
|
|
var current_toy_id = "00";
|
|
|
+
|
|
|
var current_toy_UUID = "";
|
|
|
|
|
|
+
|
|
|
+var FlagOpenControl = false;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+var services = [
|
|
|
+ "6e400001-b5a3-f393-e0a9-e50e24dcca9e",
|
|
|
+ "0000fff0-0000-1000-8000-00805f9b34fb"
|
|
|
+]
|
|
|
+var charateristics = [
|
|
|
+ {
|
|
|
+ notify: "6e400003-b5a3-f393-e0a9-e50e24dcca9e",
|
|
|
+ write: "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
|
|
|
+ },
|
|
|
+ {
|
|
|
+ notify: "0000fff1-0000-1000-8000-00805f9b34fb",
|
|
|
+ write: "0000fff2-0000-1000-8000-00805f9b34fb"
|
|
|
+ },
|
|
|
+]
|
|
|
+
|
|
|
+function getServicesAndCharateristc() {
|
|
|
+
|
|
|
+ let $brainSn = game_store.getters.getDeviceSn();
|
|
|
+ let $deviceId = game_store.getters.getDeviceId();
|
|
|
+ let $index = $brainSn.toUpperCase().indexOf("JELLYFISH") != -1 ? 0 : 1;
|
|
|
+ return {
|
|
|
+ deviceId:$deviceId,
|
|
|
+ service:services[$index],
|
|
|
+ charateristic:charateristics[$index]
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export default {
|
|
|
|
|
|
connect_toy, control_close, current_toy_id, current_toy_UUID,
|
|
@@ -31,20 +66,25 @@ export default {
|
|
|
},
|
|
|
|
|
|
sendControl() {
|
|
|
- this.SendOrder('07')
|
|
|
- },
|
|
|
-
|
|
|
- sendControlPause() {
|
|
|
-
|
|
|
- this.SendOrder('08')
|
|
|
+ let that = this;
|
|
|
+ let $SendControlIntv = setInterval(() => {
|
|
|
+ if (FlagOpenControl) {
|
|
|
+ clearInterval($SendControlIntv)
|
|
|
+ } else {
|
|
|
+ that.SendOrder('07')
|
|
|
+ }
|
|
|
+ }, 1000);
|
|
|
},
|
|
|
|
|
|
sendControlClose() {
|
|
|
let that = this
|
|
|
+ control_close = false;
|
|
|
let $intv = setInterval(() => {
|
|
|
if (!control_close) {
|
|
|
that.SendOrder('09')
|
|
|
} else {
|
|
|
+
|
|
|
+ FlagOpenControl = false;
|
|
|
clearInterval($intv)
|
|
|
|
|
|
that.clearLocalDatas()
|
|
@@ -64,27 +104,23 @@ export default {
|
|
|
|
|
|
sendToyPower_CheckReconected() {
|
|
|
let that = this;
|
|
|
- let $connect_false_count = 0;
|
|
|
- let $game_status = game_store.getters.getGameStatus();
|
|
|
- if ($game_status == 1) {
|
|
|
- let toy_intv = setInterval(() => {
|
|
|
- if (connect_toy) {
|
|
|
- that.SendOrder('8a')
|
|
|
- connect_toy = false
|
|
|
- $connect_false_count = 0
|
|
|
- } else {
|
|
|
- $connect_false_count += 1;
|
|
|
- console.log("循环数:" + $connect_false_count)
|
|
|
- if ($connect_false_count == 2) {
|
|
|
-
|
|
|
- connect_toy = false;
|
|
|
- that.ReconnectToy()
|
|
|
- clearInterval(toy_intv)
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- }, 3000)
|
|
|
- }
|
|
|
+ let toy_intv = setInterval(() => {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ that.SendOrder('8a')
|
|
|
+
|
|
|
+ }
|
|
|
+ }, 3000)
|
|
|
},
|
|
|
|
|
|
|
|
@@ -181,9 +217,22 @@ export default {
|
|
|
that.reconnect(res.deviceId, $this)
|
|
|
}
|
|
|
});
|
|
|
+ control_close = true
|
|
|
|
|
|
|
|
|
-
|
|
|
+ } else {
|
|
|
+ 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();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -192,109 +241,119 @@ export default {
|
|
|
|
|
|
reconnect($deviceId, $this) {
|
|
|
let that = this;
|
|
|
- let $serviceId = game_store.getters.getServiceId();
|
|
|
- let $characteristicId = game_store.getters.getReadCharacterId();
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
|
|
|
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)
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.createBLEConnection({
|
|
|
+ deviceId: $deviceInfo.deviceId,
|
|
|
+ success(res) {
|
|
|
+ Notify({type: 'success', message: `第${$connect_count}次重新连接成功`});
|
|
|
+ that.openNotify($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)
|
|
|
- },
|
|
|
- fail(res) {
|
|
|
- Notify({type: 'danger', message: `第${$connect_count}次重新连接失败`});
|
|
|
- game_store.setters.setGameCloseStatus(1);
|
|
|
}
|
|
|
- })
|
|
|
- if ($connect_count == 3) {
|
|
|
- $this.game_finished();
|
|
|
+ } else {
|
|
|
+ clearInterval($rec)
|
|
|
}
|
|
|
|
|
|
- }, 5000)
|
|
|
|
|
|
- },
|
|
|
+ }, 5000)
|
|
|
|
|
|
-
|
|
|
- getBLEDeviceServices(deviceId) {
|
|
|
- const $this = this;
|
|
|
- current_device_mac = deviceId
|
|
|
- wx.getBLEDeviceServices({
|
|
|
- deviceId,
|
|
|
- success: (res) => {
|
|
|
- for (let i = 0; i < res.services.length; i++) {
|
|
|
- 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);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- fail(res) {
|
|
|
- console.log("连接蓝牙成功,获取服务失败");
|
|
|
- },
|
|
|
- });
|
|
|
},
|
|
|
|
|
|
-
|
|
|
- getBLEDeviceCharacteristics(deviceId, serviceId) {
|
|
|
- console.log(deviceId, serviceId);
|
|
|
- const $this = this;
|
|
|
- wx.getBLEDeviceCharacteristics({
|
|
|
- deviceId,
|
|
|
- serviceId,
|
|
|
- success: (res) => {
|
|
|
- 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.sendOpenBigData();
|
|
|
- }
|
|
|
-
|
|
|
- if (item.properties.notify || item.properties.indicate) {
|
|
|
- wx.notifyBLECharacteristicValueChange({
|
|
|
- deviceId,
|
|
|
- serviceId,
|
|
|
- characteristicId: item.uuid,
|
|
|
- state: true,
|
|
|
- });
|
|
|
- game_store.setters.setReadCid(item.uuid);
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- fail(res) {
|
|
|
- console.error("getBLEDeviceCharacteristics", res);
|
|
|
- },
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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();
|
|
|
+ }
|
|
|
});
|
|
|
},
|
|
|
|
|
@@ -328,26 +387,37 @@ export default {
|
|
|
$this.change_toy_connect_status(3);
|
|
|
return false;
|
|
|
}
|
|
|
- let $hexAry = $this.current_hex.split(" ")
|
|
|
-
|
|
|
-
|
|
|
+ let $hex_index = hexStr.substr(hexStr.length - 4, 2)
|
|
|
+ let $toy_id = hexStr.substr(8, 2)
|
|
|
+ console.log("连接", $hex_index)
|
|
|
+ console.log("玩具", $toy_id)
|
|
|
|
|
|
- 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);
|
|
|
-
|
|
|
- that.SendOrder('8a')
|
|
|
- }
|
|
|
+ if (new RegExp("00").test($hex_index) == true) {
|
|
|
+ that.sendConnectOneToOne($toy_id)
|
|
|
+ $this.current_hex = `03 00 ${$toy_id} 01 0A`
|
|
|
+ console.log("一对多")
|
|
|
+ }
|
|
|
+ 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('8a')
|
|
|
+ }
|
|
|
+
|
|
|
+ if (new RegExp("02").test($hex_index) == true) {
|
|
|
+
|
|
|
+ connect_toy = true
|
|
|
+ wx.hideLoading()
|
|
|
+ wx.showToast({
|
|
|
+ title: "教具重连成功"
|
|
|
+ })
|
|
|
+ that.sendControl()
|
|
|
+ that.sendToyPower_CheckReconected();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -362,7 +432,12 @@ export default {
|
|
|
console.log("电压:" + $voltage / 10)
|
|
|
$this.toy_power = $power;
|
|
|
|
|
|
- connect_toy = true;
|
|
|
+ if ($power != 0) {
|
|
|
+ connect_toy = true;
|
|
|
+ }
|
|
|
+ if (hexStr.toUpperCase().indexOf("FF") != -1) {
|
|
|
+ connect_toy = false;
|
|
|
+ }
|
|
|
if (current_toy_UUID == "") {
|
|
|
|
|
|
that.SendOrder('84')
|
|
@@ -445,12 +520,12 @@ export default {
|
|
|
|
|
|
that.WriteBufferInBle(`03 00 ${current_toy_id} 02 0A`)
|
|
|
}
|
|
|
- if (hexStr.toUpperCase().indexOf("AADD0A02") != -1) {
|
|
|
-
|
|
|
- connect_toy = true
|
|
|
- wx.showToast({
|
|
|
- title: "教具重连成功"
|
|
|
- })
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (hexStr.toUpperCase().indexOf("AADD07") != -1) {
|
|
|
+ FlagOpenControl = true;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -557,9 +632,7 @@ export default {
|
|
|
},
|
|
|
|
|
|
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();
|
|
|
+ let $deviceInfo = getServicesAndCharateristc();
|
|
|
let $hex_header = "aa cc ";
|
|
|
|
|
|
|
|
@@ -580,9 +653,9 @@ 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 + ',写入成功')
|
|
@@ -628,6 +701,7 @@ export default {
|
|
|
|
|
|
|
|
|
ReconnectToy() {
|
|
|
+ let $game_status = game_store.getters.getGameStatus();
|
|
|
let that = this;
|
|
|
try {
|
|
|
|
|
@@ -648,20 +722,33 @@ export default {
|
|
|
sum = sum.substr(0, sum.length - 1);
|
|
|
let $hexStr = `03 ff ${sum} 8e`;
|
|
|
let loop_count = "";
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- that.WriteBufferInBle($hexStr, 16)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ if ($game_status == 1) {
|
|
|
+ wx.showLoading({
|
|
|
+ title: "重新连接教具中"
|
|
|
+ })
|
|
|
+ }
|
|
|
+ let connect_toy_intv = setInterval(() => {
|
|
|
+ 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)
|