Guest User

Untitled

a guest
Aug 20th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. Android NFC, do a null check in onCreate?
  2. // Setup an intent filter for all MIME based dispatches
  3. IntentFilter nfcv = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
  4. try {
  5. nfcv.addDataType("*/*");
  6. } catch (MalformedMimeTypeException e) {
  7. throw new RuntimeException("fail", e);
  8. }
  9. mFilters = new IntentFilter[] {
  10. nfcv,
  11. };
  12.  
  13. // Setup a tech list for all NfcF tags
  14. mTechLists = new String[][] { new String[] { NfcV.class.getName() } };
  15.  
  16. //mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
  17.  
  18. NfcV nfcMessage = NfcV.get(new TagGet().getTag());
  19.  
  20. byte[] data = new byte[2048]; //tag length can't be any larger
  21. String value = "";
  22. try {
  23. data = nfcMessage.transceive(new byte[2048]);
  24. value = new String(data);
  25. } catch (IOException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28.  
  29. public class Android_nfc_ibox extends Activity implements Runnable {
  30.  
  31.  
  32.  
  33.  
  34. NfcAdapter mNfcAdapter;
  35. private String[][] mTechLists;
  36. PendingIntent pendingIntent;
  37. Tag tag;
  38. NfcA mTag;
  39.  
  40. /** Called when the activity is first created. */
  41. @Override
  42. public void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44.  
  45. // Initialize the NFC adapter
  46. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  47. if (mNfcAdapter != null) {
  48. dialog_text.append("Tap an NFC tag for accessnr");
  49. } else {
  50. dialog_text.append("This phone is not NFC enablednr");
  51. }
  52.  
  53. // Create the PendingIntent object which will contain the details of the tag that has been scanned
  54. pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  55.  
  56. // Setup a tech list for all desired tag types
  57. mTechLists = new String[][] { new String[] { NfcA.class.getName() } };
  58.  
  59. }
  60.  
  61. /** Re-enable the tag dispatch if the app is in the foreground */
  62. @Override
  63. public void onResume() {
  64. super.onResume();
  65. if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, pendingIntent, null, mTechLists);
  66. }
  67.  
  68. /** Disable the tag dispatch when the app is no longer in the foreground */
  69. @Override
  70. public void onPause() {
  71. super.onPause();
  72. if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
  73. }
  74.  
  75. /** A tag has been discovered */
  76. @Override
  77. public void onNewIntent(Intent intent){
  78.  
  79. // get the tag object for the discovered tag
  80. Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  81.  
  82. // try and get the MifareUltralight instance for this tag
  83. mTag = NfcV.get(tag);
  84.  
  85. // if null then this wasn't a NfcV tag so wait for next time
  86. if(mTag == null){
  87. dialog_text.append("Not a Nfc V tagnr");
  88. }
  89.  
  90. // Start the tag communications thread
  91. Thread myThread = new Thread(this);
  92. myThread.start();
  93.  
  94. }
  95. }
  96.  
  97. // (we could create other threads for other types of tags)
  98. public void run(){
  99. // try to connect to the Nfc V tag
  100. try{
  101.  
  102. mTag.connect();
  103. }catch(IOException e){
  104. //handle the error here
  105. }
  106.  
  107. //this will send raw data
  108. //send the values you want in the byte[]
  109. //just add the raw hex values with commas
  110. //pageBuffer is an array that will hold the response
  111. try{
  112. pageBuffer = mTag.transceive(new byte[] {0x11, 0x24, 0x11});
  113. }catch(IOException e){
  114. //handle error here
  115. }
  116. }
Add Comment
Please, Sign In to add comment