bt_cmd.js 13 KB

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