Example usage for android.nfc.tech Ndef makeReadOnly

List of usage examples for android.nfc.tech Ndef makeReadOnly

Introduction

In this page you can find the example usage for android.nfc.tech Ndef makeReadOnly.

Prototype

public boolean makeReadOnly() throws IOException 

Source Link

Document

Make a tag 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);//from ww  w .j  a va2 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);
    }

}

From source file:net.networksaremadeofstring.rhybudd.WriteNFCActivity.java

public void WriteTag(final Tag receivedTag) {
    tagHandler.sendEmptyMessage(TAG_IO_IN_PROGRESS);

    (new Thread() {

        public void run() {
            //This could go all kinds of weird
            Ndef thisNdef = null;

            try {
                thisNdef = Ndef.get(receivedTag);
            } catch (Exception e) {
                BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e);
                e.printStackTrace();/* w  w  w .java2  s  .  com*/
            }

            if (null == thisNdef) {
                NdefFormatable formatter = null;
                try {
                    formatter = NdefFormatable.get(receivedTag);
                    formatter.connect();
                    formatter.format(
                            new NdefMessage(new NdefRecord[] { NdefRecord.createApplicationRecord("io.d0") }));
                    formatter.close();
                    thisNdef = Ndef.get(receivedTag);
                } catch (Exception d) {
                    BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", d);
                    d.printStackTrace();
                    tagHandler.sendEmptyMessage(FORMATEXCEPTION);
                }
            }

            try {
                if (null == thisNdef) {
                    throw new FormatException("No NDEF Tag returned from get");
                } else {
                    thisNdef.connect();
                }

                if (thisNdef.isWritable()) {
                    //Is this a 203 or larger?
                    if (thisNdef.getMaxSize() < aaRecord.toByteArray().length + idRecord.toByteArray().length) {
                        /*Log.i("WriteTag","This tag was too big. tried to write " + (aaRecord.toByteArray().length + idRecord.toByteArray().length) + " to " + thisNdef.getMaxSize());
                        idRecord = NdefRecord.createMime("text/plain", Integer.toString(tagMetaData.getInt("i")).getBytes(Charset.forName("US-ASCII")));
                        Log.i("WriteTag Size Check", "Writing " + (idRecord.toByteArray().length + aaRecord.toByteArray().length) + " to " + thisNdef.getMaxSize());*/
                        tagHandler.sendEmptyMessage(SIZE_ERROR);
                    } else {
                        //Log.i("WriteTag Size Check", "Writing " + (aaRecord.toByteArray().length + idRecord.toByteArray().length) + " to " + thisNdef.getMaxSize());
                        NdefMessage tagMsg = new NdefMessage(new NdefRecord[] { idRecord, aaRecord });
                        //Log.i("WriteTag Size Check", "Wrote " + tagMsg.getByteArrayLength());
                        thisNdef.writeNdefMessage(tagMsg);
                        thisNdef.makeReadOnly();
                        thisNdef.close();
                        tagHandler.sendEmptyMessage(WRITE_SUCCESS);
                    }
                } else {
                    tagHandler.sendEmptyMessage(READONLY);
                }
            } catch (IOException e) {
                BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e);
                tagHandler.sendEmptyMessage(IOEXCEPTION);
            } catch (FormatException e) {
                BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e);
                e.printStackTrace();
                tagHandler.sendEmptyMessage(FORMATEXCEPTION);
            }
        }
    }).start();
}