Back to project page sodf.
The source code is released under:
Copyright (c) 2013 Lorenz Lehmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...
If you think the Android project sodf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package lal.sodf.framework.nfc; //w w w .j av a 2 s .c o m import java.io.IOException; import java.nio.charset.Charset; import lal.sodf.framework.exceptions.MalformedTypeException; import lal.sodf.framework.exceptions.TagEmptyException; import lal.sodf.framework.exceptions.UnformattedTagException; import android.nfc.FormatException; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.Tag; import android.nfc.TagLostException; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; /** * A class to read or write data from NFC tags with the help of the Android libraries * @author Lorenz Lehmann * */ public class NfcHandler { /** The type parameter which is used if the app does not provide its package name */ public static final String UNKNOWN_APP = "unknown"; /** * Write data on a NFC tag * @param tag The tag where the data is written on * @param app A string to identify the app. This should be the package name of the application under which it is listend in the Google Play store * @param payload The payload, which will be written on the tag * @param compression The compression algorithm or null if no compression was used * @throws IOException If there is an I/O failure, or the operation is canceled when reading from the tag * @throws FormatException If the NDEF Message on the tag is malformed * @throws TagLostException If the device is moved away from the tag during the read */ public static void writeData(Tag tag, String app, byte[] payload, String compression) throws TagLostException, IOException, FormatException{ Ndef ndef = null; NdefFormatable formattable = null; try { //build the type description String type = "sodf/"; if (compression != null) type = type + compression + "/"; if (app != null && app.length() > 0) type = type + app; else type = type + UNKNOWN_APP; //build the NDEF record, which can be written on the tag NdefRecord record = new NdefRecord( NdefRecord.TNF_MIME_MEDIA , type.getBytes(Charset.forName("UTF-8")), new byte[0], payload); //put the NDEF record in a message NdefRecord[] records = { record }; NdefMessage message = new NdefMessage(records); // Get an instance of Ndef for the tag. ndef = Ndef.get(tag); //if the tag was not formatted yet it will be null if (ndef == null) { //try to format the tag formattable = NdefFormatable.get(tag); formattable.connect(); formattable.format(message); formattable.close(); } else {//if the tag is formatted just write the data // Enable I/O ndef.connect(); // Write the message ndef.writeNdefMessage(message); // Close the connection ndef.close(); } } finally {//always close used connections if (ndef != null) ndef.close(); if (formattable != null) formattable.close(); } } /** * Read data from an NFC tag * @param tag The tag where the data is read from * @return The data, which was read from the tag * @throws IOException If there is an I/O failure, or the operation is canceled when reading from the tag * @throws FormatException If the NDEF Message on the tag is malformed * @throws TagLostException If the device is moved away from the tag during the read * @throws MalformedTypeException If the type of the NDEF message is not in the format sodf/ * or sodf/ * / * * @throws UnformattedTagException If the tag is not formatted * @throws TagEmptyException If the tag is empty and nod NDEF messages have been written on it */ public static NfcContentWrapper readData(Tag tag) throws TagLostException, IOException, FormatException, MalformedTypeException, UnformattedTagException, TagEmptyException{ Ndef ndef = null; try { //get the NDEF record from the tag ndef = Ndef.get(tag); if (ndef == null) throw new UnformattedTagException(); ndef.connect(); //get the message from the tag NdefMessage msg = ndef.getNdefMessage(); if (msg == null) throw new TagEmptyException("Tag contains no NDEF message. Was anything written on it?"); //close the connection again ndef.close(); //parse the NDEF records NdefRecord[] records = msg.getRecords(); if (records.length <= 0) throw new TagEmptyException("Tag does not contain any data."); NdefRecord record = records[0];//there should be only one record since we only write one //parse the content of the record NfcContentWrapper ncw = new NfcContentWrapper(); //get the type String type = new String(record.getType(), Charset.forName("UTF-8")); String[] typeParts = type.split("/"); if (typeParts.length == 2){//no compression used ncw.app = typeParts[1]; } else if (typeParts.length == 3){//compression used ncw.compression = typeParts[1]; ncw.app = typeParts[2]; } else {//the tag is not formatted properly throw new MalformedTypeException("Malformed type: "+type); } //get the payload ncw.payload = record.getPayload(); return ncw; } finally {//always close the connection, even if an exception occurs if (ndef != null) ndef.close(); } } }