Asynchronously read a tag from an intent. - Android Network

Android examples for Network:NFC Tag

Description

Asynchronously read a tag from an intent.

Demo Code


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

public class Main{
    public static final String TAG = "NFC";
    public static final String NFC_EXTRA_TAG = "NFCTag";
    /**//from w  w w .j av a  2 s  .c o  m
     * Asynchronously read a tag from an intent.
     * @param tag
     */
    public static void getNDEFMessage(Intent intent,
            final AsynchHandler<String> handler) {
        class NdefReaderTask extends AsyncTask<Tag, Void, String> {
            @Override
            protected String doInBackground(Tag... params) {
                Tag tag = params[0];
                Ndef ndef = Ndef.get(tag);
                if (ndef == null) {
                    // NDEF is not supported by this Tag.
                    return null;
                }
                NdefMessage ndefMessage = ndef.getCachedNdefMessage();
                NdefRecord[] records = ndefMessage.getRecords();
                for (NdefRecord ndefRecord : records) {
                    if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN
                            && Arrays.equals(ndefRecord.getType(),
                                    NdefRecord.RTD_TEXT)) {
                        try {
                            return readText(ndefRecord);
                        } catch (UnsupportedEncodingException e) {
                            Log.e(TAG, "Unsupported Encoding", e);
                        }
                    }
                }
                return null;
            }

            private String readText(NdefRecord record)
                    throws UnsupportedEncodingException {
                /*
                 * See NFC forum specification for "Text Record Type Definition" at 3.2.1
                 *
                 * http://www.nfc-forum.org/specs/
                 *
                 * bit_7 defines encoding
                 * bit_6 reserved for future use, must be 0
                 * bit_5..0 length of IANA language code
                 */
                byte[] payload = record.getPayload();
                // Get the Text Encoding
                String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8"
                        : "UTF-16";
                // Get the Language Code
                int languageCodeLength = payload[0] & 0063;
                // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
                // e.g. "en"
                // Get the Text
                return new String(payload, languageCodeLength + 1,
                        payload.length - languageCodeLength - 1,
                        textEncoding);
            }

            @Override
            protected void onPostExecute(String result) {
                if (result != null) {
                    handler.resultReady(result);
                }
            }
        }
        ;

        //execute task

        System.out.println("INTENT decoding");

        if (intent.getExtras() == null) {
            System.out.println("-> extra : null");
            return;
        }
        Tag tag = intent.getExtras().getParcelable(NFC_EXTRA_TAG);
        NdefReaderTask task = new NdefReaderTask();
        task.execute(tag);
    }
}

Related Tutorials