开发者

Nexus S: Writing MifareClassic NFC Tags fails when not in debug mode

I've got a weird problem. When I debug my program and put a breakpoint before the "writeBlock" command to write my MifareClassic card, everything is going fine. The card is written and my program continues.

If I remove the breakpoint, I get an "IO Exception : transceived failed"! I put the breakpoint back without changing my code, it works again!

I'm lost... Could it be possible that the problem comes from the speed of the program execution? Having a breakpoint makes the execution slower...

Here's my code (the authentication is done before this function):

private static boolean WriteMfcBlock(MifareClassic mfc, int blockNumber, byte[] value) {
    try {
        byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];

        //if the value is less than 16 bytes, fill it with '0'
        for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) {
            if (i < value.length) toWrite[i] = value[i];
            else toWrite[i] = 0;
        }

        if (!mfc.isConne开发者_开发技巧cted()) mfc.connect();

        mfc.writeBlock(blockNumber, toWrite);

        //Check if the writing is well done
        byte[] read = mfc.readBlock(blockNumber);
        for (int i = 0; i < MifareClassic.BLOCK_SIZE; i++ ) {
            if (toWrite[i] != read[i]) return false;
        }

        return true;
    }
    catch (IOException e) {
        textViewInfo.setText("IO EXCEPTION");
        return false;
    }
}

Thanks for your help

Sylvain


I go a step forward. It seems that it can come from a thread issue. The "writeblock" command of the MifareClassic has to be triggered by the main process of the activity. In my app, it's a button (implementing OnClickListener) that triggered the "writeblock". When in debugging mode, the debug thread can hide this behavior because it's the main thread and make the app running well.

So from now, what I did is just to ask the user to remove the tag from the rf field and put it back. So I get the intent that a tag has been discovered again and then I can do the "writeblock" command without any problem.

Finally I thing the best way to handle read and write on tags is the create 2 activities, one for readings and one for writings.

If you have any comment or other way to do it .. please, answer this thread.

Sylvain

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜