Search code examples
androidnfcndefandroid-applicationrecord

Reading Simple Data Into a Variable Using NFC on Android


I am making a simple Android application. The primary function is, I want to read in data from an NFC tag. I already have the basic app built (not much more than the Hello World app). I'm using Eclipse. I have the Android Application Record on the tag, and the app successfully launches when my tag is scanned. I'm just not sure how to continue. I need to:

  • In addition to the AAR, have a four digit number stored in the tag.

  • When the tag is scanned, have the app open and the four digit number stored in a local variable (using intent I think?).

Any help appreciated, as well as the simplest code possible that'll get the job done. This is my first Android app and first time using Java.


Solution

  • In your launcher Activity you should check the intent type if it is the same as you stored in the tag - getIntent.getType(). Then you can read data from the tag using this code:

    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    NdefMessage msg = (NdefMessage)rawMsgs[0];
    NdefRecord cardRecord = msg.getRecords()[0];
    String message = new String(cardRecord.getPayload());
    

    If its a number, then you need to parse it from message. I hope this helps you.