Read NFC tag with URI encoded content - Android Network

Android examples for Network:NFC Tag

Description

Read NFC tag with URI encoded content

Demo Code


import android.annotation.SuppressLint;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import static fr.utc.nf28.moka.util.LogUtils.LOGE;
import static fr.utc.nf28.moka.util.LogUtils.LOGI;

public class Main{
    /**/*from w  ww. j  a  v  a 2s. c  om*/
     * Read tag with URI encoded content
     *
     * @param tag tag detected
     * @return uri store int the payload[0]
     * @throws UnsupportedEncodingException
     */
    @SuppressLint("NewApi")
    public static String readTag(Tag tag) {
        final Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            final NdefMessage message = ndef.getCachedNdefMessage();
            if (message != null) {
                return new String(message.getRecords()[0].getPayload());
            }
            return null;
        }
        return null;
    }
}

Related Tutorials