Example usage for android.nfc NdefMessage getByteArrayLength

List of usage examples for android.nfc NdefMessage getByteArrayLength

Introduction

In this page you can find the example usage for android.nfc NdefMessage getByteArrayLength.

Prototype

public int getByteArrayLength() 

Source Link

Document

Return the length of this NDEF Message if it is written to a byte array with #toByteArray .

An NDEF Message can be formatted to bytes in different ways depending on chunking, SR, and ID flags, so the length returned by this method may not be equal to the length of the original byte array used to construct this NDEF Message.

Usage

From source file:st.alr.homA.ActivityQuickpublishNfc.java

private boolean write(String text, Tag tag) {
    boolean success = true;

    NdefRecord aar = NdefRecord.createApplicationRecord("st.alr.homA");
    NdefRecord[] records = { createRecord(text), aar };
    NdefMessage ndefMsg = new NdefMessage(records);

    try {/*from   w  ww. j  av a 2 s .  co m*/
        Ndef ndef = Ndef.get(tag);
        if (ndef != null) {
            ndef.connect();

            if (!ndef.isWritable()) {
                publishProgress(getResources().getString(R.string.nfcWriteDialogTagReadOnly), false);
                success = false;
            }
            if (ndef.getMaxSize() < ndefMsg.getByteArrayLength()) {
                publishProgress(
                        getResources().getString(R.string.nfcWriteDialogTagCapacityIs) + ndef.getMaxSize()
                                + " byte" + getResources().getString(R.string.nfcWriteDialogTagMessageSizeIs)
                                + ndefMsg.getByteArrayLength() + "byte.",
                        false);
                success = false;
            }

            ndef.writeNdefMessage(ndefMsg);
            success = true;
        } else {
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null) {
                try {
                    format.connect();
                    format.format(ndefMsg);
                    publishProgress(getResources().getString(R.string.nfcWriteDialogTagFormatedAndWrote), true);
                    success = true;
                } catch (IOException e) {
                    publishProgress(getResources().getString(R.string.nfcWriteDialogTagFailedToFormat), false);
                    success = false;
                }
            } else {
                publishProgress(getResources().getString(R.string.nfcWriteDialogTagNoNDEFSupport), false);
                success = false;
            }
        }
    } catch (Exception e) {
        Log.e(this.toString(),
                getResources().getString(R.string.nfcWriteDialogTagFailedToWrite) + " (" + e.getMessage() + ")",
                e);
    }

    return success;
}