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:de.stadtrallye.rallyesoft.net.NfcCallback.java

@Override
@TargetApi(14)//  www.  j  a  va  2s.  co m
public NdefMessage createNdefMessage(NfcEvent event) {
    ObjectMapper mapper = Serialization.getJsonInstance();
    String text = null;
    try {
        text = mapper.writeValueAsString(server.exportLogin());
    } catch (JsonProcessingException e) {
        Log.e(THIS, "Could not export Login", e);
    }
    NdefMessage msg = new NdefMessage(new NdefRecord[] {
            new NdefRecord(NdefRecord.TNF_MIME_MEDIA, Std.APP_MIME.getBytes(Charset.forName("US-ASCII")),
                    new byte[0], text.getBytes(Charset.forName("US_ASCII"))),
            NdefRecord.createApplicationRecord("de.stadtrallye.rallyesoft") });
    return msg;
}

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//ww  w  .  j  a  va  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:root.gast.playground.speech.activation.util.TagWriterExecutionActivity.java

/**
 * create a new NDEF record and containing NDEF message using the app's
 * custom MIME type but that is otherwise empty.
 * /*from  w  w  w.  j  a  va  2s.  c om*/
 * @return
 */
private NdefMessage createMimeTypeMessage() {
    String mimeType = SPEECH_ACTIVATION_TYPE;
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
    byte[] id = new byte[0];
    byte[] data = new byte[0];
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, data);
    NdefMessage m = new NdefMessage(new NdefRecord[] { record });
    return m;
}

From source file:com.commonsware.android.webbeam.WebBeamActivity.java

@Override
public NdefMessage createNdefMessage(NfcEvent arg0) {
    NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
            MIME_TYPE.getBytes(Charset.forName("US-ASCII")), new byte[0],
            beamFragment.getUrl().getBytes(Charset.forName("US-ASCII")));
    NdefMessage msg = new NdefMessage(new NdefRecord[] { uriRecord,
            NdefRecord.createApplicationRecord("com.commonsware.android.webbeam") });

    return (msg);
}

From source file:org.thinkfree.axihome.NFC.TagViewer.java

void resolveIntent(Intent intent) {

    Log.e(TAG, "Tag detected");
    String action = intent.getAction();

    Log.e(TAG, action);/*  w w  w. j  ava 2s.  c  om*/

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;

        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
            Log.e(TAG, "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 };
        }
        // Setup the views
        processMessage(msgs);
    } else {

        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}

From source file:root.gast.playground.speech.activation.util.TagWriterExecutionActivity.java

private NdefMessage createNdefWithJson() {

    // create a JSON object out of the values:
    JSONObject activationSpecs = new JSONObject();
    try {//from ww  w.ja v a  2  s . c  o  m
        activationSpecs.put("target", "placeholder");
    } catch (JSONException e) {
        Log.d(TAG, "Could not create JSON");
    }

    // create a new NDEF record and containing NDEF message using the app's
    // custom MIME type:
    String mimeType = SPEECH_ACTIVATION_TYPE;
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
    String data = activationSpecs.toString();
    byte[] dataBytes = data.getBytes(Charset.forName("UTF-8"));
    byte[] id = new byte[0];
    NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, dataBytes);
    NdefMessage m = new NdefMessage(new NdefRecord[] { record });

    // return the NDEF message
    return m;
}

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

/**
 * NDEF????/*from   w ww. jav  a  2s  . c  o m*/
 * 
 * @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:de.berlin.magun.nfcmime.core.NdefMessageBuilder.java

/**
 * Creates a CRC32 checksum out of all previously added NDEF records and adds it as last record entry. 
 * After that, it will not be possible to add additional records. The record payload will be a String 
 * consisting of the prefix 'crc32:' and a String representation of the checksum long, e.g. 
 * 'crc32:2868450884'. The record will be MIME formatted as 'text/plain'.
 *//*from  w ww.  j a v  a 2  s .c o m*/
public void addChecksum() {
    CrcGenerator generator = new CrcGenerator();
    NdefRecord[] records = new NdefRecord[recordlist.size()];
    for (int i = 0; i < recordlist.size(); i++) {
        records[i] = recordlist.get(i);
    }
    recordlist.add(new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(Charset.forName("US-ASCII")),
            new byte[0],
            ("crc32:" + String.valueOf(generator.getChecksum(records))).getBytes(Charset.forName("US-ASCII"))));
}

From source file:com.battlelancer.seriesguide.ui.OverviewActivity.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/* w  w  w .ja v  a2  s . c o m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_overview);
    setupActionBar();
    setupNavDrawer();

    mShowId = getIntent().getIntExtra(OverviewFragment.InitBundle.SHOW_TVDBID, -1);
    if (mShowId == -1) {
        finish();
        return;
    }

    setupViews(savedInstanceState);

    // Support beaming shows via Android Beam
    if (AndroidUtils.isICSOrHigher()) {
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter != null) {
            mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
                @Override
                public NdefMessage createNdefMessage(NfcEvent event) {
                    final Series show = DBUtils.getShow(OverviewActivity.this, mShowId);
                    // send id, also title and overview (both can be empty)
                    return new NdefMessage(new NdefRecord[] {
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    String.valueOf(mShowId).getBytes()),
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    show.getTitle().getBytes()),
                            createMimeRecord("application/com.battlelancer.seriesguide.beam",
                                    show.getOverview().getBytes()) });
                }

                /**
                 * Creates a custom MIME type encapsulated in an NDEF record
                 */
                public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
                    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
                    return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
                }
            }, this);
        }
    }

    updateShowDelayed(mShowId);
}

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