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.

NFCDeviceWechat.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var config = require("./NFCConfig");
  2. var nfcTool = require("./NFCManagerTool");
  3. var scanTimeId; //定时扫描
  4. var connectTimeId; //定时链接卡片
  5. var transTimeId; //定时发送数据
  6. var timeOut = 10000;
  7. var isConnect = false;
  8. export function startScanDevice(callBack) {
  9. scanTimeId = setTimeout(res => {
  10. nfcTool.stopScanNfcDevice(res => {
  11. callBack.call(this, {
  12. code: config.timeOutCode(),
  13. msg: "扫描超时",
  14. data: null
  15. })
  16. })
  17. }, timeOut);
  18. nfcTool.startScanNfcDevice(res => {
  19. clearTimeout(scanTimeId);
  20. console.log("返回上层数据:" + JSON.stringify(res));
  21. callBack.call(this, res)
  22. })
  23. };
  24. export function stopScanDevice(callBack) {
  25. nfcTool.stopScanNfcDevice(res => {
  26. callBack.call(this, res)
  27. })
  28. };
  29. export function connectDevice(callBack) {
  30. if (isConnect) {
  31. callBack.call(this, {
  32. code: 10004,
  33. msg: "卡片已被连接",
  34. data: null
  35. });
  36. return
  37. }
  38. nfcTool.connectDevice(res => {
  39. if (res.code == 0) {
  40. isConnect = true;
  41. callBack.call(this, {
  42. code: config.successCode(),
  43. msg: "连接成功",
  44. data: null
  45. })
  46. } else {
  47. disConnectDevice(res => {});
  48. console.log("返回上层数据:" + JSON.stringify(res));
  49. callBack.call(this, {
  50. code: config.nfcErrorCode(),
  51. msg: "连接失败",
  52. data: null
  53. })
  54. }
  55. })
  56. };
  57. export function disConnectDevice(callBack) {
  58. nfcTool.disConnectDevice(res => {
  59. clearTimeout(scanTimeId);
  60. clearTimeout(connectTimeId);
  61. clearTimeout(transTimeId);
  62. isConnect = false;
  63. callBack.call(this, res)
  64. })
  65. };
  66. export function ICCTransCmd(cmdArr, callBack) {
  67. console.info("发送数据=============>>::" + cmdArr);
  68. if (!isConnect) {
  69. callBack.call(this, {
  70. code: config.errorCode(),
  71. msg: "未连接NFC标签",
  72. data: null
  73. });
  74. return
  75. }
  76. transTimeId = setTimeout(res => {
  77. callBack.call(this, {
  78. code: config.timeOutCode(),
  79. msg: "NFC连接超时",
  80. data: null
  81. })
  82. }, timeOut);
  83. nfcTool.sendMessageToDevice(cmdArr, res => {
  84. clearTimeout(transTimeId);
  85. console.log("返回上层数据:" + JSON.stringify(res));
  86. callBack.call(this, res)
  87. })
  88. };
  89. export function remove(callBack) {
  90. stopScanDevice(res => {
  91. disConnectDevice(res => {
  92. nfcTool.removeObj();
  93. callBack.call(this, {
  94. code: config.successCode(),
  95. msg: "断开NFC成功",
  96. data: null
  97. });
  98. })
  99. })
  100. }