bt_cmd.js 13 KB

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