Example usage for android.nfc NdefRecord TNF_MIME_MEDIA

List of usage examples for android.nfc NdefRecord TNF_MIME_MEDIA

Introduction

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

Prototype

short TNF_MIME_MEDIA

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

Click Source Link

Document

Indicates the type field contains a media-type BNF construct, defined by RFC 2046.

Use this with MIME type names such as "image/jpeg" , or using the helper #createMime .

Usage

From source file:de.gadc.moneybeam.MoneyBeamActivity.java

/**
 * Creates a custom MIME type encapsulated in an NDEF record.
 * /* w  ww . ja va2 s. co  m*/
 * @return the {@link NdefRecord} for usage in the {@link NdefMessage}
 */
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    final NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

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

/**
 * @param records Ndefrecord[] array/*from w  w  w.  java2  s . co m*/
 * @return an ArrayList of the NdefRecords within records. Will only put MIME formatted
 * NdefRecords in the ArrayList. Returns null if the array is null.
 */
private ArrayList<NdefMimeRecord> extractMimeRecords(NdefRecord[] records) {
    if (records == null) {
        return null; // TODO Give more specific feedback?
    }
    ArrayList<NdefMimeRecord> recordlist = new ArrayList<NdefMimeRecord>();
    for (int i = 0; i < records.length; i++) {
        // check if NdefRecord is MIME formatted
        if (records[i].getTnf() == NdefRecord.TNF_MIME_MEDIA) {
            recordlist.add(new NdefMimeRecord(records[i].getPayload(), records[i].getType()));
        }
    }
    return recordlist;
}

From source file:net.eledge.android.europeana.gui.activity.SearchActivity.java

@TargetApi(14)
private void createNdefPushMessageCallback() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
            @Override/*w ww . j  ava  2s  .co m*/
            public NdefMessage createNdefMessage(NfcEvent event) {
                return new NdefMessage(new NdefRecord[] {
                        new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                "application/vnd.net.eledge.android.europeana.search".getBytes(), new byte[0],
                                searchController.getPortalUrl().getBytes()),
                        NdefRecord.createApplicationRecord(getPackageName()) });
            }
        }, this);
    }
}

From source file:net.eledge.android.europeana.gui.activity.RecordActivity.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void createNdefPushMessageCallback() {
    NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        mNfcAdapter.setNdefPushMessageCallback(new CreateNdefMessageCallback() {
            @Override/*  w w w .ja v a  2  s.  c om*/
            public NdefMessage createNdefMessage(NfcEvent event) {
                return new NdefMessage(new NdefRecord[] {
                        new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                "application/vnd.net.eledge.android.europeana.record".getBytes(), new byte[0],
                                recordController.getPortalUrl().getBytes()),
                        NdefRecord.createApplicationRecord(getPackageName()) });
            }
        }, this);
    }
}

From source file:mai.whack.StickyNotesActivity.java

private NdefMessage getNoteAsNdef() {
    byte[] textBytes = (new String("")).getBytes();
    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(), new byte[] {},
            textBytes);/*ww  w . j  a  va2  s . c  o  m*/
    return new NdefMessage(new NdefRecord[] { textRecord });
}

From source file:com.evandroid.musica.fragment.LyricsViewFragment.java

@TargetApi(16)
private void beamLyrics(final Lyrics lyrics, Activity activity) {
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
    if (nfcAdapter != null && nfcAdapter.isEnabled()) {
        if (lyrics.getText() != null) {
            nfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
                @Override//from   www  .j a v  a2s  . c o  m
                public NdefMessage createNdefMessage(NfcEvent event) {
                    try {
                        byte[] payload = lyrics.toBytes(); // whatever data you want to send
                        NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                "application/lyrics".getBytes(), new byte[0], payload);
                        return new NdefMessage(new NdefRecord[] { record, // your data
                                NdefRecord.createApplicationRecord("com.geecko.QuickLyric"), // the "application record"
                        });
                    } catch (IOException e) {
                        return null;
                    }
                }
            }, activity);
        }
    }
}

From source file:us.rader.wyfy.MainActivity.java

/**
 * Initialize from a legacy <code>NdefMessage</code>
 * /*from  w  ww  . j  a  v  a  2  s  . co m*/
 * Provide backward compatibility for tags written with older versions of
 * this app
 * 
 * @param ndefMessage
 *            legacy <code>NdefMessage</code>
 * 
 * @return <code>true</code> if and only if an asynchronouse attempt to
 *         connect was launched
 */
private boolean parseLegacyMessage(NdefMessage ndefMessage) {

    try {

        NdefRecord[] records = ndefMessage.getRecords();

        if (records.length > 0) {

            NdefRecord record = records[0];

            if (record.getTnf() != NdefRecord.TNF_MIME_MEDIA) {

                return false;

            }

            String type = new String(record.getType(), "US-ASCII"); //$NON-NLS-1$

            if ("application/x-wyfy".equals(type)) { //$NON-NLS-1$

                String payload = new String(record.getPayload(), "US-ASCII"); //$NON-NLS-1$
                return parseUri(payload);

            }
        }

    } catch (Exception e) {

        Log.e(getClass().getName(), "initializeNdefMessage", e); //$NON-NLS-1$

    }

    return false;

}

From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java

@SuppressLint("NewApi")
@Override/*ww  w .j av a 2s . c o  m*/
public NdefMessage createNdefMessage(NfcEvent arg0) {
    String data;
    if (mEditText != null) {
        data = mEditText.getText().toString();
    } else {
        data = content;
    }

    String mimeType = "application/com.hipipal.qpy.nfc";

    byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
    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 message = new NdefMessage(new NdefRecord[] { record });

    return message;
}

From source file:at.flack.MainActivity.java

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

From source file:ch.bfh.instacircle.NetworkActiveActivity.java

/**
 * Creates a custom MIME type encapsulated in an NDEF record
 * //from  w  w w .  j a  v  a  2s. c o m
 * @param mimeType
 *            The string with the mime type name
 */
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
}