write Tag to NdefMessage for NFC - Android Network

Android examples for Network:NFC Message

Description

write Tag to NdefMessage for NFC

Demo Code


//package com.java2s;
import android.nfc.FormatException;
import android.nfc.NdefMessage;

import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import java.io.IOException;

public class Main {
    public static void writeTag(NdefMessage message, Tag tag)
            throws Exception {
        Ndef ndef = Ndef.get(tag);//  www .  ja  v a 2  s  .  c  om
        if (ndef != null) {
            writeInNdef(message, ndef);
        } else {
            formatNdef(message, tag);
        }
    }

    private static void writeInNdef(NdefMessage message, Ndef ndef)
            throws Exception {
        ndef.connect();
        if (!ndef.isWritable()) {
            throw new IOException("Tag is read-only.");
        }
        if (ndef.getMaxSize() < message.toByteArray().length) {
            throw new IOException("Tag capacity is " + ndef.getMaxSize()
                    + " bytes, message is " + message.toByteArray().length
                    + " bytes.");
        }
        ndef.writeNdefMessage(message);
    }

    private static void formatNdef(NdefMessage message, Tag tag)
            throws Exception {
        NdefFormatable format = NdefFormatable.get(tag);
        if (format != null) {
            format.connect();
            format.format(message);
        } else {
            throw new FormatException("Tag doesn't support NDEF.");
        }
    }
}

Related Tutorials