bt_cmd.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // 设备命令
  2. // TLV格式(16进制)
  3. // 二进制协议使用 TLV (Type-Length-Value)编码格式。
  4. // - Type:固定长度, 一字节。
  5. // - Length:可变长度。 0 或 1 个字节。
  6. // - Value:二进制数据。 长度由 Length 字段决定。 数据本身可以上数字或者字符串。
  7. const EnumOpen = {
  8. close: 0,
  9. open: 1
  10. };
  11. const EnumConnectType = {
  12. bt: 0,
  13. ble: 1,
  14. upnp: 2,
  15. mqtt: 3
  16. }
  17. class BtCmd {
  18. // BtCmd._();
  19. constructor() { }
  20. // 获取版本号
  21. static queryVersion() {
  22. return this._build(CmdBase.queryVersion, [0x00]);
  23. }
  24. // 查询电量 电量值 0-9,0对应10%,9对应100%,10充电
  25. static queryKwh() {
  26. return this._build(CmdKwh.queryKwh, [0x00]);
  27. }
  28. // 查询耳机电量
  29. static queryKwhEarPhone() {
  30. return this._build(CmdEarPhone.queryEarPhoneKwh, [0x00]);
  31. }
  32. // 查询EQ音效
  33. static queryEQ() {
  34. return this._build(CmdEarPhone.queryEQ, [0x00]);
  35. }
  36. // 设置EQ音效
  37. static setEQ(list) {
  38. return this._build(CmdEarPhone.setEQ, list);
  39. }
  40. // B5耳机-低电耗模式
  41. static queryLowPower() {
  42. return this._build(CmdEarPhone.queryLowPower, [0x00]);
  43. }
  44. static setLowPowerMode(isOpen) {
  45. return this._build(CmdEarPhone.setLowPowerMode, [isOpen]);
  46. }
  47. // B5耳机-自定义操作手势
  48. static queryCtrlStatus() {
  49. return this._build(CmdEarPhone.queryCtrlStatus, [0x00]);
  50. }
  51. static setCtrlStatus(singleClick, doubleClick, longClick) {
  52. return this._build(CmdEarPhone.setCtrlStatus, [singleClick, doubleClick, longClick]);
  53. }
  54. // 查询低延时模式
  55. static queryLowDelayMode() {
  56. return this._build(CmdEarPhone.queryLowDelayMode, [0x00]);
  57. }
  58. // 低延时模式
  59. static setLowDelayMode(open, mode) {
  60. return this._build(CmdEarPhone.setLowDelayMode, [open, mode]);
  61. }
  62. // 低电量提示音
  63. static queryLowKwhWarningTone() {
  64. return this._build(CmdKwh.queryLowKwhWarningTone, [0x00]);
  65. }
  66. static setLowKwhWarningTone(isNotify) {
  67. return this._build(CmdKwh.setLowKwhWarningTone, isNotify ? [0x01] : [0x00]);
  68. }
  69. // 蓝牙自动播放
  70. static setAutoPlay(switchStatus) {
  71. return this._build(CmdBase.setAutoPlay, [switchStatus]);
  72. }
  73. static AutoPlay() {
  74. return this._build(CmdBase.getAutoPlay, [0x00]);
  75. }
  76. // 查询音量
  77. static queryVolume() {
  78. return this._build(CmdBase.queryVolume, [0x00]);
  79. }
  80. static setVolume(volume) {
  81. return this._build(CmdBase.setVolume, [volume]);
  82. }
  83. // 查询闹钟
  84. static queryAlarm() {
  85. return this._build(CmdRtc.queryAlarm, [0x00]);
  86. }
  87. // 设置闹钟
  88. static setAlarm(switchStatus, weekCycle, hour, minutes, channel = 1) {
  89. console.log("setAlarm=====", switchStatus, "=", weekCycle, "=", hour, "=", minutes);
  90. const weeks = CmdWeek.week2Cmd(weekCycle);
  91. return this._build(CmdRtc.setAlarm, [switchStatus, weeks, hour, minutes, channel]);
  92. }
  93. // 查询休眠
  94. static querySleep() {
  95. return this._build(CmdRtc.querySleep, [0x00]);
  96. }
  97. static querySleepAfterPlayPause() {
  98. return this._build(CmdRtc.querySleepAfterPlayPause, [0x00]);
  99. }
  100. static setSleepAfterPlayPause(time) {
  101. if (time > 0) {
  102. // let day = (60 * 60)
  103. // let hour = 60
  104. // const hours = time / day;
  105. // const minutes = (time % day) / hour;
  106. // const seconds = (time % day % hour);
  107. // 将小时、分钟和秒转换为16进制字符串并转为大写
  108. // console.log(`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`);
  109. // console.log("休眠:", hour, minutes, seconds)
  110. // 跟app一样错误的处理方式
  111. return this._build(CmdRtc.setSleepAfterPlayPause, [time]);
  112. } else {
  113. return this._build(CmdRtc.setSleepAfterPlayPause, [0]);
  114. }
  115. }
  116. // 设置休眠
  117. static setSleep(switchStatus, hour, minutes) {
  118. return this._build(CmdRtc.setSleep, [switchStatus, hour, minutes]);
  119. }
  120. // 设置时间
  121. static setDevTime() {
  122. const now = new Date();
  123. const y = now.getFullYear();
  124. const year = Math.floor(y / 100);
  125. const _year = y % 100;
  126. const month = now.getMonth() + 1; // getMonth() 返回 0-11,所以需要加 1
  127. const day = now.getDate();
  128. const hour = now.getHours();
  129. const minutes = now.getMinutes();
  130. const seconds = now.getSeconds();
  131. console.log(`setTime========${year},${_year},${month},${day},${hour},${minutes},${seconds}`);
  132. // console.log(`setTime======== 2, ${CmdRtc.setTime}`);
  133. let cmd = this._build(0x50, [year, _year, month, day, hour, minutes, seconds]);
  134. console.log(`setTime========${cmd}`);
  135. return cmd;
  136. }
  137. // 查询时间
  138. static queryTime() {
  139. return this._build(CmdRtc.queryTime, [0x00]);
  140. }
  141. // 设置RGB
  142. static setRGB(r, g, b) {
  143. return this._build(CmdRgb.set, [r, g, b]);
  144. }
  145. // 查询RGB
  146. static queryRGB() {
  147. return this._build(CmdRgb.query, [0x00]);
  148. }
  149. static stringToUint8Array(text) {
  150. // return [0x73, 0x6d, 0x61, 0x72, 0x74, 0x5f, 0x41, 0x32, 0x46]
  151. // return [115, 109, 97, 114, 116, 95, 65, 50, 70]
  152. // return [115, 109, 97, 114, 116, 95, 65, 50, 70]
  153. // 73 6D 61 72 74 5F 41 32 46
  154. if (!text) {
  155. return []
  156. }
  157. console.log("stringToUint8Array", text)
  158. let code = Array.from(text).map(char => char.charCodeAt(0));
  159. console.log("stringToUint8Array", code)
  160. return code
  161. }
  162. static listInt2String(data) {
  163. return String.fromCharCode(...data);
  164. }
  165. static intToHex(num) {
  166. let value = num ?? 0;
  167. let hexStr = value.toString(16).toUpperCase();
  168. hexStr = hexStr.padStart(2, '0');
  169. let hexNum = parseInt(hexStr, 16);
  170. return hexNum;
  171. }
  172. // 校验设备
  173. static checkDevice() {
  174. let secretKey = this.stringToUint8Array(CmdBase.secretKey)
  175. // console.log(`checkDevice========${secretKey}`);
  176. return this._build(CmdBase.checkDevice, secretKey);
  177. }
  178. // 杰理语音开始回应
  179. static jlStartVoiceResponse() {
  180. return this._build(CmdVoice.jlStartVoiceResponse);
  181. }
  182. // 杰理语音结束回应
  183. static jlStopVoiceResponse() {
  184. return this._build(CmdVoice.jlStopVoiceResponse);
  185. }
  186. // 领芯语音
  187. static lxStartVoiceResponse() {
  188. return this._build(CmdVoice.lxStartVoiceResponse);
  189. }
  190. // 杰理语音结束回应
  191. static lxStopVoiceResponse() {
  192. return this._build(CmdVoice.lxStopVoiceResponse);
  193. }
  194. // b1开始语音回调
  195. static b1StartVoiceResponse() {
  196. const data = [0x04, 0x80, 0x01, 0x00, 0x02];
  197. return new Uint8Array(data);
  198. }
  199. // b1结束语音回调
  200. static b1StopVoiceResponse() {
  201. const data = [0x05, 0x80, 0x01, 0x00, 0x02];
  202. return new Uint8Array(data);
  203. }
  204. // 固定头部
  205. static get _header() {
  206. return [0x54, 0x44, 0x44, 0x48];
  207. }
  208. // 版本号
  209. static get _version() {
  210. return 0x01;
  211. }
  212. // wifi ,md5都用这个
  213. static sendWiFiInfo(wifiName, pwd) {
  214. // [0x22, (wifiList.length + pwdList.length + 6), 0x33, (wifiList.length), (wifiList), 0x44, (pwdList)];
  215. if (!wifiName) {
  216. return;
  217. }
  218. let result = [];
  219. // 字母*6 +
  220. let wifiList = BtCmd.stringToUint8Array(wifiName);
  221. // 数字*3 +
  222. var pwdList = [];
  223. if (pwd) {
  224. pwdList = BtCmd.stringToUint8Array(pwd);
  225. }
  226. console.log("wifiList", wifiList, "pwdList", pwdList)
  227. // 16进制
  228. result.push(0x22);
  229. result.push(BtCmd.intToHex(wifiList.length + pwdList.length + 6));
  230. // 账号
  231. result.push(0x33);
  232. result.push(BtCmd.intToHex(wifiList.length));
  233. let p = result[3] + 4;
  234. let j = 0;
  235. for (let i = 4; i < p; i++) {
  236. result.splice(i, 0, wifiList[j++]);
  237. }
  238. result.splice(p, 0, 0x44);
  239. // 有密码
  240. if (pwdList.length > 0) {
  241. result.splice(++p, 0, BtCmd.intToHex(pwdList.length));
  242. p++;
  243. j = 0;
  244. for (let i = p; i < p + pwdList.length; i++) {
  245. result.splice(i, 0, pwdList[j++]);
  246. }
  247. } else {
  248. p++
  249. result.splice(++p, 0, BtCmd.intToHex(pwdList.length));
  250. }
  251. console.log("发送转换拼接数据:", result.toString());
  252. return result;
  253. }
  254. /* 黑胶音箱的指令 */
  255. // 壁纸指令 1开始, 0结束
  256. static wallPaper(value) {
  257. let cmd = this._build(CmdBase.wallPaper, [value]);
  258. console.log("图片上传:", cmd);
  259. return cmd;
  260. }
  261. static wallPaperMD5(value) {
  262. let cmd = this._build(CmdBase.wallPaperMD5, value, true, false);
  263. console.log("图片上传MD5:", cmd);
  264. return cmd;
  265. }
  266. // 壁纸指令
  267. static wallPaperData(value) {
  268. let uint8Array = new Uint8Array(value);
  269. let cmd = this._build(CmdBase.wallPaperData, uint8Array, true, false);
  270. return cmd;
  271. }
  272. // 设置wifi
  273. static otaSetWifi(value) {
  274. return this._build(CmdBase.heijiaoBackImg, value, true, false);
  275. } // 背景图指令
  276. static backgroudImgData(value) {
  277. return this._build(CmdBase.heijiaoBackImgData, [value]);
  278. }
  279. // OTA指令 1开始, 0结束
  280. static otaCmd(value) {
  281. return this._build(CmdBase.heiJiaoOta, [value]);
  282. }
  283. // 发送OTA的URL
  284. static otaUrl(value) {
  285. return this._build(CmdBase.heiJiaoOtaData, value, true, false);
  286. }
  287. //暂时没用了
  288. static otaData(value) {
  289. return this._build(CmdBase.heiJiaoOta, [value], true, false);
  290. }
  291. /// 获取设备型号指令
  292. static getClientType() {
  293. return this._build(0x31, [0x00]);
  294. } ///获取设备mac地址
  295. static getMac() {
  296. return this._build(0x35, [0x00]);
  297. }
  298. ///获取手机是否连接了设备
  299. static getIsConnect() {
  300. return this._build(0x36, [0x00]);
  301. }
  302. // 生成命令(固定头部+版本号+命令总长度+命令类型+)
  303. static _build(cmdType, otherCmd = [], isWriteChildCmdLength = true, isChangeHex = true) {
  304. const cmd = [];
  305. // 固定头部
  306. cmd.push(...this._header);
  307. // 版本号
  308. cmd.push(this._version);
  309. // 命令类型
  310. cmd.push(cmdType);
  311. if (isWriteChildCmdLength) {
  312. // 子命令长度
  313. const childLength = this._int2Hex(otherCmd.length);
  314. cmd.push(childLength);
  315. }
  316. // 其他命令
  317. if (otherCmd.length > 0) {
  318. if (isChangeHex) {
  319. for (const element of otherCmd) {
  320. cmd.push(this._int2Hex(element));
  321. }
  322. } else {
  323. cmd.push(...otherCmd);
  324. }
  325. }
  326. // 命令长度(位置在版本号之后)
  327. const length = cmd.length + 1;
  328. const l = this._int2Hex(length);
  329. cmd.splice(5, 0, l);
  330. // console.log(`打印cmd:${cmd}`);
  331. return cmd;
  332. }
  333. // 打印cmd
  334. static printTLV(cmd, isSend = false) {
  335. let result = "";
  336. const key = isSend ? "发送" : "接收";
  337. if (isSend) {
  338. // console.log(`${key}原始-${cmd.toString()}`);
  339. }
  340. for (let i = 0; i < cmd.length; i++) {
  341. result += (`${this._int2HexString(cmd[i])}`.padStart(2, "0") + ":");
  342. }
  343. if (isSend) {
  344. mqttAddDebugCmd(`发送蓝牙指令:${cmd} \n ${result}`);
  345. }
  346. console.log(`${key}解析- [${result}],原始数据:${cmd}`);
  347. }
  348. static _int2Hex(num) {
  349. let value = num ?? 0;
  350. let hexStr = value.toString(16).toUpperCase();
  351. hexStr = hexStr.padStart(2, '0');
  352. let hexNum = parseInt(hexStr, 16);
  353. return hexNum;
  354. }
  355. static _int2HexString(value) {
  356. return value.toString(16).toUpperCase();
  357. }
  358. }
  359. // 设备命令类型
  360. const CmdBase = {
  361. secretKey: "smart_A2F",
  362. queryVersion: 0x30,
  363. checkDevice: 0x28,
  364. checkDeviceSuccess: 0x27,
  365. setAutoPlay: 0x24,
  366. getAutoPlay: 0x23,
  367. queryChannelAngle: 0x38,
  368. switchLed: 0x39,
  369. queryVolume: 0x32,
  370. setVolume: 0x33,
  371. wallPaper: 0x78,
  372. wallPaperMD5: 0x80,
  373. wallPaperData: 0x79,
  374. heiJiaoOta: 0x074,
  375. heiJiaoOtaData: 0x75,
  376. heijiaoBackImg: 0x76,
  377. heijiaoBackImgData: 0x77,
  378. // 获取设备型号指令
  379. getClientType: 0x31,
  380. ///获取设备mac地址
  381. getMac: 0x35,
  382. ///获取手机是否连接了设备
  383. getIsConnect: 0x36,
  384. newFlag: "MW-Mate X\\(4G_WIFI\\)|MW-2AX\\(WIFI-N\\)|MW-SR1\\(4G_WIFI\\)|MW-SR1\\(4G_WIFI_MEIZU01\\)",
  385. // volumeMax: (new RegExp(`^(${CmdBase.newFlag})$`, 'i').test(DeviceManager.instance.clientType || "")) ? 0x20 : 0xF,
  386. volumeMax: 15,
  387. parseVersion(version) {
  388. let ver;
  389. if (version <= 0) {
  390. ver = "1.0.0";
  391. } else {
  392. const split = version.toString().split("");
  393. if (split.length === 1) {
  394. split.unshift("0", "0");
  395. } else if (split.length === 2) {
  396. split.unshift("0");
  397. }
  398. ver = split.join(".");
  399. }
  400. console.log(`parseVersion======${version},,,,,,,${ver.toString()}`);
  401. return ver;
  402. }
  403. };
  404. // 耳机相关指令
  405. const CmdEarPhone = {
  406. queryLowDelayMode: 0xb2,
  407. setLowDelayMode: 0xb3,
  408. queryEQ: 0xb4,
  409. setEQ: 0xb5,
  410. queryEarPhoneKwh: 0xb1,
  411. queryCtrlStatus: 0xB6,
  412. setCtrlStatus: 0xB7,
  413. queryLowPower: 0xB8,
  414. setLowPowerMode: 0xB9
  415. };
  416. // 时间相关
  417. const CmdRtc = {
  418. setTime: 0x50,
  419. queryTime: 0x51,
  420. setAlarm: 0x52,
  421. queryAlarm: 0x54,
  422. setSleep: 0x56,
  423. querySleep: 0x57,
  424. setSleepAfterPlayPause: 0x5a,
  425. querySleepAfterPlayPause: 0x5b
  426. };
  427. // 设置设备颜色
  428. const CmdRgb = {
  429. query: 0x80,
  430. set: 0x81
  431. };
  432. // 电量相关命令
  433. const CmdKwh = {
  434. queryKwh: 0x22,
  435. setLowKwhWarningTone: 0x26,
  436. queryLowKwhWarningTone: 0x25
  437. };
  438. // 按键
  439. const CmdKey = {
  440. previousSong: 0x0a,
  441. nextSong: 0x0b,
  442. previousChannel: 0x0c,
  443. nextChannel: 0x0d,
  444. type: 0x21,
  445. pressShort: 0x02,
  446. pressLongUp: 0x04,
  447. pressLong: 0x08,
  448. pressDouble: 0x09,
  449. next: 0x01,
  450. previous: 0x02,
  451. changeProgram: 0x03
  452. };
  453. // 语音
  454. const CmdVoice = {
  455. startVoice: 0x01,
  456. jlStartVoice: 0x66,
  457. jlStopVoice: 0x68,
  458. jlStartVoiceResponse: 0x67,
  459. jlStopVoiceResponse: 0x69,
  460. jlReceiveVoiceData: 0x70,
  461. lxStartVoice: 0x71,
  462. lxStopVoice: 0x72,
  463. lxStartVoiceResponse: 0x40,
  464. lxStopVoiceResponse: 0x41,
  465. sjStartVoice: 0x23,
  466. sjStopVoice: 0x24
  467. };
  468. // 一周
  469. const CmdWeek = {
  470. sunday: 0x01,
  471. monday: 0x02,
  472. tuesday: 0x04,
  473. wednesday: 0x08,
  474. thursday: 0x10,
  475. friday: 0x20,
  476. saturday: 0x40,
  477. weeks: [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40],
  478. week2Cmd(weekCycle) {
  479. let result = 0;
  480. for (let i = 0; i < weekCycle.length; i++) {
  481. const item = weekCycle[i];
  482. if (item === EnumOpen.open) {
  483. result += this.weeks[i];
  484. }
  485. }
  486. return result;
  487. },
  488. cmd2Week(cmd) {
  489. const w = [0, 0, 0, 0, 0, 0, 0];
  490. for (let i = this.weeks.length - 1; i >= 0; i--) {
  491. const week = this.weeks[i];
  492. if (week <= cmd) {
  493. w[i] = EnumOpen.open;
  494. cmd -= week;
  495. }
  496. }
  497. return w;
  498. }
  499. };
  500. module.exports = {
  501. BtCmd,
  502. CmdBase,
  503. CmdEarPhone,
  504. CmdRtc,
  505. CmdRgb,
  506. CmdKwh,
  507. CmdKey,
  508. CmdVoice,
  509. CmdWeek
  510. };