Example usage for android.nfc NdefMessage toString

List of usage examples for android.nfc NdefMessage toString

Introduction

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

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.macleod2486.androidswissknife.components.NFCCallback.java

@Override
public void onTagDiscovered(Tag tag) {

    EditText tagResult = activity.findViewById(R.id.textEntry);

    try {/*from   w  ww . j  a v  a  2s.  co  m*/
        Ndef ndef = Ndef.get(tag);

        if (ndef != null) {
            NdefMessage ndefMesg = ndef.getCachedNdefMessage();
            if (ndefMesg != null) {
                nfcData = "Message: " + ndefMesg.toString();
                Log.i("NFCCallback", nfcData);
            }
        } else {
            nfcData = "Attempting to format";
            Log.i("NFCCallback", "Attempting to format");

            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tagResult.setText("");
                    tagResult.append("Attempting to format");
                }
            });

            NdefFormatable format = NdefFormatable.get(tag);

            if (format != null) {
                format.connect();

                if (format.isConnected()) {
                    try {
                        format.format(null);
                        nfcData = "Formatted";
                        Log.i("NFCCallback", "Formatted");
                    } catch (Exception e) {
                        nfcData = "Error occurred in formatting";
                        e.printStackTrace();
                    }

                    format.close();
                }
            } else {
                Log.i("NFCCallback", "Tag not found");
                nfcData = "Tag not found or formatted";
            }
        }

        if (tagResult != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tagResult.setText("");
                    Log.i("NFCCallback", "Cleared");
                    tagResult.append(nfcData);
                }
            });
        }
    } catch (Exception e) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tagResult.setText("");
                tagResult.append("Error reading tag");
            }
        });

        e.printStackTrace();
    }
}