bt_cmd.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. // 校验设备
  132. static get checkDevice() {
  133. return this._build(CmdBase.checkDevice, StringUtil.string2ListInt(CmdBase.secretKey));
  134. }
  135. // 杰理语音开始回应
  136. static get jlStartVoiceResponse() {
  137. return this._build(CmdVoice.jlStartVoiceResponse);
  138. }
  139. // 杰理语音结束回应
  140. static get jlStopVoiceResponse() {
  141. return this._build(CmdVoice.jlStopVoiceResponse);
  142. }
  143. // 领芯语音
  144. static get lxStartVoiceResponse() {
  145. return this._build(CmdVoice.lxStartVoiceResponse);
  146. }
  147. // 杰理语音结束回应
  148. static get lxStopVoiceResponse() {
  149. return this._build(CmdVoice.lxStopVoiceResponse);
  150. }
  151. // b1开始语音回调
  152. static get b1StartVoiceResponse() {
  153. const data = [0x04, 0x80, 0x01, 0x00, 0x02];
  154. return new Uint8Array(data);
  155. }
  156. // b1结束语音回调
  157. static get b1StopVoiceResponse() {
  158. const data = [0x05, 0x80, 0x01, 0x00, 0x02];
  159. return new Uint8Array(data);
  160. }
  161. // 固定头部
  162. static get _header() {
  163. return [0x54, 0x44, 0x44, 0x48];
  164. }
  165. // 版本号
  166. static get _version() {
  167. return 0x01;
  168. }
  169. // 生成命令(固定头部+版本号+命令总长度+命令类型+)
  170. static _build(cmdType, otherCmd = [], isWriteChildCmdLength = true) {
  171. const cmd = [];
  172. // 固定头部
  173. cmd.push(...this._header);
  174. // 版本号
  175. cmd.push(this._version);
  176. // 命令类型
  177. cmd.push(cmdType);
  178. if (isWriteChildCmdLength) {
  179. // 子命令长度
  180. const childLength = this._int2Hex(otherCmd.length);
  181. cmd.push(childLength);
  182. }
  183. // 其他命令
  184. if (otherCmd.length > 0) {
  185. for (const element of otherCmd) {
  186. cmd.push(this._int2Hex(element));
  187. }
  188. }
  189. // 命令长度(位置在版本号之后)
  190. const length = cmd.length + 1;
  191. const l = this._int2Hex(length);
  192. cmd.splice(5, 0, l);
  193. return new Uint8Array(cmd);
  194. }
  195. // 打印cmd
  196. static printTLV(cmd, isSend = false) {
  197. let result = "";
  198. const key = isSend ? "发送" : "接收";
  199. if (isSend) {
  200. // console.log(`${key}原始-${cmd.toString()}`);
  201. }
  202. for (let i = 0; i < cmd.length; i++) {
  203. const cmdStr = `${this._int2HexString(cmd[i])}`.padStart(2, "0");
  204. result += cmdStr;
  205. }
  206. if (isSend) {
  207. mqttAddDebugCmd(`发送蓝牙指令:${cmd} \n ${result}`);
  208. }
  209. console.log(`${key}解析-[${result}]`);
  210. }
  211. static _int2Hex(value) {
  212. return value & 0xFF;
  213. }
  214. static _int2HexString(value) {
  215. return value.toString(16).toUpperCase();
  216. }
  217. }
  218. // 设备命令类型
  219. const CmdBase = {
  220. secretKey: "smart_A2F",
  221. queryVersion: 0x30,
  222. checkDevice: 0x28,
  223. checkDeviceSuccess: 0x27,
  224. setAutoPlay: 0x24,
  225. getAutoPlay: 0x23,
  226. queryChannelAngle: 0x38,
  227. switchLed: 0x39,
  228. queryVolume: 0x32,
  229. setVolume: 0x33,
  230. newFlag: "MW-Mate X\\(4G_WIFI\\)|MW-2AX\\(WIFI-N\\)|MW-SR1\\(4G_WIFI\\)|MW-SR1\\(4G_WIFI_MEIZU01\\)",
  231. // volumeMax: (new RegExp(`^(${CmdBase.newFlag})$`, 'i').test(DeviceManager.instance.clientType || "")) ? 0x20 : 0xF,
  232. parseVersion(version) {
  233. let ver;
  234. if (version <= 0) {
  235. ver = "1.0.0";
  236. } else {
  237. const split = version.toString().split("");
  238. if (split.length === 1) {
  239. split.unshift("0", "0");
  240. } else if (split.length === 2) {
  241. split.unshift("0");
  242. }
  243. ver = split.join(".");
  244. }
  245. console.log(`parseVersion======${version},,,,,,,${ver.toString()}`);
  246. return ver;
  247. }
  248. };
  249. // 耳机相关指令
  250. const CmdEarPhone = {
  251. queryLowDelayMode: 0xb2,
  252. setLowDelayMode: 0xb3,
  253. queryEQ: 0xb4,
  254. setEQ: 0xb5,
  255. queryEarPhoneKwh: 0xb1,
  256. queryCtrlStatus: 0xB6,
  257. setCtrlStatus: 0xB7,
  258. queryLowPower: 0xB8,
  259. setLowPowerMode: 0xB9
  260. };
  261. // 时间相关
  262. const CmdRtc = {
  263. setTime: 0x50,
  264. queryTime: 0x51,
  265. setAlarm: 0x52,
  266. queryAlarm: 0x54,
  267. setSleep: 0x56,
  268. querySleep: 0x57,
  269. setSleepAfterPlayPause: 0x5a,
  270. querySleepAfterPlayPause: 0x5b
  271. };
  272. // 设置设备颜色
  273. const CmdRgb = {
  274. query: 0x80,
  275. set: 0x81
  276. };
  277. // 电量相关命令
  278. const CmdKwh = {
  279. queryKwh: 0x22,
  280. setLowKwhWarningTone: 0x26,
  281. queryLowKwhWarningTone: 0x25
  282. };
  283. // 按键
  284. const CmdKey = {
  285. previousSong: 0x0a,
  286. nextSong: 0x0b,
  287. previousChannel: 0x0c,
  288. nextChannel: 0x0d,
  289. type: 0x21,
  290. pressShort: 0x02,
  291. pressLongUp: 0x04,
  292. pressLong: 0x08,
  293. pressDouble: 0x09,
  294. next: 0x01,
  295. previous: 0x02,
  296. changeProgram: 0x03
  297. };
  298. // 语音
  299. const CmdVoice = {
  300. startVoice: 0x01,
  301. jlStartVoice: 0x66,
  302. jlStopVoice: 0x68,
  303. jlStartVoiceResponse: 0x67,
  304. jlStopVoiceResponse: 0x69,
  305. jlReceiveVoiceData: 0x70,
  306. lxStartVoice: 0x71,
  307. lxStopVoice: 0x72,
  308. lxStartVoiceResponse: 0x40,
  309. lxStopVoiceResponse: 0x41,
  310. sjStartVoice: 0x23,
  311. sjStopVoice: 0x24
  312. };
  313. // 一周
  314. const CmdWeek = {
  315. sunday: 0x01,
  316. monday: 0x02,
  317. tuesday: 0x04,
  318. wednesday: 0x08,
  319. thursday: 0x10,
  320. friday: 0x20,
  321. saturday: 0x40,
  322. weeks: [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40],
  323. week2Cmd(weekCycle) {
  324. let result = 0;
  325. for (let i = 0; i < weekCycle.length; i++) {
  326. const item = weekCycle[i];
  327. if (item === EnumOpen.open) {
  328. result += this.weeks[i];
  329. }
  330. }
  331. return result;
  332. },
  333. cmd2Week(cmd) {
  334. const w = [0, 0, 0, 0, 0, 0, 0];
  335. for (let i = this.weeks.length - 1; i >= 0; i--) {
  336. const week = this.weeks[i];
  337. if (week <= cmd) {
  338. w[i] = EnumOpen.open;
  339. cmd -= week;
  340. }
  341. }
  342. return w;
  343. }
  344. };