android nfc - mifare classic 1k Increment operation tranceive failed

纵然是瞬间 提交于 2019-12-25 11:54:21

问题


I want to store an integer value and increment or decrement it with API function.

I have readed the card with an utility and this is the content of block 5:

It seems that there is not any value block.

This is my code:

    int sector = 5;
    this.mClassic.connect();
    boolean success = this.mClassic.authenticateSectorWithKeyA(sector, MifareClassic.KEY_DEFAULT );

        if(success){
            int firstBlock = mClassic.sectorToBlock(sector);
            Log.i("MIFARE CLASSIC", "first block of the given sector:" + firstBlock);


            //set the value = 0
            byte[] zeroValue = {0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,};
            //save this value 
                            mClassic.writeBlock(firstBlock, zeroValue);

            //increment the value and store it
            this.mClassic.increment(firstBlock, 1);
            this.mClassic.transfer(firstBlock);

            // read the incremented value by converting it in integer from bytearray
            b = readSector(firstBlock);
            data = b.toByteArray();
            value = 0;
            for (int i = 0; i < data.length; i++)
            {
               value = (value << 8) + (data[i] & 0xff);
            }
            Log.i("MIFARE CLASSIC", "After increment " + value);
        }
        mClassic.close();

I have returned tranceive failed at this.mClassic.increment(firstBlock, 1); I don't understand what I am doing wrong...who can help me? Thanks a lot.


回答1:


The Mifare 1K does a data-integrity check on the value-block. Your zeroValue block is unfortunately not a valid value-block. Therefore the tag complains and you get an error.

You can find the format in the Mifare Datasheets (worth reading!)

However, the format of the value-block is simple:

byte 0..3:   32 bit value in little endian
byte 4..7:   copy of byte 0..3, with inverted bits (aka. XOR 255)
byte 8..11:  copy of byte 0..3
byte 12:     index of backup block (can be any value)
byte 13:     copy of byte 12 with inverted bits (aka. XOR 255)
byte 14:     copy of byte 12
byte 15:     copy of byte 13

If you store your 32 bit value using the format above, your code will very likely just work.



来源:https://stackoverflow.com/questions/16480205/android-nfc-mifare-classic-1k-increment-operation-tranceive-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!