Skip to content

ReadUtility

Daneo van Overloop edited this page Jun 17, 2014 · 2 revisions

Android NFC Library

Home Reading NFC Tags Writing NFC Tags Creating NdefMessages Exception Handling
How it works Reading Writing NdefMessages Exceptions

Read Utility

For reading an NFC tag we have added some convenience methods.

When you extend the NFCTechDiscoveredActivity :

	public void onNewIntent(Intent paramIntent) {
		super.onNewIntent(paramIntent);
		for (String data : getNfcMessages()) {
    	    Toast.makeText(this, data + "", Toast.LENGTH_SHORT).show();
    	}
	}

If you do not, then the following snippets provide you with the ability to read the tag.

	public void onNewIntent(Intent paramIntent) {
		super.onNewIntent(paramIntent);
		for (String data : mNfcReadUtility.readFromTagWithMap(paramIntent).values())
		{
			Toast.makeText(this,data,Toast.LENGTH_SHORT).show();
		}
	}
	public void onNewIntent(Intent paramIntent) {
		super.onNewIntent(paramIntent);
		SparseArray<String> messages = mNfcReadUtility.readFromTagWithSparseArray(paramIntent);
  		for (int i = 0; i < messages.size() ; i++ )
 	    {
 	    	Toast.makeText(this,messages.valueAt(i),Toast.LENGTH_SHORT).show();
    	}

Somewhere you will probably define a field NfcReadUtility mNfcReadUtility = new NfcReadUtilityImpl();

As you'll see, you can use either a map or a SparseArray. The SparseArray allows us to avoid a lot of the overhead Map produces due to the unboxing and relying on an actual Object for each key entry, it still maintains a feature to index the records on type.

When filtering on the Nfc Payload Type

As the NfcType is used as an index in the SparseArray, you can just execute the following line of code in order to get the messages of the type you're interested in :

 String website  = mNfcReadUtility.readFromTag(paramIntent).get(NfcPayloadHeader.HTTP);

This will return either null when there is no such record, or the actual record in the tag. All of the currently known payload headers are recorded in the class NfcPayloadHeader, so use them to your advantage !

When you actually don't care about the Nfc Payload Type

With this in mind, it might come in handy to have a method like this defined somewhere, in case you do not care about the NFC type :

    private List<String> transformSparseArrayToArrayList(SparseArray<String> sparseArray) {
        List<String> list = new ArrayList<String>(sparseArray.size());
        for (int i = 0; i < sparseArray.size(); i++) {
            list.add(sparseArray.valueAt(i));
        }
        return list;
    }

This transforms the SparseArray returned by our method into a List. This is the exact same method used internally by the getNfcMessages() method.


Note : It is not possible to use a for-each to loop through a SparseArray, keep this in mind, you'll have to use a normal for-loop to achieve this.


Should you encounter a discrepancy between the standards from the NFC Forum and our implementation, be sure to inform us!

Clone this wiki locally