小程序蓝牙通信例子
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.7 KiB

  1. function formatTime(date) {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. function formatNumber(n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. function uint8ArrayToString(fileData){
  15. let dataString = "";
  16. for (let i = 0; i < fileData.length; i++) {
  17. dataString += String.fromCharCode(fileData[i]);
  18. }
  19. return dataString
  20. }
  21. function stringToUint8Array(str){
  22. let arr = [];
  23. for (let i = 0, j = str.length; i < j; ++i) {
  24. arr.push(str.charCodeAt(i));
  25. }
  26. let tmpUint8Array = new Uint8Array(arr);
  27. return tmpUint8Array
  28. }
  29. function stringToUint8ArrayWithPadding(str, length, padding){
  30. let arr = [];
  31. for (let i = 0, j = str.length; i < j; ++i) {
  32. arr.push(str.charCodeAt(i));
  33. }
  34. for (let i = str.length; i < length; i++) {
  35. arr.push(padding);
  36. }
  37. let tmpUint8Array = new Uint8Array(arr);
  38. return tmpUint8Array
  39. }
  40. // 微信官方给的ArrayBuffer转十六进制示例
  41. function ab2hex(buffer) {
  42. let hexArr = Array.prototype.map.call(
  43. new Uint8Array(buffer),
  44. function (bit) {
  45. return ('00' + bit.toString(16)).slice(-2)
  46. }
  47. )
  48. return hexArr.join(',');
  49. }
  50. //转成可展会的文字
  51. function hexCharCodeToStr(hexCharCodeStr) {
  52. let trimedStr = hexCharCodeStr.trim();
  53. let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
  54. let len = rawStr.length;
  55. let curCharCode;
  56. let resultStr = [];
  57. for (let i = 0; i < len; i = i + 2) {
  58. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  59. resultStr.push(String.fromCharCode(curCharCode));
  60. }
  61. return resultStr.join('');
  62. }
  63. //转换成需要的格式
  64. function buf2string(buffer) {
  65. let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  66. return arr.map((char, i) => {
  67. return String.fromCharCode(char);
  68. }).join('');
  69. }
  70. function decodeHexNibble(c) {
  71. if (c >= 48 && c <= 57) {
  72. return c - 48;
  73. } else if (c >= 65 && c <= 70) {
  74. return c - 65 + 10;
  75. } else {
  76. return c >= 97 && c <= 102 ? c - 97 + 10 : -1;
  77. }
  78. }
  79. function decodeHexByte(s, pos) {
  80. let hi = decodeHexNibble(s.charCodeAt(pos));
  81. let lo = decodeHexNibble(s.charCodeAt(pos + 1));
  82. return ((hi << 4) + lo);
  83. }
  84. function hexStringToBytesWithPadding(str, byteLen, padding) {
  85. let len = str.length
  86. let length = 2 * byteLen
  87. if (length >= len && (length & 1) == 0) {
  88. if (length == 0) {
  89. return [0];
  90. } else {
  91. let bytes = new Uint8Array(length >>> 1);
  92. for(let i = 0; i < len; i += 2) {
  93. bytes[i >>> 1] = decodeHexByte(str, i);
  94. }
  95. for(let i = 0; i < byteLen-len/2; i++)
  96. bytes[len/2+i] = padding;
  97. return bytes;
  98. }
  99. }
  100. else
  101. return [0];
  102. }
  103. function timeStringToBytes(timeStr, timeLen) {
  104. let s = timeStr;
  105. if(s.length < timeLen)
  106. {
  107. let tmp = timeLen-s.length
  108. for(let i = 0; i< tmp; i++)
  109. s = "0" + s;
  110. }
  111. else
  112. s = timeStr.substr(s.length-timeLen, s.length);
  113. let start = 0
  114. let length = s.length
  115. if (length >= 0 && (length & 1) == 0) {
  116. if (length == 0) {
  117. return new byte[0];
  118. } else {
  119. let bytes = new Uint8Array(length >>> 1);
  120. for(let i = 0; i < length; i += 2) {
  121. bytes[i >>> 1] = decodeHexByte(s, start + i);
  122. }
  123. return bytes;
  124. }
  125. }
  126. else
  127. return [0];
  128. }
  129. function time40ToBytes(dt) {
  130. var cs = dt.substring(0,4) + "/" + dt.substring(4,6) + "/" + dt.substring(6,8) + " " + dt.substring(8,10) + ":" + dt.substring(10,12)
  131. + ":" + dt.substring(12,14) + ":100";
  132. var milliTs = new Date(cs).getTime();
  133. var secondTs = milliTs/1000
  134. let ts = secondTs.toString(16)
  135. let t = timeStringToBytes(ts, 10)
  136. return t;
  137. }
  138. module.exports = {
  139. formatTime: formatTime,
  140. uint8ArrayToString: uint8ArrayToString,
  141. stringToUint8Array: stringToUint8Array,
  142. stringToUint8ArrayWithPadding: stringToUint8ArrayWithPadding,
  143. ab2hex: ab2hex,
  144. hexCharCodeToStr,
  145. hexCharCodeToStr: buf2string,
  146. decodeHexNibble,
  147. decodeHexNibble: decodeHexByte,
  148. hexStringToBytesWithPadding: hexStringToBytesWithPadding,
  149. timeStringToBytes: timeStringToBytes,
  150. time40ToBytes: time40ToBytes
  151. }