bluetoothUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import CryptoJS from "crypto-js";
  2. export function genReqSecretPkg() {
  3. const requet = packetData("REQUEST_SECRET_CMD", 0);
  4. return requet;
  5. }
  6. export function genOpDoorPkg() {
  7. var sessionKey = uni.getStorageSync("sessionKey");
  8. sessionKey = hexStringToUint8Array(sessionKey);
  9. const requet = packetData("OPEN_DOOR_CMD", 0, sessionKey);
  10. return requet;
  11. }
  12. export function genQuPowerPkg() {
  13. var sessionKey = uni.getStorageSync("sessionKey");
  14. sessionKey = hexStringToUint8Array(sessionKey);
  15. const requet = packetData("QUERY_POWER_CMD", 3, sessionKey);
  16. return requet;
  17. }
  18. export function gnQuDoorLogPkg() {
  19. var sessionKey = uni.getStorageSync("sessionKey");
  20. sessionKey = hexStringToUint8Array(sessionKey);
  21. const requet = packetData("QUERY_DOOR_LOG_CMD", 2, sessionKey);
  22. return requet;
  23. }
  24. //需要根据命令字段进行不同的包封装
  25. function packetData(command, id, sessionKey) {
  26. var cmd;
  27. var data;
  28. if (command == "REQUEST_SECRET_CMD") {
  29. cmd = 0x01;
  30. data = new Uint8Array();
  31. }
  32. if (command == "OPEN_DOOR_CMD") {
  33. cmd = 0x11;
  34. data = sessionKey;
  35. }
  36. if (command == "QUERY_POWER_CMD") {
  37. cmd = 0x04;
  38. data = sessionKey;
  39. }
  40. if (command == "QUERY_DOOR_LOG_CMD") {
  41. cmd = 0x12;
  42. data = sessionKey;
  43. }
  44. var packet = new Uint8Array(6 + data.length);
  45. // 设置开始码
  46. packet[0] = 0xa5;
  47. // 设置命令号
  48. packet[1] = cmd;
  49. // 设置包序号
  50. packet[2] = id;
  51. // 设置实际数据
  52. for (var j = 0; j < data.length; j++) {
  53. packet[6 + j] = data[j];
  54. }
  55. // 计算负载的校验和
  56. var chksum = 0;
  57. for (var i = 6; i < packet.length; i++) {
  58. chksum ^= packet[i];
  59. }
  60. packet[3] = chksum;
  61. // 设置负载长度的低位和高位
  62. var dataLength = data.length;
  63. packet[4] = dataLength & 0xff; // 低位
  64. packet[5] = (dataLength >> 8) & 0xff; // 高位
  65. return packet.buffer;
  66. }
  67. //分包延迟定时器
  68. export function Delayed(ms, res) {
  69. return new Promise(function (Resolve, Reject) {
  70. setTimeout(function () {
  71. Resolve(res);
  72. }, ms);
  73. });
  74. }
  75. //AES解密
  76. export function decryptAes(originalKey, encryptedData) {
  77. let key = hexStringToUint8Array(originalKey);
  78. let data = encryptedData;
  79. try {
  80. // 将密文和密钥转换为 WordArray 对象
  81. const ciphertext = CryptoJS.lib.WordArray.create(data);
  82. const keyHex = CryptoJS.lib.WordArray.create(key);
  83. // 进行解密
  84. const decrypted = CryptoJS.AES.decrypt({ ciphertext }, keyHex, {
  85. mode: CryptoJS.mode.ECB,
  86. padding: CryptoJS.pad.NoPadding,
  87. });
  88. const decryptedUint8Array = new Uint8Array(decrypted.words.length * 4);
  89. for (let i = 0; i < decrypted.words.length; i++) {
  90. decryptedUint8Array[i * 4] = (decrypted.words[i] >> 24) & 0xff;
  91. decryptedUint8Array[i * 4 + 1] = (decrypted.words[i] >> 16) & 0xff;
  92. decryptedUint8Array[i * 4 + 2] = (decrypted.words[i] >> 8) & 0xff;
  93. decryptedUint8Array[i * 4 + 3] = decrypted.words[i] & 0xff;
  94. }
  95. // 返回解密后的数据
  96. return decryptedUint8Array;
  97. } catch (e) {
  98. console.error("解密异常", e);
  99. return null;
  100. }
  101. }
  102. //字符串转Uint8
  103. export function hexStringToUint8Array(hexString) {
  104. // 去除字符串中的空格
  105. hexString = hexString.replace(/\s/g, "");
  106. // 使用正则表达式将字符串拆分成两个字符一组的数组
  107. const match = hexString.match(/.{1,2}/g);
  108. // 将每两个字符转换为一个字节的十六进制数
  109. const uint8Array = new Uint8Array(match.map((byte) => parseInt(byte, 16)));
  110. return uint8Array;
  111. }
  112. // 将后端传过来的蓝牙设备id转化为符合接口要求的id
  113. export function parseBELDeviceId(deviceId = "") {
  114. const arrayOfDeviceId = deviceId.toUpperCase().split("");
  115. let targetId = arrayOfDeviceId[0] + arrayOfDeviceId[1];
  116. for (let i = 2; i < arrayOfDeviceId.length; i += 2) {
  117. targetId = targetId + ":" + arrayOfDeviceId[i] + arrayOfDeviceId[i + 1];
  118. }
  119. return targetId;
  120. }