bt_parse.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. import {
  2. BtCmd,
  3. CmdBase,
  4. CmdEarPhone,
  5. CmdRtc,
  6. // CmdRgb,
  7. CmdKwh,
  8. CmdKey,
  9. CmdVoice,
  10. CmdWeek
  11. } from './bt_cmd';
  12. import {
  13. CmdEvent,
  14. EnumCmdEvent,
  15. EnumPlayStatus,
  16. EnumSupplier
  17. } from '../cmd_key_event';
  18. import eventBus from '../../utils/eventBus'
  19. class BtParse {
  20. // 型号名称
  21. // static mateX2 = "MW-Mate X(4G_WIFI)";
  22. // static matePVX = "猫王音响·Mate_PVX";
  23. // static matePVXL = "猫王音响·Mate_PVXL";
  24. // 防止收到另外的消息
  25. static isReceiveCmd = false;
  26. // 如果10ms内获取两个一模一样的就删除一个
  27. static lastCmd = null;
  28. static lastTime = null;
  29. constructor() {}
  30. // 解析命令
  31. static parseTLV(cmd) {
  32. BtCmd.printTLV(cmd, false);
  33. // todo 在线
  34. // if (!PlayDevice.isOnline) {
  35. // return;
  36. // }
  37. // if (!BtParse.isReceiveCmd) {
  38. // return;
  39. // }
  40. if (cmd.length > 5) {
  41. // 指令类别
  42. const type = cmd[6];
  43. if (cmd.length < 9) {
  44. // 杰里、领芯的语音控制指令
  45. BtParse._voiceCmd(cmd);
  46. } else {
  47. // cmd[7]为命令长度
  48. // 命令类型
  49. const value = cmd[8];
  50. // 第二部分走杰理
  51. if (type === CmdVoice.jlReceiveVoiceData) {
  52. BtParse._receiveRecordData(cmd);
  53. }
  54. // 查询版本号后校验设备
  55. else if (type === CmdBase.queryVersion) {
  56. const version = CmdBase.parseVersion(value);
  57. eventBus.fire(CmdEvent.version({
  58. version
  59. }));
  60. eventBus.fire(CmdEvent.getDeviceInfo());
  61. } else if (BtParse._getDataHeader(cmd)) {
  62. // 控制指令
  63. BtParse._controlCmd(cmd);
  64. } else {
  65. // console.log("收到不明指令:", cmd);
  66. const manufacturer = "";
  67. // DeviceManager.instance.device?.manufacturer ?? "";
  68. // 山景的语音判断
  69. // if (manufacturer === EnumSupplier.shanJing) {
  70. // const sjCmd = cmd[4];
  71. // if (sjCmd === CmdVoice.sjStartVoice) {
  72. // BtParse._startRecord(EnumSupplier.shanJing);
  73. // } else if (sjCmd === CmdVoice.sjStopVoice) {
  74. // BtParse._stopRecord(EnumSupplier.shanJing);
  75. // }
  76. // // 山景的都要走接收数据
  77. // BtParse._receiveRecordData(cmd);
  78. // } else {
  79. // // b1耳机、领芯、杰里p2第二部分语音数据直接走这里
  80. // BtParse._receiveRecordData(cmd);
  81. // }
  82. }
  83. }
  84. }
  85. }
  86. static _send(cmdEvent) {
  87. eventBus.fire(CmdEvent[cmdEvent]);
  88. }
  89. static _getDataHeader(cmd) {
  90. if (cmd.length < 4) {
  91. return false;
  92. }
  93. ["54", "44", "44", "48", "1", "9", "22", "1", "4"]
  94. const header = [0x54, 0x44, 0x44, 0x48];
  95. let isCmd = cmd[0] === header[0] && cmd[1] === header[1] && cmd[2] === header[2] && cmd[3] === header[3];
  96. // console.log("收到数据头:", cmd[0], header[0], cmd[1], header[1], cmd[2], header[2], cmd[3], header[3],);
  97. return isCmd;
  98. }
  99. // todo 暂时不要
  100. static _voiceCmd(cmd) {
  101. const type = cmd[6];
  102. console.log("收到语音指令:", cmd);
  103. // switch (type) {
  104. // // 杰理语音开始
  105. // case CmdVoice.jlStartVoice:
  106. // BtParse._startRecord(EnumSupplier.jieLi);
  107. // break;
  108. // // 杰理结束
  109. // case CmdVoice.jlStopVoice:
  110. // BtParse._stopRecord(EnumSupplier.jieLi);
  111. // break;
  112. // // 发给插件
  113. // case CmdVoice.jlReceiveVoiceData:
  114. // console.log("发送杰里的p1语音:没有进来过,不走这里的,直接判断0x70 && jieLi");
  115. // BtParse._receiveRecordData(cmd);
  116. // break;
  117. // // 领心语音开始
  118. // case CmdVoice.lxStartVoice:
  119. // BtParse._startRecord(EnumSupplier.lingXin);
  120. // break;
  121. // // 领心语音结束
  122. // case CmdVoice.lxStopVoice:
  123. // BtParse._stopRecord(EnumSupplier.lingXin);
  124. // break;
  125. // default:
  126. // console.log("发送语音:默认发送,不走这里", cmd);
  127. // break;
  128. // }
  129. }
  130. static async _startRecord(supplier) {
  131. console.log("StartVoice======语音开始", supplier);
  132. }
  133. static async _stopRecord(supplier) {
  134. console.log("stopRecord======语音结束", supplier);
  135. }
  136. // 杰理接收设备第二部分 发送给插件
  137. static _receiveRecordData(cmd) {
  138. // todo 暂时不要
  139. // RecordVoiceManager.sendDataToPlugin(cmd);
  140. }
  141. static async _controlCmd(cmd) {
  142. const type = cmd[6];
  143. const value = cmd[8];
  144. // mqttAddDebugCmd("收到蓝牙指令:" + cmd);
  145. // console.log("收到控制指令:", cmd, type)
  146. switch (type) {
  147. // 校验设备
  148. case CmdBase.checkDeviceSuccess:
  149. case CmdBase.checkDevice:
  150. // [84, 68, 68, 72, 1, 17, 40, 9, 115, 109, 97, 114, 116, 95, 65, 50, 70]
  151. break;
  152. // 按键 短按、长按等
  153. case CmdKey.type:
  154. // IOS的不要了
  155. // if (DeviceUtil.isIOS) {
  156. // if (BtParse.lastCmd && JSON.stringify(BtParse.lastCmd) === JSON.stringify(cmd)) {
  157. // if (BtParse.lastTime) {
  158. // const curTime = TimeUtil.getCurrentMillis();
  159. // if (curTime - BtParse.lastTime < 5) {
  160. // return;
  161. // }
  162. // }
  163. // }
  164. // BtParse.lastCmd = cmd;
  165. // BtParse.lastTime = TimeUtil.getCurrentMillis();
  166. // }
  167. let t = 0;
  168. if (cmd.length > 9) {
  169. t = cmd[9];
  170. }
  171. switch (t) {
  172. // 0x54 44 44 48 01 0a 21 02 01 02
  173. // 54, 44, 44, 48, 1, a, 21, 2, 1(value), 2(type)
  174. // 上一曲
  175. case CmdKey.previousSong:
  176. BtParse._send(EnumCmdEvent.previousSong);
  177. break;
  178. // 下一曲
  179. case CmdKey.nextSong:
  180. BtParse._send(EnumCmdEvent.nextSong);
  181. break;
  182. // 上一频道
  183. case CmdKey.previousChannel:
  184. BtParse._send(EnumCmdEvent.previousChannel);
  185. break;
  186. // 下一频道
  187. case CmdKey.nextChannel:
  188. BtParse._send(EnumCmdEvent.nextChannel);
  189. break;
  190. // 0x02 2
  191. case CmdKey.pressShort:
  192. // 1 下一个频道
  193. if (value === CmdKey.next) {
  194. BtParse._send(EnumCmdEvent.nextChannel);
  195. }
  196. // 2 上一个频道
  197. else if (value === CmdKey.previous) {
  198. BtParse._send(EnumCmdEvent.previousChannel);
  199. }
  200. // 3 单击下一个专辑 或者下一曲
  201. else if (value === CmdKey.changeProgram) {
  202. // if (ProviderUtil.device.mDevice?.clientType === BtParse.mateX2) {
  203. // BtParse._send(EnumCmdEvent.nextAlbum);
  204. // } else {
  205. // BtParse._send(EnumCmdEvent.nextSong);
  206. // }
  207. }
  208. break;
  209. // 0x09 9
  210. case CmdKey.pressDouble:
  211. // 1 双击下一曲
  212. if (value === CmdKey.next) {
  213. BtParse._send(EnumCmdEvent.nextSong);
  214. }
  215. // 2 双击上一曲
  216. else if (value === CmdKey.previous) {
  217. BtParse._send(EnumCmdEvent.previousSong);
  218. }
  219. // 3 单击上一专辑或者上一曲
  220. else if (value === CmdKey.changeProgram) {
  221. // todo
  222. // if (ProviderUtil.device.mDevice?.clientType === BtParse.mateX2) {
  223. // BtParse._send(EnumCmdEvent.previousAlbum);
  224. // } else {
  225. // BtParse._send(EnumCmdEvent.previousSong);
  226. // }
  227. }
  228. break;
  229. // 0x08 8
  230. case CmdKey.pressLong:
  231. if (value === CmdKey.previous) {
  232. console.log("快退");
  233. BtParse._send(EnumCmdEvent.fastback);
  234. } else if (value === CmdKey.next) {
  235. console.log("快进");
  236. BtParse._send(EnumCmdEvent.speed);
  237. }
  238. break;
  239. // 0x04 4
  240. case CmdKey.pressLongUp:
  241. if (value === CmdKey.previous) {
  242. console.log("停止快退");
  243. BtParse._send(EnumCmdEvent.stopFastback);
  244. } else if (value === CmdKey.next) {
  245. console.log("停止快进");
  246. BtParse._send(EnumCmdEvent.stopSpeed);
  247. } else if (value === CmdKey.pressLongUp) {
  248. // [54, 44, 44, 48, 1, a, 21, 2, 4, 4]
  249. console.log("录音结束");
  250. BtParse._send(EnumCmdEvent.stopRecord);
  251. }
  252. break;
  253. }
  254. BtHelper.instance.changeChannelCallBack();
  255. break;
  256. // 音量 54 44 44 48 1 a 32 2 b 0
  257. case CmdBase.queryVolume:
  258. // 手机最大音量 需要获取
  259. const phoneMax = 15;
  260. // todo 音量
  261. //await VolumeUtil.getMaxVolume();
  262. const max = CmdBase.volumeMax;
  263. const d = phoneMax / max;
  264. const result = (d * value).toFixed(2);
  265. var trueVolume = parseFloat(result) ?? 0;
  266. console.log("trueVolume=====", max, "===", d, ",,,,", value, ",,,,", result, ",,,,", trueVolume);
  267. if (trueVolume > phoneMax) {
  268. trueVolume = phoneMax;
  269. }
  270. // 仅用于记录设备音量,播控页面显示用
  271. eventBus.fire(CmdEvent.volume({
  272. volume: trueVolume
  273. }));
  274. // if (DeviceUtil.isAndroid) {
  275. // const name = "";
  276. // // todo
  277. // // DeviceManager.instance.name;
  278. // if (name === "猫王·霹雳唱机") {
  279. // // VolumeUtil.setVolume(trueVolume);
  280. // }
  281. // }
  282. break;
  283. // 查询电量
  284. case CmdKwh.queryKwh:
  285. eventBus.fire(CmdEvent.battery({
  286. kwh: value
  287. }));
  288. break;
  289. // 自动播放 54, 44, 44, 48, 1, 9, 24, 1, 1
  290. case CmdBase.getAutoPlay:
  291. case CmdBase.setAutoPlay:
  292. setTimeout(() => {
  293. eventBus.fire(CmdEvent.playStatus({
  294. playStatus: value === EnumOpen.open ? EnumPlayStatus.play : EnumPlayStatus.pause,
  295. }));
  296. }, 300);
  297. break;
  298. // 查询低电量设置
  299. case CmdKwh.queryLowKwhWarningTone:
  300. eventBus.fire(CmdEvent.queryLowKwn({
  301. kwh: value
  302. }));
  303. break;
  304. // 耳机电量 index0 :左耳机, index1:左耳机 ,index2: 充电盒。value: 0-9代表10-100% 获取不到用-1
  305. // [54, 44, 44, 48, 1, b, b1, 3, 8, 8, 2]
  306. case CmdEarPhone.queryEarPhoneKwh:
  307. const rightKwh = cmd[9];
  308. const boxKwh = cmd[10];
  309. let kwh = 0;
  310. if (value > 0 && rightKwh > 0) {
  311. kwh = Math.min(value, rightKwh);
  312. } else {
  313. kwh = Math.max(value, rightKwh);
  314. }
  315. eventBus.fire(CmdEvent.batteryEarphone({
  316. kwhLeft: value,
  317. kwhBox: boxKwh,
  318. kwhRight: rightKwh,
  319. kwh: kwh,
  320. }));
  321. break;
  322. // EQ [84, 68, 68, 72, 1, 18, 180, 10, 0, 0, 240, 237, 236, 0, 236, 0, 0, 0]
  323. // B5耳机获取EQ
  324. case CmdEarPhone.queryEQ:
  325. if (cmd.length > 15) {
  326. const result = cmd.slice(8, cmd.length);
  327. const list = result.map(element => parseInt(element, 16));
  328. console.log("eq==============", list);
  329. eventBus.fire(CmdEvent.eqs({
  330. eqs: list
  331. }));
  332. }
  333. break;
  334. // 低延迟 [54, 44, 44, 48, 1, a, b2, 2, 0, 2]
  335. case CmdEarPhone.queryLowDelayMode:
  336. const lowDelayMode = cmd[9];
  337. eventBus.fire(CmdEvent.lowDelayMode({
  338. lowDelayMode,
  339. lowDelayModeOpen: value
  340. }));
  341. break;
  342. // 耳机低电量
  343. case CmdEarPhone.queryLowPower:
  344. const lowPowerOpen = cmd[8];
  345. eventBus.fire(CmdEvent.lowPowerOpen({
  346. lowPowerOpen
  347. }));
  348. break;
  349. // 查询唤醒闹钟
  350. case CmdRtc.queryAlarm:
  351. const wakeSwitch = cmd[8];
  352. if (cmd.length > 9) {
  353. const week = cmd[9];
  354. const hour = cmd[10];
  355. const minutes = cmd[11];
  356. const wakeCycle = CmdWeek.cmd2Week(week);
  357. console.log("queryAlarm=====", wakeSwitch, "=", wakeCycle);
  358. eventBus.fire(CmdEvent.wake({
  359. wakeSwitch,
  360. wakeCycle,
  361. wakeHour: hour,
  362. wakeMinutes: minutes,
  363. }));
  364. }
  365. break;
  366. // 查询自动休眠状态
  367. case CmdRtc.querySleepAfterPlayPause:
  368. case CmdRtc.setSleepAfterPlayPause: {
  369. let hour = cmd[8] ?? 0;
  370. let minutes = cmd[9] ?? 0;
  371. var seconds = cmd[10] ?? 0;
  372. console.log("hour=====", hour, "minutes=====", minutes, "seconds=====", seconds)
  373. hour = hour > 24 ? 0 : hour;
  374. minutes = minutes > 60 ? 0 : minutes;
  375. seconds = seconds > 60 ? 0 : seconds;
  376. let allSeconds = hour * 3600 + minutes * 60 + seconds;
  377. // [84, 68, 68, 72, 1, 12, 90, 4, 0, 14, 0, 72]
  378. eventBus.fire(CmdEvent.pauseSleep({
  379. pauseSleep: allSeconds
  380. }));
  381. }
  382. break;
  383. // 查询休眠
  384. case CmdRtc.querySleep:
  385. const sleepSwitch = cmd[8];
  386. const hour = cmd[9];
  387. const minutes = cmd[10];
  388. const leftHour = cmd[11];
  389. const leftMinutes = cmd[12];
  390. eventBus.fire(CmdEvent.sleep({
  391. sleepSwitch,
  392. sleepHour: hour,
  393. sleepMinutes: minutes,
  394. }));
  395. break;
  396. // 开始录音
  397. case CmdVoice.startVoice:
  398. console.log("没有意义的开始录音");
  399. // _send(EnumCmdEvent.startRecord);
  400. break;
  401. // 查询 耳机手势
  402. case CmdEarPhone.queryCtrlStatus:
  403. const singleC = cmd[8];
  404. const doubleC = cmd[9];
  405. const longC = cmd[10];
  406. eventBus.fire(CmdEvent.ctrlStatus({
  407. ctrlStatus: [singleC, doubleC, longC]
  408. }));
  409. break;
  410. case CmdBase.getMac: {
  411. var values = ""
  412. // [54:44:44:48:01:0D:35:90:9D:99:CF:34:5B:]
  413. for (let i = 7; i < cmd.length; i++) {
  414. values += cmd[i].toString(16);
  415. if (i != cmd.length - 1) {
  416. values += ":";
  417. }
  418. }
  419. // let str = this.hexArrayToString(values);
  420. // getMac===== ?MV-SR1(4G_WIFI)
  421. // let device = getApp().globalData.mDeviceList[0] ?? {};
  422. // device["mac"] = values;
  423. console.log("getMac=====", values)
  424. eventBus.fire(CmdEvent.btMac({
  425. "btMac": values
  426. }));
  427. }
  428. // [84, 68, 68, 72, 1, 23, 53, 16, 77, 86, 45, 83, 82, 49, 40, 52, 71, 95, 87, 73, 70, 73, 41]
  429. break;
  430. case CmdBase.getClientType: {
  431. var values = []
  432. for (let i = 8; i < cmd.length; i++) {
  433. values.push(cmd[i]);
  434. }
  435. let str = this.hexArrayToString(values);
  436. let device = getApp().globalData.mDeviceList[0] ?? {};
  437. device["trueClientType"] = str;
  438. console.log("getClientType=====", device)
  439. eventBus.fire(CmdEvent.clientType(str));
  440. }
  441. //  [84, 68, 68, 72, 1, 14, 49, 7, 228, 159, 128, 9, 224, 236]
  442. break;
  443. case CmdBase.getIsConnect:
  444. // [84, 68, 68, 72, 1, 10, 54, 2, 0, 1]
  445. let device = getApp().globalData.mDeviceList[0] ?? {};
  446. device["isConnectMac"] = value;
  447. console.log("getIsConnect=====", device)
  448. break;
  449. case CmdBase.heiJiaoOta:
  450. // [54:44:44:48:01:0A:74:01:01:00]
  451. {
  452. let kind = cmd[9]
  453. eventBus.fire(CmdEvent.otaCmd({
  454. value: value,
  455. kind: kind
  456. }));
  457. }
  458. break;
  459. case CmdBase.heijiaoBackImg:
  460. // 0x76
  461. {
  462. let kind = cmd[9]
  463. eventBus.fire(CmdEvent.otaWifi({
  464. value: value,
  465. kind: kind
  466. }));
  467. }
  468. break;
  469. case CmdBase.wallPaper:
  470. // [54:44:44:48:01:0A:78:01:01:00]
  471. {
  472. let kind = cmd[9]
  473. eventBus.fire(CmdEvent.wallpaper({
  474. value: value,
  475. kind: kind
  476. }));
  477. }
  478. break;
  479. case CmdBase.wallPaperData:
  480. // [54:44:44:48:01:0A:78:01:01:00]
  481. {
  482. let kind = cmd[9]
  483. eventBus.fire(CmdEvent.wallPaperData({
  484. value: value,
  485. kind: kind
  486. }));
  487. }
  488. break;
  489. case CmdBase.heiJiaoOtaData:
  490. //0x75
  491. {
  492. let kind = cmd[9]
  493. eventBus.fire(CmdEvent.otaUrl({
  494. value: value,
  495. kind: kind
  496. }));
  497. }
  498. break;
  499. case CmdBase.wallPaperMD5:
  500. //0x80
  501. {
  502. let kind = cmd[9]
  503. eventBus.fire(CmdEvent.wallPaperMD5({
  504. value: value,
  505. kind: kind
  506. }));
  507. }
  508. break;
  509. default:
  510. console.log("接收到语音:", cmd);
  511. // _receiveRecordData(cmd);
  512. break;
  513. }
  514. }
  515. static hexArrayToString(hexArray) {
  516. let str = '';
  517. for (let i = 0; i < hexArray.length; i++) {
  518. const charCode = hexArray[i];
  519. if (charCode >= 32 && charCode <= 126) { // 可打印的ASCII字符范围
  520. str += String.fromCharCode(charCode);
  521. } else {
  522. str += '?'; // 替换为问号或其他符号
  523. }
  524. }
  525. return str;
  526. }
  527. static 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. // 导出类
  538. module.exports = BtParse;