nfc.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 包路径
  2. const package_NdefRecord = 'android.nfc.NdefRecord';
  3. const package_NdefMessage = 'android.nfc.NdefMessage';
  4. const package_TECH_DISCOVERED = 'android.nfc.action.TECH_DISCOVERED';
  5. const package_Intent = 'android.content.Intent';
  6. const package_Activity = 'android.app.Activity';
  7. const package_PendingIntent = 'android.app.PendingIntent';
  8. const package_IntentFilter = 'android.content.IntentFilter';
  9. const package_NfcAdapter = 'android.nfc.NfcAdapter';
  10. const package_Ndef = 'android.nfc.tech.Ndef';
  11. const package_NdefFormatable = 'android.nfc.tech.NdefFormatable';
  12. const package_Parcelable = 'android.os.Parcelable';
  13. const package_String = 'java.lang.String';
  14. let NfcAdapter;
  15. let NdefRecord;
  16. let NdefMessage;
  17. let readyRead = true; //开启读
  18. let noNFC = false;
  19. let techListsArray = [
  20. ['android.nfc.tech.IsoDep'],
  21. ['android.nfc.tech.NfcA'],
  22. ['android.nfc.tech.NfcB'],
  23. ['android.nfc.tech.NfcF'],
  24. ['android.nfc.tech.Nfcf'],
  25. ['android.nfc.tech.NfcV'],
  26. ['android.nfc.tech.NdefFormatable'],
  27. ['android.nfc.tech.MifareClassi'],
  28. ['android.nfc.tech.MifareUltralight']
  29. ];
  30. // 要写入的数据
  31. let text = '{id:8888,name:nfc,stie:wangqin.com}';
  32. let readResult = '';
  33. export default {
  34. listenNFCStatus: function() {
  35. console.log("---------listenNFCStatus--------------")
  36. let that = this;
  37. try {
  38. let main = plus.android.runtimeMainActivity();
  39. let Intent = plus.android.importClass('android.content.Intent');
  40. let Activity = plus.android.importClass('android.app.Activity');
  41. let PendingIntent = plus.android.importClass('android.app.PendingIntent');
  42. let IntentFilter = plus.android.importClass('android.content.IntentFilter');
  43. NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
  44. let nfcAdapter = NfcAdapter.getDefaultAdapter(main);
  45. if (nfcAdapter == null) {
  46. uni.showToast({
  47. title: '设备不支持NFC!',
  48. icon: 'none'
  49. })
  50. noNFC = true;
  51. return;
  52. }
  53. if (!nfcAdapter.isEnabled()) {
  54. uni.showToast({
  55. title: '请在系统设置中先启用NFC功能!',
  56. icon: 'none'
  57. });
  58. noNFC = true;
  59. return;
  60. } else {
  61. noNFC = false;
  62. }
  63. let intent = new Intent(main, main.getClass());
  64. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  65. let pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
  66. let ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
  67. ndef.addDataType("*/*");
  68. let intentFiltersArray = [ndef];
  69. //重点部分代码
  70. const promise = new Promise((resolve, reject) => {
  71. plus.globalEvent.addEventListener('newintent', function() {
  72. // 轮询调用 NFC
  73. // setTimeout(that.nfcRuning(resolve), 1000);
  74. setTimeout(() => {
  75. that.nfcRuning(resolve)
  76. }, 1000);
  77. });
  78. })
  79. nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
  80. return promise
  81. } catch (e) {
  82. }
  83. },
  84. nfcRuning: function(resolve) {
  85. // console.log("--------------nfcRuning---------------")
  86. NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
  87. NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
  88. let main = plus.android.runtimeMainActivity();
  89. let intent = main.getIntent();
  90. let that = this;
  91. if (package_TECH_DISCOVERED == intent.getAction()) {
  92. if (readyRead) {
  93. //这里通过read方法拿到NFC数据
  94. const id = that.read(intent);
  95. // console.log("id:--------"+id)
  96. // readyRead = false;
  97. //将数据返回出去
  98. resolve(id)
  99. }
  100. }
  101. },
  102. read(intent) {
  103. toast('请勿移开标签正在读取数据');
  104. let that = this;
  105. // NFC id
  106. let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
  107. let nfc_id = that.byteArrayToHexString(bytesId);
  108. return nfc_id;
  109. },
  110. byteArrayToHexString: function(inarray) { // converts byte arrays to string
  111. let i, j, inn;
  112. let hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
  113. let out = "";
  114. for (j = 0; j < inarray.length; ++j) {
  115. inn = inarray[j] & 0xff;
  116. i = (inn >>> 4) & 0x0f;
  117. out += hex[i];
  118. i = inn & 0x0f;
  119. out += hex[i];
  120. }
  121. return out;
  122. },
  123. }
  124. function toast(content) {
  125. uni.showToast({
  126. title: content,
  127. icon: 'none'
  128. })
  129. }