Create new NFC Text Record - Android Network

Android examples for Network:NFC Record

Description

Create new NFC Text Record

Demo Code


//package com.java2s;

import java.nio.charset.Charset;

import java.util.Locale;

import android.nfc.NdefRecord;

public class Main {

    public static NdefRecord newTextRecord(String text, Locale locale,
            boolean encodeInUtf8) {
        byte[] langBytes = locale.getLanguage().getBytes(
                Charset.forName("US-ASCII"));

        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8")
                : Charset.forName("UTF-16");
        byte[] textBytes = text.getBytes(utfEncoding);

        int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        char status = (char) (utfBit + langBytes.length);

        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte) status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length,
                textBytes.length);//from  w ww.j  av  a  2  s .c  o m

        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_TEXT, new byte[0], data);
    }
}

Related Tutorials