小程序蓝牙通信例子
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.

153 lines
3.8 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. // 微信官方给的ArrayBuffer转十六进制示例
  30. function ab2hex(buffer) {
  31. let hexArr = Array.prototype.map.call(
  32. new Uint8Array(buffer),
  33. function (bit) {
  34. return ('00' + bit.toString(16)).slice(-2)
  35. }
  36. )
  37. return hexArr.join(',');
  38. }
  39. //转成可展会的文字
  40. function hexCharCodeToStr(hexCharCodeStr) {
  41. let trimedStr = hexCharCodeStr.trim();
  42. let rawStr = trimedStr.substr(0, 2).toLowerCase() === '0x' ? trimedStr.substr(2) : trimedStr;
  43. let len = rawStr.length;
  44. let curCharCode;
  45. let resultStr = [];
  46. for (let i = 0; i < len; i = i + 2) {
  47. curCharCode = parseInt(rawStr.substr(i, 2), 16);
  48. resultStr.push(String.fromCharCode(curCharCode));
  49. }
  50. return resultStr.join('');
  51. }
  52. //转换成需要的格式
  53. function buf2string(buffer) {
  54. let arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
  55. return arr.map((char, i) => {
  56. return String.fromCharCode(char);
  57. }).join('');
  58. }
  59. function decodeHexNibble(c) {
  60. if (c >= 48 && c <= 57) {
  61. return c - 48;
  62. } else if (c >= 65 && c <= 70) {
  63. return c - 65 + 10;
  64. } else {
  65. return c >= 97 && c <= 102 ? c - 97 + 10 : -1;
  66. }
  67. }
  68. function decodeHexByte(s, pos) {
  69. let hi = decodeHexNibble(s.charCodeAt(pos));
  70. let lo = decodeHexNibble(s.charCodeAt(pos + 1));
  71. return ((hi << 4) + lo);
  72. }
  73. function hexStringToBytesWithPadding(str, byteLen, padding) {
  74. let len = str.length
  75. let length = 2 * byteLen
  76. if (length >= len && (length & 1) == 0) {
  77. if (length == 0) {
  78. return [0];
  79. } else {
  80. let bytes = new Uint8Array(length >>> 1);
  81. for(let i = 0; i < len; i += 2) {
  82. bytes[i >>> 1] = decodeHexByte(str, i);
  83. }
  84. for(let i = 0; i < byteLen-len/2; i++)
  85. bytes[len/2+i] = padding;
  86. return bytes;
  87. }
  88. }
  89. else
  90. return [0];
  91. }
  92. function hexStringToBytes(str) {
  93. let s = str;
  94. if(s.length < 12)
  95. {
  96. let tmp = 12-s.length
  97. for(let i = 0; i< tmp; i++)
  98. s = "0" + s;
  99. }
  100. else
  101. s = str.substr(s.length-12, s.length);
  102. let start = 0
  103. let length = s.length
  104. if (length >= 0 && (length & 1) == 0) {
  105. if (length == 0) {
  106. return new byte[0];
  107. } else {
  108. let bytes = new Uint8Array(length >>> 1);
  109. for(let i = 0; i < length; i += 2) {
  110. bytes[i >>> 1] = decodeHexByte(s, start + i);
  111. }
  112. return bytes;
  113. }
  114. }
  115. else
  116. return [0];
  117. }
  118. module.exports = {
  119. formatTime: formatTime,
  120. uint8ArrayToString: uint8ArrayToString,
  121. stringToUint8Array: stringToUint8Array,
  122. ab2hex: ab2hex,
  123. hexCharCodeToStr,
  124. hexCharCodeToStr: buf2string,
  125. decodeHexNibble,
  126. decodeHexNibble: decodeHexByte,
  127. hexStringToBytesWithPadding: hexStringToBytesWithPadding,
  128. hexStringToBytes: hexStringToBytes
  129. }