Android examples for Network:NFC Ndef
get NFC Ndef Well Known Text
//package com.java2s; import java.nio.charset.Charset; import java.util.Locale; import java.util.Random; import android.nfc.NdefMessage; import android.nfc.NdefRecord; public class Main { public static NdefMessage getNdefWellKnownText(String text) { NdefRecord textRecord = getTextRecord(text); return createNdefMessage(textRecord); }// ww w.j av a 2 s.co m private static NdefRecord getTextRecord(String message) { NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, getRandomIdBytes(), getTextPayLoad(message)); return record; } private static NdefMessage createNdefMessage(NdefRecord... records) { NdefMessage ndefMessage = new NdefMessage(records); return ndefMessage; } private static byte[] getRandomIdBytes() { Random rand = new Random(System.currentTimeMillis()); byte buf[] = new byte[4]; rand.nextBytes(buf); return buf; } /** * 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; } } }