Example usage for android.nfc NdefRecord TNF_WELL_KNOWN

List of usage examples for android.nfc NdefRecord TNF_WELL_KNOWN

Introduction

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

Prototype

short TNF_WELL_KNOWN

To view the source code for android.nfc NdefRecord TNF_WELL_KNOWN.

Click Source Link

Document

Indicates the type field contains a well-known RTD type name.

Use this tnf with RTD types such as #RTD_TEXT , #RTD_URI .

Usage

From source file:Main.java

private static NdefRecord createText(String paramString1, String paramString2) {
    int i = 0;/*from  w  w w  . j  a  v  a 2  s .c o  m*/
    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!!");
    }/*from  w ww . ja  v  a  2  s  .  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

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 containing a URI, using the URI format definition.
 * @param uri the URI to be written/*from   w w w .ja v a  2 s. c  om*/
 * @throws IOException
 */
public void addUriRecord(String uri) throws IOException {
    if (!this.hasChecksum) {
        recordlist.add(new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0],
                ArrayUtils.addAll(new byte[] { (byte) 0x00 }, uri.getBytes(Charset.forName("UTF-8")))));
    } else {
        throw new IOException("Cannot add record - last record is a checksum.");
    }
}

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

/**
 * Reads URI records from NDEF records//  w  w  w .  j a  v a 2s .  c  o m
 * @param tag
 * @return an ArrayList of URIs, represented as Strings.
 */
public ArrayList<String> getUriRecords(Tag tag) {
    readRecords(tag);
    if (this.records != null) {
        ArrayList<String> uriStrings = new ArrayList<String>();
        for (int i = 0; i < this.records.length; i++) {
            if (this.records[i].getTnf() == NdefRecord.TNF_WELL_KNOWN
                    && Arrays.equals(this.records[i].getType(), NdefRecord.RTD_URI)) {
                uriStrings.add(EncodingUtils.getString(ArrayUtils.remove(records[i].getPayload(), 0), "UTF-8"));
            }
        }
        return uriStrings;
    } else {
        return null;
    }
}

From source file:jp.tomorrowkey.android.felicalitewriter.ndef.UriNdefBuilder.java

/**
 * NDEF????/*from  w w  w.  j  av  a2  s .  com*/
 * 
 * @return
 */
public NdefMessage build() {
    try {
        int index = getProtocolIndex(mUriString);
        String protocol = sProtocolList.get(index);

        String uriBody = mUriString.replace(protocol, "");
        byte[] uriBodyBytes = uriBody.getBytes("UTF-8");

        ByteArrayBuffer buffer = new ByteArrayBuffer(1 + uriBody.length());
        buffer.append((byte) index);
        buffer.append(uriBodyBytes, 0, uriBodyBytes.length);

        byte[] payload = buffer.toByteArray();
        NdefMessage message = new NdefMessage(new NdefRecord[] {
                new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload) });

        return message;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);/* www .j a va2 s .  co m*/

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}

From source file:se.anyro.nfc_reader.TagViewer.java

private 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);

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

From source file:com.sftoolworks.nfcoptions.SelectActivity.java

protected void writeSelection(Intent intent) {
    ListView list = (ListView) findViewById(R.id.listView1);
    if (list.getCount() == 0)
        return;/*  w ww  . j a va  2s .  c om*/

    try {

        Object[] results = ((OptionListAdapter) list.getAdapter()).getCheckedValues();
        JSONObject obj = new JSONObject();

        JSONArray array = new JSONArray();

        if (null != results) {
            for (Object result : results)
                array.put(result.toString());
        }

        obj.put("selection", array);
        obj.put("key", selectKey);

        SharedPreferences sharedPref = getSharedPreferences(getString(R.string.preference_file_key),
                Context.MODE_PRIVATE);

        // android studio (0.5.1) decorates this line as an error (some
        // of the time, anyway) but it's not an error.

        String identifier = sharedPref.getString(getString(R.string.user_id_key), "");
        if (identifier.length() > 0)
            obj.put("user", identifier);

        String json = obj.toString(0);

        String outbound = "\u0002en";
        outbound += json;

        NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, new byte[] { 'T' }, null,
                outbound.getBytes());

        NdefMessage message = new NdefMessage(new NdefRecord[] { textRecord });
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Ndef ndef = Ndef.get(tag);
        ndef.connect();

        ndef.writeNdefMessage(message);
        ndef.close();

        Toast.makeText(this, R.string.write_ok, Toast.LENGTH_LONG).show();
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                finish();
            }
        }, 1500);

    } catch (Exception e) {

        Log.d(TAG, e.toString());
        String err = getString(R.string.tag_write_err) + "\n" + e.getMessage();
        Toast.makeText(this, err, Toast.LENGTH_LONG).show();
    }
}