Example usage for android.nfc.tech NdefFormatable formatReadOnly

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

Introduction

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

Prototype

public void formatReadOnly(NdefMessage firstMessage) throws IOException, FormatException 

Source Link

Document

Formats a tag as NDEF, write a NdefMessage , and make read-only.

Usage

From source file:com.hybris.mobile.activity.NFCWriteActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    // default failed
    setResult(RESULT_CANCELED);/*  w w w  .j  a v  a2 s .  c o m*/

    if (NFCUtil.supportsNdef(tag)) {
        Ndef ndef = Ndef.get(tag);

        try {
            int maxSize = ndef.getMaxSize();
            int messageSize = this.msg.toByteArray().length;

            if (ndef.isWritable() && maxSize > messageSize) {
                ndef.connect();
                ndef.writeNdefMessage(this.msg);

                if (getResources().getBoolean(R.bool.nfc_read_only)) {
                    if (ndef.canMakeReadOnly()) {
                        boolean success = ndef.makeReadOnly();
                        if (!success)
                            Toast.makeText(this, "Unable to make tag readonly!", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(this, "Tag is now readonly!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "This tag cannot be made readonly!", Toast.LENGTH_LONG).show();
                    }
                }

                setResult(RESULT_OK);
                showDialogFragmentWithMessage(R.string.nfc_tag_written);
            } else {
                showDialogFragmentWithMessage(R.string.error_nfc_readonly_size);
            }
        } catch (IOException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext());

        } catch (FormatException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext());
        } finally {
            try {
                ndef.close();
            } catch (IOException e) {
            }
        }

    } else if (NFCUtil.supportsNdefFormatable(tag)) {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        try {
            ndefFormatable.connect();

            if (getResources().getBoolean(R.bool.nfc_read_only)) {
                ndefFormatable.formatReadOnly(this.msg);
            } else {
                ndefFormatable.format(this.msg);
            }

            setResult(RESULT_OK);
            showDialogFragmentWithMessage(R.string.nfc_tag_written);
        } catch (IOException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext());
        } catch (FormatException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext());
        } finally {
            try {
                ndefFormatable.close();
            } catch (IOException e) {
            }
        }

    } else {
        showDialogFragmentWithMessage(R.string.error_nfc_no_ndef);
    }

}