write NFC Template - Android Network

Android examples for Network:NFC

Description

write NFC Template

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;

import android.nfc.Tag;

import android.nfc.tech.Ndef;

public class Main {
    public static void writeTemplate(byte[] template, Tag tag)
            throws IOException, FormatException {

        NdefRecord[] records = { createTemplateRecord(template) };
        NdefMessage message = new NdefMessage(records);
        Ndef ndef = Ndef.get(tag);/*from  w w w.  j ava2 s . c  o  m*/
        ndef.connect();
        ndef.writeNdefMessage(message);
        ndef.close();
    }

    private static NdefRecord createTemplateRecord(byte[] template)
            throws UnsupportedEncodingException {

        NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_TEXT, new byte[0], template);
        return recordNFC;
    }
}

Related Tutorials