Get Text Pay Load from NFC Message - Android Network

Android examples for Network:NFC Message

Description

Get Text Pay Load from NFC Message

Demo Code


//package com.java2s;
import java.nio.charset.Charset;
import java.util.Locale;

public class Main {
    /**/* w  ww  .  j av  a 2 s . c  o  m*/
     * payload[0] contains the "Status Byte Encodings" field, per the NFC Forum
     * "Text Record Type Definition" section 3.2.1.
     * 
     * bit7 is the Text Encoding Field.
     * 
     * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1): The text
     * is encoded in UTF16
     * 
     * Bit_6 is reserved for future use and must be set to zero.
     * 
     * Bits 5 to 0 are the length of the IANA language code.
     */
    public static byte[] getTextPayLoad(String message) {

        Locale locale = Locale.US;
        final byte[] langBytes = locale.getLanguage().getBytes(
                Charset.forName("UTF-8"));

        final byte[] textBytes = message.getBytes(Charset.forName("UTF-8"));
        final int utfBit = 0;
        final char status = (char) (utfBit + langBytes.length);

        int totalLength = 1 + langBytes.length + textBytes.length;
        byte data[] = new byte[totalLength];

        copySmallArraysToBigArray(new byte[][] {
                new byte[] { (byte) status }, langBytes, textBytes }, data);

        return data;
    }

    public static void copySmallArraysToBigArray(
            final byte[][] smallArrays, final byte[] bigArray) {
        int currentOffset = 0;
        for (final byte[] currentArray : smallArrays) {
            System.arraycopy(currentArray, 0, bigArray, currentOffset,
                    currentArray.length);
            currentOffset += currentArray.length;
        }
    }
}

Related Tutorials