bt_cmd.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. const weeks = CmdWeek.week2Cmd(weekCycle);
  90. return this._build(CmdRtc.setAlarm, [switchStatus, weeks, hour, minutes, channel]);
  91. }
  92. // 查询休眠
  93. static querySleep() {
  94. return this._build(CmdRtc.querySleep, [0x00]);
  95. }
  96. static querySleepAfterPlayPause() {
  97. return this._build(CmdRtc.querySleepAfterPlayPause, [0x00]);
  98. }
  99. static setSleepAfterPlayPause(time) {
  100. const hours = Math.floor(time / (1000 * 60 * 60));
  101. const minutes = Math.floor((time % (1000 * 60 * 60)) / (1000 * 60));
  102. const seconds = Math.floor((time % (1000 * 60)) / 1000);
  103. console.log(`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`);
  104. return this._build(CmdRtc.setSleepAfterPlayPause, [hours, minutes, seconds]);
  105. }
  106. // 设置休眠
  107. static setSleep(switchStatus, hour, minutes) {
  108. return this._build(CmdRtc.setSleep, [switchStatus, hour, minutes]);
  109. }
  110. // 设置时间
  111. static setDevTime() {
  112. const now = new Date();
  113. const y = now.getFullYear();
  114. const year = Math.floor(y / 100);
  115. const _year = y % 100;
  116. const month = now.getMonth() + 1; // getMonth() 返回 0-11,所以需要加 1
  117. const day = now.getDate();
  118. const hour = now.getHours();
  119. const minutes = now.getMinutes();
  120. const seconds = now.getSeconds();
  121. console.log(`setTime========${year},${_year},${month},${day},${hour},${minutes},${seconds}`);
  122. // console.log(`setTime======== 2, ${CmdRtc.setTime}`);
  123. let cmd = this._build(0x50, [year, _year, month, day, hour, minutes, seconds]);
  124. console.log(`setTime========${cmd}`);
  125. return cmd;
  126. }
  127. // 查询时间
  128. static queryTime() {
  129. return this._build(CmdRtc.queryTime, [0x00]);
  130. }
  131. // 设置RGB
  132. static setRGB(r, g, b) {
  133. return this._build(CmdRgb.set, [r, g, b]);
  134. }
  135. // 查询RGB
  136. static queryRGB() {
  137. return this._build(CmdRgb.query, [0x00]);
  138. }
  139. static stringToUint8Array(str) {
  140. // 54 44 44 48 01 11 28 09 73 6d6172745f413246
  141. // const encoder = new TextEncoder();
  142. // const uint8Array = encoder.encode(str);
  143. // return uint8Array;
  144. // return [0x73, 0x6d, 0x61, 0x72, 0x74, 0x5f, 0x41, 0x32, 0x46]
  145. return [115, 109, 97, 114, 116, 95, 65, 50, 70]
  146. }
  147. // 校验设备
  148. static checkDevice() {
  149. let secretKey = this.stringToUint8Array(CmdBase.secretKey)
  150. // console.log(`checkDevice========${secretKey}`);
  151. return this._build(CmdBase.checkDevice, secretKey);
  152. }
  153. // 杰理语音开始回应
  154. static jlStartVoiceResponse() {
  155. return this._build(CmdVoice.jlStartVoiceResponse);
  156. }
  157. // 杰理语音结束回应
  158. static jlStopVoiceResponse() {
  159. return this._build(CmdVoice.jlStopVoiceResponse);
  160. }
  161. // 领芯语音
  162. static lxStartVoiceResponse() {
  163. return this._build(CmdVoice.lxStartVoiceResponse);
  164. }
  165. // 杰理语音结束回应
  166. static lxStopVoiceResponse() {
  167. return this._build(CmdVoice.lxStopVoiceResponse);
  168. }
  169. // b1开始语音回调
  170. static b1StartVoiceResponse() {
  171. const data = [0x04, 0x80, 0x01, 0x00, 0x02];
  172. return new Uint8Array(data);
  173. }
  174. // b1结束语音回调
  175. static b1StopVoiceResponse() {
  176. const data = [0x05, 0x80, 0x01, 0x00, 0x02];
  177. return new Uint8Array(data);
  178. }
  179. // 固定头部
  180. static get _header() {
  181. return [0x54, 0x44, 0x44, 0x48];
  182. }
  183. // 版本号
  184. static get _version() {
  185. return 0x01;
  186. }
  187. /* 黑胶音箱的指令 */
  188. // 壁纸指令 1开始, 0结束
  189. static wallPaper(value) {
  190. return this._build(CmdBase.wallPaper, [value]);
  191. }
  192. // 壁纸指令
  193. static wallPaperData(value) {
  194. return this._build(CmdBase.wallPaperData, [value]);
  195. }
  196. // 背景图指令 1开始, 0结束
  197. static backgroudImg(value) {
  198. return this._build(CmdBase.heijiaoBackImg, [value]);
  199. } // 背景图指令
  200. static backgroudImgData(value) {
  201. return this._build(CmdBase.heijiaoBackImgData, [value]);
  202. }
  203. // OTA指令 1开始, 0结束
  204. static otaCmd(value) {
  205. return this._build(CmdBase.heiJiaoOta, [value]);
  206. } static otaData(value) {
  207. return this._build(CmdBase.heiJiaoOtaData, [value]);
  208. }
  209. /// 获取设备型号指令
  210. static getClientType() {
  211. return this._build(0x31, [0x00]);
  212. } ///获取设备mac地址
  213. static getMac() {
  214. return this._build(0x35, [0x00]);
  215. }
  216. ///获取手机是否连接了设备
  217. static getIsConnect() {
  218. return this._build(0x36, [0x00]);
  219. }
  220. // 生成命令(固定头部+版本号+命令总长度+命令类型+)
  221. static _build(cmdType, otherCmd = [], isWriteChildCmdLength = true) {
  222. const cmd = [];
  223. // 固定头部
  224. cmd.push(...this._header);
  225. // 版本号
  226. cmd.push(this._version);
  227. // 命令类型
  228. cmd.push(cmdType);
  229. if (isWriteChildCmdLength) {
  230. // 子命令长度
  231. const childLength = this._int2Hex(otherCmd.length);
  232. cmd.push(childLength);
  233. }
  234. // 其他命令
  235. if (otherCmd.length > 0) {
  236. for (const element of otherCmd) {
  237. cmd.push(this._int2Hex(element));
  238. }
  239. }
  240. // 命令长度(位置在版本号之后)
  241. const length = cmd.length + 1;
  242. const l = this._int2Hex(length);
  243. cmd.splice(5, 0, l);
  244. return new Uint8Array(cmd);
  245. }
  246. // 打印cmd
  247. static printTLV(cmd, isSend = false) {
  248. let result = "";
  249. const key = isSend ? "发送" : "接收";
  250. if (isSend) {
  251. // console.log(`${key}原始-${cmd.toString()}`);
  252. }
  253. for (let i = 0; i < cmd.length; i++) {
  254. const cmdStr = `${this._int2HexString(cmd[i])}`.padStart(2, "0");
  255. result += cmdStr;
  256. }
  257. if (isSend) {
  258. mqttAddDebugCmd(`发送蓝牙指令:${cmd} \n ${result}`);
  259. }
  260. console.log(`${key}解析- [${result}]`);
  261. }
  262. static _int2Hex(num) {
  263. let value = num ?? 0
  264. let hexStr = value.toString(16).toUpperCase(); // 转换为16进制字符串并转为大写
  265. return hexStr.padStart(2, '0');
  266. }
  267. static _int2HexString(value) {
  268. return value.toString(16).toUpperCase();
  269. }
  270. }
  271. // 设备命令类型
  272. const CmdBase = {
  273. secretKey: "smart_A2F",
  274. queryVersion: 0x30,
  275. checkDevice: 0x28,
  276. checkDeviceSuccess: 0x27,
  277. setAutoPlay: 0x24,
  278. getAutoPlay: 0x23,
  279. queryChannelAngle: 0x38,
  280. switchLed: 0x39,
  281. queryVolume: 0x32,
  282. setVolume: 0x33,
  283. wallPaper: 0x78,
  284. wallPaperData: 0x79,
  285. heiJiaoOta: 0x074,
  286. heiJiaoOtaData: 0x075,
  287. heijiaoBackImg: 0x76,
  288. heijiaoBackImgData: 0x77,
  289. // 获取设备型号指令
  290. getClientType: 0x31,
  291. ///获取设备mac地址
  292. getMac: 0x35,
  293. ///获取手机是否连接了设备
  294. getIsConnect: 0x36,
  295. newFlag: "MW-Mate X\\(4G_WIFI\\)|MW-2AX\\(WIFI-N\\)|MW-SR1\\(4G_WIFI\\)|MW-SR1\\(4G_WIFI_MEIZU01\\)",
  296. // volumeMax: (new RegExp(`^(${CmdBase.newFlag})$`, 'i').test(DeviceManager.instance.clientType || "")) ? 0x20 : 0xF,
  297. volumeMax: 15,
  298. parseVersion(version) {
  299. let ver;
  300. if (version <= 0) {
  301. ver = "1.0.0";
  302. } else {
  303. const split = version.toString().split("");
  304. if (split.length === 1) {
  305. split.unshift("0", "0");
  306. } else if (split.length === 2) {
  307. split.unshift("0");
  308. }
  309. ver = split.join(".");
  310. }
  311. console.log(`parseVersion======${version},,,,,,,${ver.toString()}`);
  312. return ver;
  313. }
  314. };
  315. // 耳机相关指令
  316. const CmdEarPhone = {
  317. queryLowDelayMode: 0xb2,
  318. setLowDelayMode: 0xb3,
  319. queryEQ: 0xb4,
  320. setEQ: 0xb5,
  321. queryEarPhoneKwh: 0xb1,
  322. queryCtrlStatus: 0xB6,
  323. setCtrlStatus: 0xB7,
  324. queryLowPower: 0xB8,
  325. setLowPowerMode: 0xB9
  326. };
  327. // 时间相关
  328. const CmdRtc = {
  329. setTime: 0x50,
  330. queryTime: 0x51,
  331. setAlarm: 0x52,
  332. queryAlarm: 0x54,
  333. setSleep: 0x56,
  334. querySleep: 0x57,
  335. setSleepAfterPlayPause: 0x5a,
  336. querySleepAfterPlayPause: 0x5b
  337. };
  338. // 设置设备颜色
  339. const CmdRgb = {
  340. query: 0x80,
  341. set: 0x81
  342. };
  343. // 电量相关命令
  344. const CmdKwh = {
  345. queryKwh: 0x22,
  346. setLowKwhWarningTone: 0x26,
  347. queryLowKwhWarningTone: 0x25
  348. };
  349. // 按键
  350. const CmdKey = {
  351. previousSong: 0x0a,
  352. nextSong: 0x0b,
  353. previousChannel: 0x0c,
  354. nextChannel: 0x0d,
  355. type: 0x21,
  356. pressShort: 0x02,
  357. pressLongUp: 0x04,
  358. pressLong: 0x08,
  359. pressDouble: 0x09,
  360. next: 0x01,
  361. previous: 0x02,
  362. changeProgram: 0x03
  363. };
  364. // 语音
  365. const CmdVoice = {
  366. startVoice: 0x01,
  367. jlStartVoice: 0x66,
  368. jlStopVoice: 0x68,
  369. jlStartVoiceResponse: 0x67,
  370. jlStopVoiceResponse: 0x69,
  371. jlReceiveVoiceData: 0x70,
  372. lxStartVoice: 0x71,
  373. lxStopVoice: 0x72,
  374. lxStartVoiceResponse: 0x40,
  375. lxStopVoiceResponse: 0x41,
  376. sjStartVoice: 0x23,
  377. sjStopVoice: 0x24
  378. };
  379. // 一周
  380. const CmdWeek = {
  381. sunday: 0x01,
  382. monday: 0x02,
  383. tuesday: 0x04,
  384. wednesday: 0x08,
  385. thursday: 0x10,
  386. friday: 0x20,
  387. saturday: 0x40,
  388. weeks: [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40],
  389. week2Cmd(weekCycle) {
  390. let result = 0;
  391. for (let i = 0; i < weekCycle.length; i++) {
  392. const item = weekCycle[i];
  393. if (item === EnumOpen.open) {
  394. result += this.weeks[i];
  395. }
  396. }
  397. return result;
  398. },
  399. cmd2Week(cmd) {
  400. const w = [0, 0, 0, 0, 0, 0, 0];
  401. for (let i = this.weeks.length - 1; i >= 0; i--) {
  402. const week = this.weeks[i];
  403. if (week <= cmd) {
  404. w[i] = EnumOpen.open;
  405. cmd -= week;
  406. }
  407. }
  408. return w;
  409. }
  410. };
  411. module.exports = {
  412. BtCmd,
  413. CmdBase,
  414. CmdEarPhone,
  415. CmdRtc,
  416. CmdRgb,
  417. CmdKwh,
  418. CmdKey,
  419. CmdVoice,
  420. CmdWeek
  421. };