read NFC Ndef Card Content - Android Network

Android examples for Network:NFC Ndef

Description

read NFC Ndef Card Content

Demo Code


//package com.java2s;

import java.io.UnsupportedEncodingException;

import android.content.Intent;

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

import android.os.Parcelable;

public class Main {

    public static String[] readNdefCardContent(Intent intent) {

        Parcelable[] rawMessage = intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        byte[][] data = null; 
        String[] message = null;/*w w  w  . j  a va 2s.c o  m*/

        for (int i = 0; i < rawMessage.length; i++) {
            NdefMessage Ndefmsg = (NdefMessage) rawMessage[i];
            NdefRecord Ndefrecord[] = Ndefmsg.getRecords();
            data = new byte[Ndefrecord.length][];
            for (int j = 0; j < Ndefrecord.length; j++) {
                data[j] = Ndefrecord[j].getPayload();
            }
        }

        message = new String[data.length];
        for (int i = 0; i < data.length; i++) {
            if (data[i] != null) {
                try {
                    message[i] = new String(data[i], "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }

        return message;
    }
}

Related Tutorials