get NFC Ndef Messages - Android Network

Android examples for Network:NFC Message

Description

get NFC Ndef Messages

Demo Code


//package com.java2s;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;

import android.os.Parcelable;
import android.util.Log;

public class Main {
    private static final String TAG = "NFCUtil";

    public static NdefMessage[] getNdefMessages(Intent intent) {

        // Parse the intent
        NdefMessage[] msgs = null;/*w w  w . j  a  va  2  s  . c  om*/
        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
            Parcelable[] rawMsgs = intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            if (rawMsgs != null) {
                msgs = new NdefMessage[rawMsgs.length];
                for (int i = 0; i < rawMsgs.length; i++) {
                    msgs[i] = (NdefMessage) rawMsgs[i];
                }
            } else {
                // Unknown tag type
                byte[] empty = new byte[] {};
                NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN,
                        empty, empty, empty);
                NdefMessage msg = new NdefMessage(
                        new NdefRecord[] { record });
                msgs = new NdefMessage[] { msg };
            }
        } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
            Log.d(TAG, "ACTION_TECH_DISCOVERED intent.");
        } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
            Log.d(TAG, "Unknown intent. ACTION_TAG_DISCOVERED Discovered.");
        }
        return msgs;
    }
}

Related Tutorials