bluetooth.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. import game_store from "../store/game";
  2. import ble_store from "../store/bluetooth";
  3. import bluetooth from "../store/bluetooth";
  4. export default {
  5. // 发送开启脑电数据包指令
  6. sendOpen(deviceId, serviceId, Cid) {
  7. // 向蓝牙设备发送一个0x00的16进制数据
  8. // 玩具对应指令数组
  9. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0xff) ^ 0xffffffff) & 0xff;
  10. let $CheckSum = "0x" + CheckSum.toString(16);
  11. let buffer = new ArrayBuffer(8);
  12. let dataView = new DataView(buffer);
  13. dataView.setUint8(0, "0xaa");
  14. dataView.setUint8(1, "0xcc");
  15. dataView.setUint8(2, "0x03");
  16. dataView.setUint8(3, "0x00");
  17. dataView.setUint8(4, "0x00");
  18. dataView.setUint8(5, "0x00");
  19. dataView.setUint8(6, "0xff");
  20. dataView.setUint8(7, $CheckSum);
  21. // 向低功耗蓝牙设备特征值中写入二进制数据
  22. wx.writeBLECharacteristicValue({
  23. deviceId: deviceId,
  24. serviceId: serviceId,
  25. characteristicId: Cid,
  26. value: buffer,
  27. success: function (res) {
  28. // console.log('开启脑电数据包成功')
  29. // console.log(res)
  30. },
  31. fail: function (err) {
  32. console.log("开启脑电数据包失败");
  33. console.log(err);
  34. },
  35. });
  36. },
  37. // 发送开启教具脑控指令
  38. sendControl(deviceId, serviceId, Cid) {
  39. const that = this;
  40. // 玩具对应指令数组
  41. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x07) ^ 0xffffffff) & 0xff;
  42. let $CheckSum = "0x" + CheckSum.toString(16);
  43. // 向蓝牙设备发送一个0x00的16进制数据
  44. let buffer = new ArrayBuffer(8);
  45. let dataView = new DataView(buffer);
  46. dataView.setUint8(0, "0xaa");
  47. dataView.setUint8(1, "0xcc");
  48. dataView.setUint8(2, "0x03");
  49. dataView.setUint8(3, "0x00");
  50. dataView.setUint8(4, "0x00");
  51. dataView.setUint8(5, "0x00");
  52. dataView.setUint8(6, "0x07");
  53. dataView.setUint8(7, $CheckSum);
  54. // 向低功耗蓝牙设备特征值中写入二进制数据
  55. wx.writeBLECharacteristicValue({
  56. deviceId: deviceId,
  57. serviceId: serviceId,
  58. characteristicId: Cid,
  59. value: buffer,
  60. success: function (res) {
  61. // console.log('开启教具脑控成功')
  62. // that.sendOpen()
  63. // console.log(res)
  64. },
  65. fail: function (err) {
  66. console.log("开启教具脑控失败");
  67. console.log(err);
  68. },
  69. });
  70. },
  71. // 发送连接玩具指令
  72. sendConnect($hex, deviceId, serviceId, Cid, Toast, $this) {
  73. //获取最后两个字节
  74. const that = this;
  75. // 玩具对应指令数组
  76. let arr = [
  77. "0x01",
  78. "0x02",
  79. "0x03",
  80. "0x04",
  81. "0x05",
  82. "0x06",
  83. "0x07",
  84. "0x08",
  85. "0x09",
  86. "0x0a",
  87. "0x0b",
  88. ];
  89. let arr1 = [
  90. 0x01,
  91. 0x02,
  92. 0x03,
  93. 0x04,
  94. 0x05,
  95. 0x06,
  96. 0x07,
  97. 0x08,
  98. 0x09,
  99. 0x0a,
  100. 0x0b,
  101. ];
  102. let $index = arr.indexOf($hex);
  103. let CheckSum =
  104. ((0x03 + 0x00 + arr1[$index] + 0x00 + 0x0a) ^ 0xffffffff) & 0xff;
  105. let $CheckSum = "0x" + CheckSum.toString(16);
  106. // 向蓝牙设备发送指令
  107. let buffer = new ArrayBuffer(8);
  108. let dataView = new DataView(buffer);
  109. dataView.setUint8(0, "0xaa");
  110. dataView.setUint8(1, "0xcc");
  111. dataView.setUint8(2, "0x03");
  112. dataView.setUint8(3, "0x00");
  113. dataView.setUint8(4, arr[$index]);
  114. dataView.setUint8(5, "0x00");
  115. dataView.setUint8(6, "0x0a");
  116. dataView.setUint8(7, $CheckSum);
  117. // 向低功耗蓝牙设备特征值中写入二进制数据
  118. wx.writeBLECharacteristicValue({
  119. deviceId: deviceId,
  120. serviceId: serviceId,
  121. characteristicId: Cid,
  122. value: buffer,
  123. success: function (res) {
  124. console.log("发送连接玩具指令成功");
  125. // wx.showLoading({
  126. // title: '正在创建连接',
  127. // })
  128. that.watching_toy($this, deviceId, serviceId, Cid, Toast);
  129. let $game_status = game_store.getters.getGameStatus();
  130. if ($game_status == 1) {
  131. Toast.loading({
  132. duration: 0,
  133. message: "正在创建连接",
  134. forbidClick: true,
  135. });
  136. }
  137. },
  138. fail: function (err) {
  139. console.log("发送连接玩具指令失败");
  140. Toast.fail({
  141. message: "玩具连接失败",
  142. });
  143. console.log(err);
  144. },
  145. });
  146. },
  147. //发送断开玩具的指令
  148. sendEnd(deviceId, serviceId, Cid) {
  149. //AA CC 03 00 00 00 09 F3
  150. // 玩具对应指令数组
  151. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x09) ^ 0xffffffff) & 0xff;
  152. let $CheckSum = "0x" + CheckSum.toString(16);
  153. let buffer = new ArrayBuffer(8);
  154. let dataView = new DataView(buffer);
  155. dataView.setUint8(0, "0xaa");
  156. dataView.setUint8(1, "0xcc");
  157. dataView.setUint8(2, "0x03");
  158. dataView.setUint8(3, "0x00");
  159. dataView.setUint8(4, "0x00");
  160. dataView.setUint8(5, "0x00");
  161. dataView.setUint8(6, "0x09");
  162. dataView.setUint8(7, $CheckSum);
  163. // 向低功耗蓝牙设备特征值中写入二进制数据
  164. wx.writeBLECharacteristicValue({
  165. deviceId: deviceId,
  166. serviceId: serviceId,
  167. characteristicId: Cid,
  168. value: buffer,
  169. success: function (res) {
  170. console.log("断开玩具成功");
  171. // that.sendOpen()
  172. console.log(res);
  173. },
  174. fail: function (err) {
  175. console.log("断开玩具失败");
  176. console.log(err);
  177. },
  178. });
  179. },
  180. // 发送停止发送脑电数据包指令
  181. sendPause(deviceId, serviceId, Cid) {
  182. let that = this;
  183. // 玩具对应指令数组
  184. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x08) ^ 0xffffffff) & 0xff;
  185. let $CheckSum = "0x" + CheckSum.toString(16);
  186. // 向蓝牙设备发送一个0x00的16进制数据
  187. let buffer = new ArrayBuffer(8);
  188. let dataView = new DataView(buffer);
  189. dataView.setUint8(0, "0xaa");
  190. dataView.setUint8(1, "0xcc");
  191. dataView.setUint8(2, "0x03");
  192. dataView.setUint8(3, "0x00");
  193. dataView.setUint8(4, "0x00");
  194. dataView.setUint8(5, "0x00");
  195. dataView.setUint8(6, "0x08");
  196. dataView.setUint8(7, $CheckSum);
  197. // 向低功耗蓝牙设备特征值中写入二进制数据
  198. wx.writeBLECharacteristicValue({
  199. deviceId: deviceId,
  200. serviceId: serviceId,
  201. characteristicId: Cid,
  202. value: buffer,
  203. success: function (res) {
  204. console.log("停止数据发送成功");
  205. console.log(res);
  206. },
  207. fail: function (err) {
  208. console.log("停止数据发送失败");
  209. console.log(err);
  210. },
  211. });
  212. },
  213. // 通过字节码获取玩具
  214. connect_toy(hexStr, deviceId, serviceId, Cid, Toast) {
  215. console.log("开始连接玩具");
  216. let $flag = "";
  217. let $toys = [
  218. {
  219. id: 1,
  220. hex: "aadd0a0000",
  221. msg: "无连接设备",
  222. flag: false,
  223. },
  224. {
  225. id: 1,
  226. hex: "aadd0a0001",
  227. msg: "已连接到智脑水舞",
  228. flag: true,
  229. },
  230. {
  231. id: 2,
  232. hex: "aadd0a0002",
  233. msg: "已连接到智脑恐龙",
  234. flag: true,
  235. },
  236. {
  237. id: 3,
  238. hex: "aadd0a0003",
  239. msg: "已连接到喷雾恐龙(小)",
  240. flag: true,
  241. },
  242. {
  243. id: 4,
  244. hex: "aadd0a0004",
  245. msg: "已连接到智脑赛车",
  246. flag: true,
  247. },
  248. {
  249. id: 5,
  250. hex: "aadd0a0005",
  251. msg: "已连接到智脑碰碰车",
  252. flag: true,
  253. },
  254. {
  255. id: 6,
  256. hex: "aadd0a0006",
  257. msg: "已连接到智脑SUV赛车",
  258. flag: true,
  259. },
  260. {
  261. id: 7,
  262. hex: "aadd0a0007",
  263. msg: "已连接到小车(中)",
  264. flag: true,
  265. },
  266. {
  267. id: 8,
  268. hex: "aadd0a0008",
  269. msg: "已连接到小车(小)",
  270. flag: true,
  271. },
  272. {
  273. id: 9,
  274. hex: "aadd0a0009",
  275. msg: "已连接到智脑无人机",
  276. flag: true,
  277. },
  278. {
  279. id: 10,
  280. hex: "aadd0a000a",
  281. msg: "已连接到飞行器(小)",
  282. flag: true,
  283. },
  284. {
  285. id: 11,
  286. hex: "aadd0a000b",
  287. msg: "已连接到水母灯",
  288. flag: true,
  289. },
  290. {
  291. id: 12,
  292. hex: "aadd070000",
  293. msg: "无连接设备",
  294. flag: false,
  295. },
  296. ];
  297. $toys.forEach(($val, $index) => {
  298. if (hexStr.substring(0, 10) == $val["hex"]) {
  299. Toast.clear();
  300. if ($val["flag"]) {
  301. Toast.success($val["msg"]);
  302. let $game_status = game_store.getters.getGameStatus();
  303. if ($game_status == 1) {
  304. this.sendControl(deviceId, serviceId, Cid);
  305. }
  306. } else {
  307. if ($val["hex"] == "aadd070000") {
  308. this.sendOpen(deviceId, serviceId, Cid);
  309. } else {
  310. Toast.fail($val["msg"]);
  311. }
  312. }
  313. $flag = $val["flag"];
  314. }
  315. });
  316. return $flag;
  317. },
  318. // 获取大包数据 进行绘制图表
  319. get_big_data(hex) {
  320. if (hex.substr(0, 6) != "555520") {
  321. return false;
  322. }
  323. //当s1为 00时 数据有效
  324. let $s1 = hex.substr(8, 2);
  325. if ($s1 != "00") {
  326. return false;
  327. }
  328. //专注度数据
  329. let $att = parseInt("0x" + hex.substr(12, 2));
  330. //放松度数据
  331. let $med = parseInt("0x" + hex.substr(16, 2));
  332. //Delta数据
  333. let $delta_1 = hex.substr(hex.indexOf("0418") + 4, 2);
  334. let $delta_2 = hex.substr(hex.indexOf("0418") + 6, 2);
  335. let $delta_3 = hex.substr(hex.indexOf("0418") + 8, 2);
  336. let $delta = parseInt(
  337. (("0x" + $delta_1) << 16) | (("0x" + $delta_2) << 8) | ("0x" + $delta_3),
  338. 16
  339. );
  340. //Theta数据
  341. let $theta_1 = hex.substr(hex.indexOf("0418") + 10, 2);
  342. let $theta_2 = hex.substr(hex.indexOf("0418") + 12, 2);
  343. let $theta_3 = hex.substr(hex.indexOf("0418") + 14, 2);
  344. let $theta = parseInt(
  345. (("0x" + $theta_1) << 16) | (("0x" + $theta_2) << 8) | ("0x" + $theta_3),
  346. 16
  347. );
  348. //low_Alpha
  349. let $low_alpha_1 = hex.substr(hex.indexOf("0418") + 16, 2);
  350. let $low_alpha_2 = hex.substr(hex.indexOf("0418") + 18, 2);
  351. let $low_alpha_3 = hex.substr(hex.indexOf("0418") + 20, 2);
  352. let $low_alpha =
  353. (("0x" + $low_alpha_1) << 16) |
  354. (("0x" + $low_alpha_2) << 8) |
  355. ("0x" + $low_alpha_3);
  356. //high_Alpha
  357. let $high_alpha_1 = hex.substr(hex.indexOf("0418") + 22, 2);
  358. let $high_alpha_2 = hex.substr(hex.indexOf("0418") + 24, 2);
  359. let $high_alpha_3 = hex.substr(hex.indexOf("0418") + 26, 2);
  360. let $high_alpha =
  361. (("0x" + $high_alpha_1) << 16) |
  362. (("0x" + $high_alpha_2) << 8) |
  363. ("0x" + $high_alpha_3);
  364. let $alpha = parseInt(($high_alpha << 8) | $low_alpha, 16);
  365. //low_beta
  366. let $low_beta_1 = hex.substr(hex.indexOf("0418") + 28, 2);
  367. let $low_beta_2 = hex.substr(hex.indexOf("0418") + 30, 2);
  368. let $low_beta_3 = hex.substr(hex.indexOf("0418") + 32, 2);
  369. let $low_beta =
  370. (("0x" + $low_beta_1) << 16) |
  371. (("0x" + $low_beta_2) << 8) |
  372. ("0x" + $low_beta_3);
  373. //high_beta
  374. let $high_beta_1 = hex.substr(hex.indexOf("0418") + 34, 2);
  375. let $high_beta_2 = hex.substr(hex.indexOf("0418") + 36, 2);
  376. let $high_beta_3 = hex.substr(hex.indexOf("0418") + 38, 2);
  377. let $high_beta =
  378. (("0x" + $low_beta_1) << 16) |
  379. (("0x" + $low_beta_2) << 8) |
  380. ("0x" + $low_beta_3);
  381. //Beta数据
  382. let $beta = parseInt(($high_beta << 8) | $low_beta, 16);
  383. return {
  384. att: $att,
  385. med: $med,
  386. delta: $delta,
  387. theta: $theta,
  388. alpha: $alpha,
  389. beta: $beta,
  390. };
  391. },
  392. //获取设备电量
  393. get_device_elc(hex) {
  394. if (hex.substr(0, 8) != "55550203") {
  395. return false;
  396. }
  397. let $power = parseInt("0x" + hex.substr(8, 2));
  398. return $power;
  399. },
  400. // 监听蓝牙连接状态
  401. watch_bluetooth_status($that) {
  402. let that = this;
  403. // 微信自身监听低功耗蓝牙连接状态的改变事件
  404. wx.onBLEConnectionStateChange((res) => {
  405. // 该方法回调中可以用于处理连接意外断开等异常情况
  406. ble_store.setters.setBluetoothLinkStatus(res.connected);
  407. if (res.connected == false) {
  408. //断开玩具连接
  409. try {
  410. $that.change_toy_connect_status(0);
  411. //断开蓝牙连接
  412. $that.change_device_status(0);
  413. } catch (e) {
  414. console.log("方法不存在");
  415. }
  416. //判断游戏是否游戏中
  417. let $game_status = game_store.getters.getGameStatus();
  418. if ($game_status == 1) {
  419. $that.game_finished();
  420. }
  421. }
  422. });
  423. },
  424. //获取蓝牙设备服务
  425. getBLEDeviceServices(deviceId) {
  426. const $this = this;
  427. wx.getBLEDeviceServices({
  428. deviceId,
  429. success: (res) => {
  430. for (let i = 0; i < res.services.length; i++) {
  431. if (res.services[i].isPrimary) {
  432. $this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
  433. return;
  434. }
  435. }
  436. },
  437. fail(res) {
  438. console.log("连接蓝牙成功,获取服务失败");
  439. },
  440. });
  441. },
  442. //获取蓝牙设备某个服务中所有特征值
  443. getBLEDeviceCharacteristics(deviceId, serviceId) {
  444. const $this = this;
  445. wx.getBLEDeviceCharacteristics({
  446. deviceId,
  447. serviceId,
  448. success: (res) => {
  449. console.log("getBLEDeviceCharacteristics success", res.characteristics);
  450. for (let i = 0; i < res.characteristics.length; i++) {
  451. let item = res.characteristics[i];
  452. if (item.properties.read) {
  453. wx.readBLECharacteristicValue({
  454. deviceId,
  455. serviceId,
  456. characteristicId: item.uuid,
  457. });
  458. }
  459. if (item.properties.write) {
  460. $this.deviceId = deviceId;
  461. $this.serviceId = serviceId;
  462. $this.cid = item.uuid;
  463. game_store.setters.setDeviceId(deviceId);
  464. game_store.setters.setServiceId(serviceId);
  465. game_store.setters.setCid(item.uuid);
  466. //打开数据帧
  467. $this.sendOpen(deviceId, serviceId, item.uuid);
  468. // $this.sendPause(deviceId, serviceId, item.uuid)
  469. //获取玩具的值
  470. // bluetooth.sendConnect($this.toy_hex, deviceId, serviceId, item.uuid)
  471. }
  472. if (item.properties.notify || item.properties.indicate) {
  473. wx.notifyBLECharacteristicValueChange({
  474. deviceId,
  475. serviceId,
  476. characteristicId: item.uuid,
  477. state: true,
  478. });
  479. }
  480. }
  481. },
  482. fail(res) {
  483. console.error("getBLEDeviceCharacteristics", res);
  484. },
  485. });
  486. },
  487. //监听玩具得状态
  488. watching_toy($this, deviceId, serviceId, Cid, Toast) {
  489. Toast.loading({
  490. forbidClick: false,
  491. message: "正在创建连接",
  492. });
  493. let that = this;
  494. let $count = 0;
  495. //关闭核销选项窗口
  496. // $this.pay_window = false
  497. wx.onBLECharacteristicValueChange((characteristic) => {
  498. try {
  499. let hexStr = that.ab2hex(characteristic.value);
  500. //连接玩具
  501. let $flag = that.connect_toy(hexStr, deviceId, serviceId, Cid, Toast);
  502. //连接不上设备
  503. if ($flag) {
  504. //连接成功 打开核销窗口
  505. // $this.pay_window = true
  506. $this.change_toy_connect_status(2);
  507. let $game_status = game_store.getters.getGameStatus();
  508. if ($game_status == 1) {
  509. $this.change_toy_connect_status(4);
  510. }
  511. wx.offBLECharacteristicValueChange();
  512. that.watchingDevice($this);
  513. } else {
  514. $count++;
  515. if ($count == 10) {
  516. $this.change_toy_connect_status(3);
  517. wx.offBLECharacteristicValueChange();
  518. that.watchingDevice($this);
  519. }
  520. }
  521. // 取消监听低功耗蓝牙设备
  522. } catch (e) {
  523. wx.offBLECharacteristicValueChange();
  524. that.watchingDevice($this);
  525. }
  526. });
  527. },
  528. // 获取当前连接得玩具
  529. getConnectedToy(deviceId, serviceId, Cid) {
  530. let that = this;
  531. // 玩具对应指令数组
  532. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x06) ^ 0xffffffff) & 0xff;
  533. let $CheckSum = "0x" + CheckSum.toString(16);
  534. // 向蓝牙设备发送一个0x00的16进制数据
  535. let buffer = new ArrayBuffer(8);
  536. let dataView = new DataView(buffer);
  537. dataView.setUint8(0, "0xaa");
  538. dataView.setUint8(1, "0xcc");
  539. dataView.setUint8(2, "0x03");
  540. dataView.setUint8(3, "0x00");
  541. dataView.setUint8(4, "0x00");
  542. dataView.setUint8(5, "0x00");
  543. dataView.setUint8(6, "0x06");
  544. dataView.setUint8(7, $CheckSum);
  545. // 向低功耗蓝牙设备特征值中写入二进制数据
  546. wx.writeBLECharacteristicValue({
  547. deviceId: deviceId,
  548. serviceId: serviceId,
  549. characteristicId: Cid,
  550. value: buffer,
  551. success: function (res) {
  552. console.log("获取当前连接的玩具类型");
  553. console.log(res);
  554. },
  555. fail: function (err) {
  556. console.log("获取当前连接的玩具类型失败");
  557. console.log(err);
  558. },
  559. });
  560. },
  561. // ArrayBuffer转16进度字符串示例
  562. ab2hex(buffer) {
  563. var hexArr = Array.prototype.map.call(
  564. new Uint8Array(buffer),
  565. function (bit) {
  566. return ("00" + bit.toString(16)).slice(-2);
  567. }
  568. );
  569. return hexArr.join("");
  570. },
  571. //断开发送数据
  572. shutdownSendControl(deviceId, serviceId, Cid) {
  573. let that = this;
  574. let CheckSum = ((0x03 + 0x00 + 0x00 + 0x00 + 0x0a) ^ 0xffffffff) & 0xff;
  575. let $CheckSum = "0x" + CheckSum.toString(16);
  576. // 向蓝牙设备发送指令
  577. let buffer = new ArrayBuffer(8);
  578. let dataView = new DataView(buffer);
  579. dataView.setUint8(0, "0xaa");
  580. dataView.setUint8(1, "0xcc");
  581. dataView.setUint8(2, "0x03");
  582. dataView.setUint8(3, "0x00");
  583. dataView.setUint8(4, "0x00");
  584. dataView.setUint8(5, "0x00");
  585. dataView.setUint8(6, "0x0a");
  586. dataView.setUint8(7, $CheckSum);
  587. // 向低功耗蓝牙设备特征值中写入二进制数据
  588. wx.writeBLECharacteristicValue({
  589. deviceId: deviceId,
  590. serviceId: serviceId,
  591. characteristicId: Cid,
  592. value: buffer,
  593. success: function (res) {
  594. console.log("断开连接玩具指令成功");
  595. },
  596. fail: function (err) {
  597. console.log("发送连接玩具指令失败");
  598. console.log(err);
  599. },
  600. });
  601. },
  602. // 监听脑环是否带正
  603. watchingDevice($this) {
  604. const that = this;
  605. wx.onBLECharacteristicValueChange((characteristic) => {
  606. let hexStr = that.ab2hex(characteristic.value);
  607. let $data = that.get_big_data(hexStr);
  608. wx.getBLEDeviceRSSI({
  609. deviceId: characteristic.deviceId,
  610. success(res) {
  611. $this.rssi = Math.abs(res["RSSI"]);
  612. },
  613. });
  614. if (hexStr.substr(0, 6) == "555520") {
  615. //当s1为 00时 数据有效
  616. let $s1 = hexStr.substr(8, 2);
  617. // console.log("监听脑环是否带正:", $s1 == '00')
  618. $this.device_bg = $s1 == "00";
  619. }
  620. // 监听脑环电量
  621. let $power = that.get_device_elc(hexStr);
  622. if ($power) {
  623. // console.log("当前脑环电量:", $power)
  624. $this.device_power = $power;
  625. }
  626. if ($power < 10 && $power > 0) {
  627. wx.showToast({
  628. title: "脑环电量不足",
  629. icon: "none",
  630. success() {
  631. $this.change_device_status(0);
  632. },
  633. });
  634. }
  635. let $game_status = game_store.getters.getGameStatus();
  636. if ($game_status == 1 && $data) {
  637. try {
  638. if ($this.played_time > 0) {
  639. //自定义定时器
  640. $this.played_time -= 1;
  641. console.log('时间自定义每秒减少1~~~ played_time',$this.played_time )
  642. game_store.setters.setPlayedTime($this.played_time)
  643. $this.played_time_text = that.formatPlaySeconds($this.played_time);
  644. $this.do_datas($data);
  645. }
  646. if ($this.played_time == 0) {
  647. //判断是否隐藏 隐藏则不绘画
  648. that.shutdownSendControl(
  649. characteristic.deviceId,
  650. $this._serviceId,
  651. $this._characteristicId
  652. );
  653. let $hide_status = game_store.getters.getHideStatus();
  654. if (!$hide_status) {
  655. $this.game_finished();
  656. }
  657. }
  658. } catch (e) {
  659. console.log("调用方法失败", e);
  660. }
  661. }
  662. });
  663. },
  664. // 游玩时间倒计时
  665. formatPlaySeconds(value) {
  666. console.log('剩余可游玩时间',value)
  667. // 字符串转数字
  668. var secondTime = parseInt(value); // 秒
  669. var minuteTime = 0; // 分
  670. if (secondTime > 60) {
  671. //如果秒数大于60,将秒数转换成整数
  672. //获取分钟,除以60取整数,得到整数分钟
  673. minuteTime = parseInt(secondTime / 60);
  674. //获取秒数,秒数取佘,得到整数秒数
  675. secondTime = parseInt(secondTime % 60);
  676. //如果分钟大于60,将分钟转换成小时
  677. }
  678. var result = "" + parseInt(secondTime);
  679. if (minuteTime > 0) {
  680. if (result.length == 1) {
  681. result = "0" + result;
  682. }
  683. if (parseInt(minuteTime).toString().length == 1) {
  684. minuteTime = "0" + parseInt(minuteTime);
  685. }
  686. result = "" + minuteTime + ":" + result;
  687. } else {
  688. result = "00:" + result;
  689. }
  690. return result;
  691. },
  692. };