2013-05-10 128 views
0

我想存储一个整数值并用API函数递增或递减它。android nfc - mifare classic 1k增量操作失败失败

我已经readed与实用的卡,这是块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(); 

我在this.mClassic.increment(firstBlock, 1); 我不明白我在做什么错返回tranceive failed ...谁可以帮我? 非常感谢。

+0

你能清理一下你的代码并告诉我们你想要做什么吗? – ThomasRS 2013-05-10 11:35:48

+0

我想存储一个整数值并用API函数递增或递减它。 – michele 2013-05-10 13:21:29

回答

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位值上面的格式,你的代码很可能会正常工作。

+0

我如何使用java创建它?谢谢 – michele 2013-05-12 12:56:19

+2

对不起,你必须亲自编写代码。 – 2013-05-12 13:33:01