native-nfc-util.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //nfc读取
  2. var NfcAdapter;
  3. var NdefRecord;
  4. var NdefMessage;
  5. //标识是否开始读取
  6. let readyRead = true;
  7. //回调函数
  8. var readback;
  9. function listenNFCStatus(readCallback) {
  10. readback = readCallback;
  11. try{
  12. var main = plus.android.runtimeMainActivity();
  13. var Intent = plus.android.importClass('android.content.Intent');
  14. var Activity = plus.android.importClass('android.app.Activity');
  15. var PendingIntent = plus.android.importClass('android.app.PendingIntent');
  16. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  17. NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
  18. var nfcAdapter = NfcAdapter.getDefaultAdapter(main);
  19. var intent = new Intent(main, main.getClass());
  20. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  21. var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
  22. var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
  23. ndef.addDataType("*/*");
  24. var intentFiltersArray = [ndef];
  25. var techListsArray = [
  26. ["android.nfc.tech.IsoDep"],
  27. ["android.nfc.tech.NfcA"],
  28. ["android.nfc.tech.NfcB"],
  29. ["android.nfc.tech.NfcF"],
  30. ["android.nfc.tech.Nfcf"],
  31. ["android.nfc.tech.NfcV"],
  32. ["android.nfc.tech.NdefFormatable"],
  33. ["android.nfc.tech.MifareClassi"],
  34. ["android.nfc.tech.MifareUltralight"]
  35. ];
  36. plus.globalEvent.addEventListener("newintent",
  37. function() {
  38. console.error('newintent');
  39. setTimeout(handle_nfc_data1, 1000);
  40. }, false);
  41. plus.globalEvent.addEventListener("pause", function(e) {
  42. if (nfcAdapter) {
  43. nfcAdapter.disableForegroundDispatch(main);
  44. console.log('pause');
  45. }
  46. }, false);
  47. plus.globalEvent.addEventListener("resume", function(e) {
  48. if (nfcAdapter) {
  49. console.log('resume');
  50. nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
  51. }
  52. }, false);
  53. nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
  54. }catch(e){
  55. console.error(e);
  56. }
  57. }
  58. function handle_nfc_data1()
  59. {
  60. NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
  61. NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
  62. var main = plus.android.runtimeMainActivity();
  63. var intent = main.getIntent();
  64. console.log("action type:" + intent.getAction());
  65. if("android.nfc.action.TECH_DISCOVERED" == intent.getAction()){
  66. if(readyRead){
  67. __read(intent);
  68. // readyRead = false;
  69. }
  70. }
  71. }
  72. function showToast(msg){
  73. plus.nativeUI.toast(msg);
  74. }
  75. function __read(intent){
  76. try{
  77. var content = "";
  78. var tag = plus.android.importClass("android.nfc.Tag");
  79. tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  80. var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
  81. console.log("bytesId:"+(bytesId));
  82. content +="卡片字节数组ID:"+tag.getId()+"<br/>";
  83. content +="卡片16进制ID:"+ bytesToHexString(tag.getId())+"<br/>";
  84. var tagid = reverseTwo(bytesToHexString(tag.getId()));
  85. content +="卡片16进制翻转ID:"+tagid+"<br/>";
  86. content +="卡片10进制卡号:"+parseInt(tagid, 16)+"<br/>";
  87. console.log("content:"+content);
  88. readback(bytesToHexString(tag.getId()));
  89. }catch(e){
  90. //TODO handle the exception
  91. }
  92. }
  93. function bytesToHexString(inarray){
  94. var i, j, x;
  95. var hex = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
  96. "B", "C", "D", "E", "F" ];
  97. var out = "";
  98. for (j = 0; j < inarray.length; ++j) {
  99. x = parseInt(inarray[j]) & 0xff;
  100. i = (x >> 4) & 0x0f;
  101. out += hex[i];
  102. i = x & 0x0f;
  103. out += hex[i];
  104. }
  105. return out;
  106. }
  107. function reverseTwo(str)
  108. {
  109. var str1 = "";
  110. for(var i = 1; i <= str.length; i++){
  111. str1 +=str[i-1];
  112. if(i%2==0){
  113. if(i == str.length){
  114. break;
  115. }
  116. str1+=":";
  117. }
  118. }
  119. var str2 = "";
  120. for(var i = str1.split(":").length-1; i >= 0 ; i--){
  121. str2+= str1.split(":")[i];
  122. }
  123. return str2;
  124. }
  125. export default {listenNFCStatus}