android nfc - mifare classic 1k增量操作tranceive失败

时间:2013-05-10 10:27:40

标签: android tags nfc mifare android-beam

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

我已经使用实用程序来刷卡,这是第5块的内容: enter image description here

似乎没有任何价值块。

这是我的代码:

    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); 我不明白我做错了什么......谁能帮帮我? 非常感谢。

1 个答案:

答案 0 :(得分: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位值,则代码很可能正常工作。