Get url and the prefix as arguments and convert them to the NdefRecord of TNF type WELL_KNOWN and RTD of URI. - Android Network

Android examples for Network:NFC Record

Description

Get url and the prefix as arguments and convert them to the NdefRecord of TNF type WELL_KNOWN and RTD of URI.

Demo Code


import android.content.Context;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Parcelable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.logging.Logger;

public class Main{
    /**/*  w  w w . j a va  2s.co  m*/
     * Get url and the prefix as arguments and convert them to the NdefRecord of TNF type WELL_KNOWN and RTD of URI.
     *
     * @param url    url of the uri
     * @param prefix prefix of uri which can be http:// or https:// ...
     * @return NdefRecord instance.
     */
    public static NdefRecord getUrlRecord(String url, RTDPrefix prefix) {

        byte[] uriField = url.getBytes(Charset.forName("US-ASCII"));
        byte[] payload = new byte[uriField.length + 1];
        payload[0] = prefix.getHex();
        System.arraycopy(uriField, 0, payload, 1, uriField.length);
        NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_URI, new byte[0], payload);
        return uriRecord;
    }
}

Related Tutorials