Example usage for android.nfc.tech Ndef canMakeReadOnly

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

Introduction

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

Prototype

public boolean canMakeReadOnly() 

Source Link

Document

Indicates whether a tag can be made read-only with #makeReadOnly() .

Usage

From source file:Main.java

static JSONObject ndefToJSON(Ndef ndef) {
    JSONObject json = new JSONObject();

    if (ndef != null) {
        try {/*  w w w .  j av a2  s .  c  om*/

            Tag tag = ndef.getTag();
            // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored
            if (tag != null) {
                json.put("id", byteArrayToJSON(tag.getId()));
                json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
            }

            json.put("type", translateType(ndef.getType()));
            json.put("maxSize", ndef.getMaxSize());
            json.put("isWritable", ndef.isWritable());
            json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
            // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
            // mTag.getTagService(); of the Ndef object sometimes returns null
            // see http://issues.mroland.at/index.php?do=details&task_id=47
            try {
                json.put("canMakeReadOnly", ndef.canMakeReadOnly());
            } catch (NullPointerException e) {
                json.put("canMakeReadOnly", JSONObject.NULL);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
        }
    }
    return json;
}

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  a  2  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);
    }

}