2012-07-08 136 views
4

我正在尝试在Mifare 1K标记上写入一个简单的“Hello World”字符串到扇区0的第一个块。该标签是全新的默认配置。当读取扇区预告片(块3)时:00 00 00 00 00 00 00 00 ff 07 80 69 ff ff ff ff ff ff ff ff。因此,访问条件是ff 07 80 69,这意味着我可以读写每个块上的键A.使用Android NFC书写Mifare标签?

不过,我无法设法在标签上写任何东西。这里是我的代码:

try { 
    mfc.connect(); 
    boolean auth = false; 

    auth = mfc.authenticateSectorWithKeyA(0,MifareClassic.KEY_DEFAULT); 

    if (auth) { 

     String text = "Hello, World!"; 
     byte[] value = text.getBytes(); 
     byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];   

     for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) { 
       if (i < value.length) toWrite[i] = value[i]; 
       else toWrite[i] = 0; 
     }   

     mfc.writeBlock(0, toWrite); 
    }  

,我发现了以下异常:Transceived failed

我在做什么错?

这里的堆栈跟踪:

07-09 00:19:44.836: W/System.err(13167): at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:52) 
07-09 00:19:44.843: W/System.err(13167): at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151) 
07-09 00:19:44.843: W/System.err(13167): at android.nfc.tech.MifareClassic.writeBlock(MifareClassic.java:453) 
07-09 00:19:44.843: W/System.err(13167): at com.example.andorid.apis.mifare.MainActivity.resolveIntent(MainActivity.java:128) 
07-09 00:19:44.843: W/System.err(13167): at com.example.andorid.apis.mifare.MainActivity.onNewIntent(MainActivity.java:275) 
07-09 00:19:44.843: W/System.err(13167): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1123) 
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2041) 
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2054) 
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2063) 
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.access$1400(ActivityThread.java:122) 
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1193) 
07-09 00:19:44.851: W/System.err(13167): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-09 00:19:44.851: W/System.err(13167): at android.os.Looper.loop(Looper.java:137) 
07-09 00:19:44.851: W/System.err(13167): at android.app.ActivityThread.main(ActivityThread.java:4340) 
07-09 00:19:44.851: W/System.err(13167): at java.lang.reflect.Method.invokeNative(Native Method) 
07-09 00:19:44.851: W/System.err(13167): at java.lang.reflect.Method.invoke(Method.java:511) 
07-09 00:19:44.851: W/System.err(13167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-09 00:19:44.851: W/System.err(13167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-09 00:19:44.851: W/System.err(13167): at dalvik.system.NativeStart.main(Native Method) 
+0

您可以显示整个堆栈跟踪? – Brad 2012-07-08 23:51:25

+0

是的,看我的编辑! – 2012-07-09 00:21:36

+0

我得到了同样的错误Transceive失败。我认为这是Android ICS4.0.3和ICS 4.0.4的错误。 我在S3 i9300和Galaxy Note E160K上测试过,都失败了。其他应用程序可以通过NDEF对我的MiFare 1k卡进行读写。 – fordiy 2012-07-13 03:29:20

回答

7

您正在尝试写块0,这是不可能的。块0始终是只读的,即使访问条件是可写的。块0包含UID和一些其他制造商数据。尝试写入块1或2,看看你是否仍然有同样的问题。

1

试图从没有连接到任何读者多标签读取时,我已经看到了特定的异常。

我知道,有些卡/读卡器的组合是很难维持一个体面的连接。

例如我的Nexus S有麻烦保持与一些标签的连接。 Nexus S没有最强的领域。我认为Galaxy Note在这方面是相似的。

尝试一些其他品牌的标签,以及其他读者。

而且,我想尝试通过一个循环运行的读,看它是否保持连接,如果是有道理的。

+0

不......我不认为这是问题所在。我可以轻松地将数据写入恩智浦标签编写器。我有一个星系连结i9250。 – 2012-07-09 01:42:21