Example usage for android.nfc FormatException FormatException

List of usage examples for android.nfc FormatException FormatException

Introduction

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

Prototype

public FormatException(String message) 

Source Link

Usage

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;/*from  w w  w.  j a va2  s  .  c  o m*/

            try {
                thisNdef = Ndef.get(receivedTag);
            } catch (Exception e) {
                BugSenseHandler.sendExceptionMessage("WriteNFCActivity", "WriteTag", e);
                e.printStackTrace();
            }

            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();
}