ailink ble wifi 小程序配网演示demo
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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function inArray(arr, key, val) {
  2. if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
  3. return -1
  4. }
  5. for (let i = 0; i < arr.length; i++) {
  6. if (!key) {
  7. if (arr[i] == val) {
  8. return i
  9. }
  10. } else if (arr[i][key] === val) {
  11. return i
  12. }
  13. }
  14. return -1;
  15. }
  16. // ArrayBuffer转16进度字符串示例
  17. function ab2hex(buffer, split) {
  18. var hexArr = Array.prototype.map.call(
  19. new Uint8Array(buffer),
  20. function (bit) {
  21. return ('00' + bit.toString(16)).slice(-2)
  22. }
  23. )
  24. return hexArr.join(split);
  25. }
  26. const {TextDecoder, TextEncoder} = require('./encoding')
  27. function hex2str(arr) {
  28. let decoder = new TextDecoder('utf8')
  29. let uint8 = new Uint8Array(arr)
  30. let res = decoder.decode(uint8)
  31. return res
  32. // if (!arr.length) {
  33. // return
  34. // }
  35. // let ret = ""
  36. // arr.forEach(item=>{
  37. // ret += '%' + item.toString(16)
  38. // })
  39. // console.log(ret)
  40. // return decodeURIComponent(ret)
  41. }
  42. function str2hex(str) {
  43. let encoder = new TextEncoder('utf8')
  44. return encoder.encode(str)
  45. // let temp = encodeURIComponent(str).split('%').slice(1)
  46. // let ret = temp.map(item=>{
  47. // return parseInt(item, 16)
  48. // })
  49. // return ret
  50. }
  51. module.exports = {
  52. inArray,
  53. ab2hex,
  54. hex2str,
  55. str2hex,
  56. }