Example usage for android.nfc.tech NdefFormatable format

List of usage examples for android.nfc.tech NdefFormatable format

Introduction

In this page you can find the example usage for android.nfc.tech NdefFormatable format.

Prototype

public void format(NdefMessage firstMessage) throws IOException, FormatException 

Source Link

Document

Format a tag as NDEF, and write a NdefMessage .

Usage

From source file:com.example.multi_ndef.Frag_Write.java

/**
 * Format a tag and write our NDEF message
 *//* www.j  av a  2s .co  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;
}

From source file:com.mario22gmail.license.nfc_project.NavigationDrawerActivity.java

private void formatTag(Tag tag, NdefMessage ndefMessage) {
    try {// w w  w .jav  a 2  s . c  o m
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        if (ndefFormatable == null) {
            Toast.makeText(this, "Tag is not ndef formatable !!!", Toast.LENGTH_SHORT).show();
            return;
        }

        ndefFormatable.connect();
        ndefFormatable.format(ndefMessage);
        Log.i(nfcDebugTag, "Tag formatat din format method");
        ndefFormatable.close();
    } catch (Exception e) {
        Log.e(nfcDebugTag, e.getMessage());
    }
}