bt_cmd.js 13 KB

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