get NFC Ndef Record - Android Network

Android examples for Network:NFC Record

Description

get NFC Ndef Record

Demo Code


//package com.java2s;

import java.nio.charset.Charset;

import java.util.Locale;

import android.nfc.NdefRecord;

public class Main {
    private final static String textEncoding = "UTF-8";

    private static NdefRecord getNdefRecord(String msg) {

        Locale locale = Locale.ENGLISH;

        byte[] langBytes = locale.getLanguage().getBytes(
                Charset.forName("US-ASCII"));

        Charset utfEncoding = Charset.forName(textEncoding);
        byte[] textBytes = msg.getBytes(utfEncoding);

        int utfBit = 0;
        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);//w ww .  j  av  a 2  s  .  co m

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

    }
}

Related Tutorials