bluetooth.js 19 KB

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