Android makeReadOnly() throws IOException

387 views Asked by At

For an Android-App I want to write a message on a NDEF-Tag and make sure that the message cannot be altered afterwards. The obvious solution is the method makeReadOnly(), but it always throws a java.io.IOException without a message (null). I have already tried the following:

  • Check if the tag is writable
  • Wrote a message on the tag
  • Check with canMakeReadOnly() if it is in theory possible to make the tag read-only
  • Use makeReadOnly in another Thread then the main-thread
  • Sealed a tag with the app Tag-Writer, to make sure that the tags I use can be sealed at all
  • Found out that IOException is the default-exception for this method, so maybe the error has nothing to do with I/O (Why I'm getting IOException when making NFC tag read only) but the answer there only explains that the exception doesn't need to be an IOException, but it doesn't solve the problem.

Here comes the concerning code:

                try {
                ndefTag.connect();

                if(ndefTag.canMakeReadOnly()) {
                    Log.d("Schuetzen", "makeReadOnly is possible");
                }
                else {
                    Log.d("Schuetzen", "makeReadOnly is not possible");
                }

                if(ndefTag.isWritable())
                {
                    Log.d("Schuetzen", "Tag isWritable");
                }

                ndefTag.makeReadOnly();
            }

            catch (IOException e) {
                e.printStackTrace();
                Log.d("myThread", e.toString());
            }

            finally {
                // Close connection:
                try {
                    ndefTag.close();
                    Log.d("myThread", "Tag closed");
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }

Logcat looks like this:

06-10 09:51:04.851  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter D/Schuetzen﹕ makeReadOnly is possible
06-10 09:51:04.851  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter D/Schuetzen﹕ Tag isWritable
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ java.io.IOException
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at android.nfc.tech.Ndef.makeReadOnly(Ndef.java:394)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at net.bits_bremen.bruenjes.gleisteinwriter.myThread.doInBackground(myThread.java:34)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
06-10 09:51:04.898  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter D/myThread﹕ java.io.IOException
06-10 09:51:04.914  32274-14000/net.bits_bremen.bruenjes.gleisteinwriter D/myThread﹕ Tag closed

Any help would be great!

0

There are 0 answers