bt_cmd.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 get queryVersion() {
  22. return this._build(CmdBase.queryVersion, [0x0]);
  23. }
  24. // 查询电量 电量值 0-9,0对应10%,9对应100%,10充电
  25. static get queryKwh() {
  26. return this._build(CmdKwh.queryKwh, [0x0]);
  27. }
  28. // 查询耳机电量
  29. static get queryKwhEarPhone() {
  30. return this._build(CmdEarPhone.queryEarPhoneKwh, [0x0]);
  31. }
  32. // 查询EQ音效
  33. static get queryEQ() {
  34. return this._build(CmdEarPhone.queryEQ, [0x0]);
  35. }
  36. // 设置EQ音效
  37. static setEQ(list) {
  38. return this._build(CmdEarPhone.setEQ, list);
  39. }
  40. // B5耳机-低电耗模式
  41. static get queryLowPower() {
  42. return this._build(CmdEarPhone.queryLowPower, [0x0]);
  43. }
  44. static setLowPowerMode(isOpen) {
  45. return this._build(CmdEarPhone.setLowPowerMode, [isOpen]);
  46. }
  47. // B5耳机-自定义操作手势
  48. static get queryCtrlStatus() {
  49. return this._build(CmdEarPhone.queryCtrlStatus, [0x0]);
  50. }
  51. static setCtrlStatus(singleClick, doubleClick, longClick) {
  52. return this._build(CmdEarPhone.setCtrlStatus, [singleClick, doubleClick, longClick]);
  53. }
  54. // 查询低延时模式
  55. static get queryLowDelayMode() {
  56. return this._build(CmdEarPhone.queryLowDelayMode, [0x0]);
  57. }
  58. // 低延时模式
  59. static setLowDelayMode(open, mode) {
  60. return this._build(CmdEarPhone.setLowDelayMode, [open, mode]);
  61. }
  62. // 低电量提示音
  63. static get 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 get getAutoPlay() {
  74. return this._build(CmdBase.getAutoPlay, [0x00]);
  75. }
  76. // 查询音量
  77. static get queryVolume() {
  78. return this._build(CmdBase.queryVolume, [0x00]);
  79. }
  80. static setVolume(volume) {
  81. return this._build(CmdBase.setVolume, [volume]);
  82. }
  83. // 查询闹钟
  84. static get 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 get querySleep() {
  94. return this._build(CmdRtc.querySleep, [0x00]);
  95. }
  96. static get querySleepAfterPlayPause() {
  97. return this._build(CmdRtc.querySleepAfterPlayPause, [0x00]);
  98. }
  99. static setSleepAfterPlayPause(time) {
  100. return this._build(CmdRtc.setSleepAfterPlayPause, [time]);
  101. }
  102. // 设置休眠
  103. static setSleep(switchStatus, hour, minutes) {
  104. return this._build(CmdRtc.setSleep, [switchStatus, hour, minutes]);
  105. }
  106. // 设置时间
  107. static get setTime() {
  108. const y = TimeUtil.getYear();
  109. const year = Math.floor(y / 100);
  110. const _year = y % 100;
  111. const month = TimeUtil.getMonth();
  112. const day = TimeUtil.getDay();
  113. const hour = TimeUtil.getHour();
  114. const minutes = TimeUtil.getMinute();
  115. const seconds = TimeUtil.getSecond();
  116. console.log(`setTime========${year},${_year},${month},${day},${hour},${minutes},${seconds}`);
  117. return this._build(CmdRtc.setTime, [year, _year, month, day, hour, minutes, seconds]);
  118. }
  119. // 查询时间
  120. static get queryTime() {
  121. return this._build(CmdRtc.queryTime, [0x00]);
  122. }
  123. // 设置RGB
  124. static setRGB(r, g, b) {
  125. return this._build(CmdRgb.set, [r, g, b]);
  126. }
  127. // 查询RGB
  128. static get queryRGB() {
  129. return this._build(CmdRgb.query, [0x0]);
  130. }
  131. static stringToUint8Array(str) {
  132. // 54 44 44 48 01 11 28 09 73 6d6172745f413246
  133. // const encoder = new TextEncoder();
  134. // const uint8Array = encoder.encode(str);
  135. // return uint8Array;
  136. return [0x73, 0x6d, 0x61, 0x72, 0x74, 0x5f, 0x41, 0x32, 0x46]
  137. }
  138. // 校验设备
  139. static checkDevice() {
  140. let secretKey = this.stringToUint8Array(CmdBase.secretKey)
  141. console.log(`checkDevice========${secretKey}`);
  142. return this._build(CmdBase.checkDevice, secretKey);
  143. }
  144. // 杰理语音开始回应
  145. static get jlStartVoiceResponse() {
  146. return this._build(CmdVoice.jlStartVoiceResponse);
  147. }
  148. // 杰理语音结束回应
  149. static get jlStopVoiceResponse() {
  150. return this._build(CmdVoice.jlStopVoiceResponse);
  151. }
  152. // 领芯语音
  153. static get lxStartVoiceResponse() {
  154. return this._build(CmdVoice.lxStartVoiceResponse);
  155. }
  156. // 杰理语音结束回应
  157. static get lxStopVoiceResponse() {
  158. return this._build(CmdVoice.lxStopVoiceResponse);
  159. }
  160. // b1开始语音回调
  161. static get b1StartVoiceResponse() {
  162. const data = [0x04, 0x80, 0x01, 0x00, 0x02];
  163. return new Uint8Array(data);
  164. }
  165. // b1结束语音回调
  166. static get b1StopVoiceResponse() {
  167. const data = [0x05, 0x80, 0x01, 0x00, 0x02];
  168. return new Uint8Array(data);
  169. }
  170. // 固定头部
  171. static get _header() {
  172. return [0x54, 0x44, 0x44, 0x48];
  173. }
  174. // 版本号
  175. static get _version() {
  176. return 0x01;
  177. }
  178. // 生成命令(固定头部+版本号+命令总长度+命令类型+)
  179. static _build(cmdType, otherCmd = [], isWriteChildCmdLength = true) {
  180. const cmd = [];
  181. // 固定头部
  182. cmd.push(...this._header);
  183. // 版本号
  184. cmd.push(this._version);
  185. // 命令类型
  186. cmd.push(cmdType);
  187. if (isWriteChildCmdLength) {
  188. // 子命令长度
  189. const childLength = this._int2Hex(otherCmd.length);
  190. cmd.push(childLength);
  191. }
  192. // 其他命令
  193. if (otherCmd.length > 0) {
  194. for (const element of otherCmd) {
  195. cmd.push(this._int2Hex(element));
  196. }
  197. }
  198. // 命令长度(位置在版本号之后)
  199. const length = cmd.length + 1;
  200. const l = this._int2Hex(length);
  201. cmd.splice(5, 0, l);
  202. return new Uint8Array(cmd);
  203. }
  204. // 打印cmd
  205. static printTLV(cmd, isSend = false) {
  206. let result = "";
  207. const key = isSend ? "发送" : "接收";
  208. if (isSend) {
  209. // console.log(`${key}原始-${cmd.toString()}`);
  210. }
  211. for (let i = 0; i < cmd.length; i++) {
  212. const cmdStr = `${this._int2HexString(cmd[i])}`.padStart(2, "0");
  213. result += cmdStr;
  214. }
  215. if (isSend) {
  216. mqttAddDebugCmd(`发送蓝牙指令:${cmd} \n ${result}`);
  217. }
  218. console.log(`${key}解析- [${result}]`);
  219. }
  220. static _int2Hex(value) {
  221. return value & 0xFF;
  222. }
  223. static _int2HexString(value) {
  224. return value.toString(16).toUpperCase();
  225. }
  226. }
  227. // 设备命令类型
  228. const CmdBase = {
  229. secretKey: "smart_A2F",
  230. queryVersion: 0x30,
  231. checkDevice: 0x28,
  232. checkDeviceSuccess: 0x27,
  233. setAutoPlay: 0x24,
  234. getAutoPlay: 0x23,
  235. queryChannelAngle: 0x38,
  236. switchLed: 0x39,
  237. queryVolume: 0x32,
  238. setVolume: 0x33,
  239. newFlag: "MW-Mate X\\(4G_WIFI\\)|MW-2AX\\(WIFI-N\\)|MW-SR1\\(4G_WIFI\\)|MW-SR1\\(4G_WIFI_MEIZU01\\)",
  240. // volumeMax: (new RegExp(`^(${CmdBase.newFlag})$`, 'i').test(DeviceManager.instance.clientType || "")) ? 0x20 : 0xF,
  241. volumeMax: 15,
  242. parseVersion(version) {
  243. let ver;
  244. if (version <= 0) {
  245. ver = "1.0.0";
  246. } else {
  247. const split = version.toString().split("");
  248. if (split.length === 1) {
  249. split.unshift("0", "0");
  250. } else if (split.length === 2) {
  251. split.unshift("0");
  252. }
  253. ver = split.join(".");
  254. }
  255. console.log(`parseVersion======${version},,,,,,,${ver.toString()}`);
  256. return ver;
  257. }
  258. };
  259. // 耳机相关指令
  260. const CmdEarPhone = {
  261. queryLowDelayMode: 0xb2,
  262. setLowDelayMode: 0xb3,
  263. queryEQ: 0xb4,
  264. setEQ: 0xb5,
  265. queryEarPhoneKwh: 0xb1,
  266. queryCtrlStatus: 0xB6,
  267. setCtrlStatus: 0xB7,
  268. queryLowPower: 0xB8,
  269. setLowPowerMode: 0xB9
  270. };
  271. // 时间相关
  272. const CmdRtc = {
  273. setTime: 0x50,
  274. queryTime: 0x51,
  275. setAlarm: 0x52,
  276. queryAlarm: 0x54,
  277. setSleep: 0x56,
  278. querySleep: 0x57,
  279. setSleepAfterPlayPause: 0x5a,
  280. querySleepAfterPlayPause: 0x5b
  281. };
  282. // 设置设备颜色
  283. const CmdRgb = {
  284. query: 0x80,
  285. set: 0x81
  286. };
  287. // 电量相关命令
  288. const CmdKwh = {
  289. queryKwh: 0x22,
  290. setLowKwhWarningTone: 0x26,
  291. queryLowKwhWarningTone: 0x25
  292. };
  293. // 按键
  294. const CmdKey = {
  295. previousSong: 0x0a,
  296. nextSong: 0x0b,
  297. previousChannel: 0x0c,
  298. nextChannel: 0x0d,
  299. type: 0x21,
  300. pressShort: 0x02,
  301. pressLongUp: 0x04,
  302. pressLong: 0x08,
  303. pressDouble: 0x09,
  304. next: 0x01,
  305. previous: 0x02,
  306. changeProgram: 0x03
  307. };
  308. // 语音
  309. const CmdVoice = {
  310. startVoice: 0x01,
  311. jlStartVoice: 0x66,
  312. jlStopVoice: 0x68,
  313. jlStartVoiceResponse: 0x67,
  314. jlStopVoiceResponse: 0x69,
  315. jlReceiveVoiceData: 0x70,
  316. lxStartVoice: 0x71,
  317. lxStopVoice: 0x72,
  318. lxStartVoiceResponse: 0x40,
  319. lxStopVoiceResponse: 0x41,
  320. sjStartVoice: 0x23,
  321. sjStopVoice: 0x24
  322. };
  323. // 一周
  324. const CmdWeek = {
  325. sunday: 0x01,
  326. monday: 0x02,
  327. tuesday: 0x04,
  328. wednesday: 0x08,
  329. thursday: 0x10,
  330. friday: 0x20,
  331. saturday: 0x40,
  332. weeks: [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40],
  333. week2Cmd(weekCycle) {
  334. let result = 0;
  335. for (let i = 0; i < weekCycle.length; i++) {
  336. const item = weekCycle[i];
  337. if (item === EnumOpen.open) {
  338. result += this.weeks[i];
  339. }
  340. }
  341. return result;
  342. },
  343. cmd2Week(cmd) {
  344. const w = [0, 0, 0, 0, 0, 0, 0];
  345. for (let i = this.weeks.length - 1; i >= 0; i--) {
  346. const week = this.weeks[i];
  347. if (week <= cmd) {
  348. w[i] = EnumOpen.open;
  349. cmd -= week;
  350. }
  351. }
  352. return w;
  353. }
  354. };
  355. module.exports = {
  356. BtCmd,
  357. CmdBase,
  358. CmdEarPhone,
  359. CmdRtc,
  360. CmdRgb,
  361. CmdKwh,
  362. CmdKey,
  363. CmdVoice,
  364. CmdWeek
  365. };