5
votes

I'm trying to write a simple "Hello World" string to the first block of sector 0 on a Mifare 1K tag. The tag is brand new with default configuration. The sector trailer (block 3), when read, is: 00 00 00 00 00 00 00 00 ff 07 80 69 ff ff ff ff ff ff ff ff. Hence, the access conditions is ff 07 80 69, meaning I can read and write on each block with key A.

Still, I can't manage to write anything on the tag. Here's my code:

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

I'm getting the following exception: Transceived failed.

What am I doing wrong?

Here's the stack trace:

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)
2
Can you show the entire stack trace?Brad
I got same error Transceive failed. I do think it's Android ICS4.0.3 and ICS 4.0.4 bugs. I tested on S3 i9300 and Galaxy Note E160K, all failed. My MiFare 1k card is readable and writable via NDEF by other applications.fordiy

2 Answers

8
votes

You are trying to write to block 0, which is not possible. Block 0 is always read-only, even of the access conditions say that it is writeable. Block 0 contais the UID and some other manufacturer data. Try writing to block 1 or 2 instead and see whether you still have the same problem.

1
votes

I've seen that particular exception when trying to read from a Tag that isn't connected to the reader any more.

I know that for some card/reader combinations it is difficult to maintain a decent connection.

For example my Nexus S has trouble maintaining a connection with some tags. The Nexus S doesn't have the strongest field. I think the Galaxy Note is similar in that respect.

Try some other brands of tags as well as other readers.

Also, I'd try running your read through a for loop to see if it maintains a connection, if that makes sense.