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:com.example.multi_ndef.Frag_Write.java

/**
 * Format a tag and write our NDEF message
 *//*from   w ww  .  ja v  a 2s.  c  o m*/
//@SuppressLint("NewApi")
private boolean writeTag(Tag tag) {
    // record to launch Play Store if app is not installed

    NdefRecord appRecord = NdefRecord.createApplicationRecord("com.example.multi_ndef");

    try {
        ENGLISH = Locale.ENGLISH;
        encodeInUtf8 = true;
    }

    catch (Exception e) {

        Toast toast = Toast.makeText(getApplicationContext(), "Locale Error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();
    }

    try {

        textRecord = createTextRecord(getText(), ENGLISH, encodeInUtf8);

    }

    catch (Exception e)

    {

        Toast toast = Toast.makeText(getApplicationContext(), "Text Conversion error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();

    }

    try {

        uriRecord = NdefRecord.createUri(getUri());
    }

    catch (Exception e) {

        Toast toast = Toast.makeText(getApplicationContext(), "Uri Conversion error " + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();
    }

    byte[] mimeBytes = MimeType.AppName.getBytes(Charset.forName("US-ASCII"));

    byte[] mimeBytesBT = MimeType.AppNameBT.getBytes(Charset.forName("US-ASCII"));

    byte[] v_data = VCard();

    // Here data is written
    byte[] payload = data(); // payload in hex

    //Comments by neha - 3 Jul 2014
    byte[] payloadBT = btData();

    NdefRecord VcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "text/x-vcard".getBytes(), new byte[0],
            v_data);

    NdefRecord BTcardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytesBT, new byte[0], payloadBT);

    NdefRecord cardRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);

    NdefRecord SMSRecord = NdefRecord.createUri(SMSpayload);

    NdefRecord MailRecord = NdefRecord.createUri(Mailpayload);

    NdefRecord TeleRecord = NdefRecord.createUri(Telepayload);

    NdefRecord LocationRecord = NdefRecord.createUri(Locationpayload);

    NdefMessage message = new NdefMessage(new NdefRecord[] { cardRecord, textRecord, uriRecord, BTcardRecord,
            VcardRecord, SMSRecord, MailRecord, TeleRecord, LocationRecord, appRecord });
    //  ringProgressDialog.dismiss();

    try {
        // see if tag is already NDEF formatted
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();

            if (!ndef.isWritable()) {
                displayMessage("Read-only tag.");

                return false;
            }

            // work out how much space we need for the data
            int size = message.toByteArray().length;
            if (ndef.getMaxSize() < size) {
                displayMessage("Tag doesn't have enough free space.");

                return false;
            }

            ndef.writeNdefMessage(message);
            displayMessage("Multiple NDEF Records written to tag successfully.");

            return true;
        } else {
            // attempt to format tag
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(message);
                    displayMessage("Tag written successfully!\nClose this app and scan tag.");

                    return true;
                } catch (IOException e) {
                    displayMessage("Unable to format tag to NDEF.");

                    return false;
                }
            } else {
                displayMessage("Tag doesn't appear to support NDEF format.");

                return false;
            }
        }
    } catch (Exception e) {
        displayMessage("Failed to write tag");

    }

    return false;
}