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:Main.java

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

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  w  w w. ja va2 s .  c  o  m*/
}

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().//from   w w w  .  j  a v a2 s  .c  o m
 * @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.");
    }
}

From source file:de.stadtrallye.rallyesoft.net.NfcCallback.java

@Override
@TargetApi(14)/*from w  ww.  ja  v a  2 s  .  c  o  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: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.
 * //  w ww.jav a2s . com
 * @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: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   w  w w.  jav a  2 s .  c om
        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: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  w w .j a va 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:de.berlin.magun.nfcmime.core.RfidDAO.java

/**
 * Checks all NDEF records against the CRC value contained in the last record.
 * @return true, if the checksum is correct.
 *///  w  ww .j  a v  a  2 s .  c om
public boolean verifyMimeRecords() {
    CrcGenerator generator = new CrcGenerator();
    NdefRecord checksumRecord = this.records[this.records.length - 1];
    NdefRecord[] payloadRecords = (NdefRecord[]) ArrayUtils.remove(this.records, this.records.length - 1);
    if (checksumRecord.getTnf() == NdefRecord.TNF_MIME_MEDIA
            && EncodingUtils.getString(checksumRecord.getPayload(), "UTF-8").startsWith("crc32:")) {
        String checksumStr = EncodingUtils.getString(checksumRecord.getPayload(), "UTF--8");
        long checksum = Long.parseLong(checksumStr.substring(checksumStr.indexOf("crc32:") + 6));
        return generator.checkHash(payloadRecords, checksum);
    } else {
        return false;
    }
}

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

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*www.  ja va2 s . com*/
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);
}