Android makeReadOnly()抛出IOException

时间:2015-06-10 08:05:02

标签: android nfc ndef

对于Android-App,我想在NDEF-Tag上写一条消息,并确保之后不能更改消息。显而易见的解决方案是方法makeReadOnly(),但它总是抛出没有消息(null)的java.io.IOException。 我已经尝试了以下内容:

  • 检查标签是否可写
  • 在标签上写了一条消息
  • 如果理论上可以将标签设为只读,请使用canMakeReadOnly()进行检查
  • 在另一个线程中使用makeReadOnly,然后在主线程中使用
  • 使用应用程序Tag-Writer密封标签,以确保我使用的标签可以完全密封
  • 发现IOException是此方法的默认异常,因此错误可能与I / O(Why I'm getting IOException when making NFC tag read only)无关,但那里的答案只能说明异常并不需要是一个IOException,但它没有解决问题。

以下是有关代码:

                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看起来像这样:

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

任何帮助都会很棒!

0 个答案:

没有答案