0

我想存储一个整数值并使用 API 函数递增或递减它。

我已经用实用程序读取了卡片,这是第 5 块的内容: 在此处输入图像描述

似乎没有任何值块。

这是我的代码:

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

我回来tranceive failed了, this.mClassic.increment(firstBlock, 1); 我不明白我做错了什么......谁能帮助我?非常感谢。

4

1 回答 1

5

Mifare 1K 对值块进行数据完整性检查。不幸的是,您的 zeroValue 块不是有效的值块。因此标签会抱怨,你会得到一个错误。

您可以在 Mifare 数据表中找到格式(值得一读!)

但是,值块的格式很简单:

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

如果您使用上述格式存储 32 位值,您的代码很可能会正常工作。

于 2013-05-11T13:55:09.280 回答