Example usage for android.nfc NdefRecord NdefRecord

List of usage examples for android.nfc NdefRecord NdefRecord

Introduction

In this page you can find the example usage for android.nfc NdefRecord NdefRecord.

Prototype

public NdefRecord(short tnf, byte[] type, byte[] id, byte[] payload) 

Source Link

Document

Construct an NDEF Record from its component fields.

Recommend to use helpers such as {#createUri} or { #createExternal where possible, since they perform stricter validation that the record is correctly formatted as per NDEF specifications.

Usage

From source file:Main.java

public static NdefMessage createNdefVCard(String vcard) {
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, MIME_TYPE_VCARD.getBytes(), new byte[] {},
            vcard.getBytes());// ww  w  .j  a v a  2s  .c o m
    return new NdefMessage(new NdefRecord[] { record });
}

From source file:Main.java

public static NdefMessage[] getNdefMessages(Parcelable[] rawMsgs) {
    // Parse the intent
    NdefMessage[] msgs = null;// w  w  w  .j av  a2s. c om
    if (rawMsgs != null) {
        msgs = new NdefMessage[rawMsgs.length];
        for (int i = 0; i < rawMsgs.length; i++) {
            msgs[i] = (NdefMessage) rawMsgs[i];
        }
    } else {
        // Unknown tag type
        byte[] empty = new byte[] {};
        NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
        NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
        msgs = new NdefMessage[] { msg };
    }
    return msgs;
}

From source file:Main.java

static NdefRecord[] jsonToNdefRecords(String ndefMessageAsJSON) throws JSONException {
    JSONArray jsonRecords = new JSONArray(ndefMessageAsJSON);
    NdefRecord[] records = new NdefRecord[jsonRecords.length()];
    for (int i = 0; i < jsonRecords.length(); i++) {
        JSONObject record = jsonRecords.getJSONObject(i);
        byte tnf = (byte) record.getInt("tnf");
        byte[] type = jsonToByteArray(record.getJSONArray("type"));
        byte[] id = jsonToByteArray(record.getJSONArray("id"));
        byte[] payload = jsonToByteArray(record.getJSONArray("payload"));
        records[i] = new NdefRecord(tnf, type, id, payload);
    }/*  www .  j  av  a2 s  . co  m*/
    return records;
}

From source file:Main.java

private static NdefRecord createText(String paramString1, String paramString2) {
    int i = 0;//from  w w w.j  a  va2 s . c om
    try {
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
        byte[] arrayOfByte1 = paramString2.getBytes(Charset.forName("US-ASCII"));
        byte[] arrayOfByte2 = paramString1.getBytes(Charset.forName("UTF-8"));

        localDataOutputStream.writeByte((byte) (char) (i + arrayOfByte1.length));
        localDataOutputStream.write(arrayOfByte1);
        localDataOutputStream.write(arrayOfByte2);
        NdefRecord localNdefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0],
                localByteArrayOutputStream.toByteArray());
        return localNdefRecord;

    } catch (IOException localIOException) {
        throw new RuntimeException(localIOException);
    }
}

From source file:Main.java

public static NdefRecord createTextRecord(String text, Locale locale, boolean encodeInUtf8) {
    if (text == null) {
        throw new NullPointerException("text is MUST require!!");
    }/*  w w w .  j  av a 2s .c o  m*/
    if (locale == null) {
        throw new NullPointerException("locale is MUST require!!");
    }

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

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

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

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

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

From source file:Main.java

@Deprecated
public static NdefRecord createExternal(String domain, String type, byte[] data) {
    if (domain == null)
        throw new NullPointerException("domain is null");
    if (type == null)
        throw new NullPointerException("type is null");

    domain = domain.trim().toLowerCase(Locale.US);
    type = type.trim().toLowerCase(Locale.US);

    if (domain.length() == 0)
        throw new IllegalArgumentException("domain is empty");
    if (type.length() == 0)
        throw new IllegalArgumentException("type is empty");

    byte[] byteDomain = domain.getBytes(Charset.forName("UTF_8"));
    byte[] byteType = type.getBytes(Charset.forName("UTF_8"));
    byte[] b = new byte[byteDomain.length + 1 + byteType.length];
    System.arraycopy(byteDomain, 0, b, 0, byteDomain.length);
    b[byteDomain.length] = ':';
    System.arraycopy(byteType, 0, b, byteDomain.length + 1, byteType.length);

    return new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, b, new byte[0], data);
}

From source file:Main.java

private static NdefRecord createMime(String paramString1, String paramString2) {
    return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, paramString1.getBytes(), new byte[0],
            paramString2.getBytes());//from  ww  w.  j  a  va 2 s  .  c om
}

From source file:Main.java

public static NdefRecord createTextRecord(String payload, boolean encodeInUtf8) {
    byte[] langBytes = Locale.getDefault().getLanguage().getBytes(Charset.forName("US-ASCII"));
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
    byte[] textBytes = payload.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);
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    return record;
}

From source file:Main.java

private static NdefRecord createRecord(String message) throws UnsupportedEncodingException {
    //create the message in according with the standard
    String lang = "en";
    byte[] textBytes = message.getBytes();
    byte[] langBytes = lang.getBytes("US-ASCII");
    int langLength = langBytes.length;
    int textLength = textBytes.length;

    byte[] payload = new byte[1 + langLength + textLength];
    payload[0] = (byte) langLength;

    // copy langbytes and textbytes into payload
    System.arraycopy(langBytes, 0, payload, 1, langLength);
    System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);

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

From source file:de.berlin.magun.nfcmime.core.NdefMessageBuilder.java

/**
 * Stores an NDEF record from an InputStream. Will throw an IOException if the last NDEF record is
 * a checksum added with addChecksum()./*  w w w .  j  a va  2s.  com*/
 * @param is InputStream that holds the payload data of the NDEF record.
 * @param mimetype MIME type definition, e.g. "text/plain"
 * @throws IOException
 */
public void addMimeRecord(InputStream is, String mimetype) throws IOException {
    if (!this.hasChecksum) {
        recordlist.add(new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimetype.getBytes(Charset.forName("US-ASCII")),
                new byte[0], IOUtils.toByteArray(is)));
    } else {
        throw new IOException("Cannot add record - last record is a checksum.");
    }
}