2012-08-31 29 views

回答

2

您应该将格式正确的数据写入标签。示例请参阅MIFARE Classic datasheet的第8.6.2节。

实施例的Android代码为整数value存储为值块在块blockIndex

// connect to the tag using a Tag object from an NFC intent 
MifareClassic mifare = MifareClassic.get(tag); 
mifare.connect(); 

// need to authenticate first to get access 
int sector = blockToSector(blockIndex); 
mifare.authenticateSectorWithKeyA(sector, keyA); // you need to know key A 
// mifare.authenticateSectorWithKeyB(sector, keyB); // in case you know key B 

// construct value block of value zero; "address" byte is set to 0 in this example 
byte[] zeroValue = {0, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 255, 0, 255 }; 
mifare.writeBlock(blockIndex, zeroValue); 

// increase the value block by some amount 
mifare.increment(blockIndex, value); 
// result is stored in scratch register inside tag; now write result to block 
mifare.transfer(blockIndex); 

请注意,用于块的访问控制位需要被正确地设置,以允许递增操作的键你用来验证。

+0

非常感谢!这真的很有用!抱歉这么晚才收到你。 – James

+0

我只有writeBlock而不写...是否正常? – michele

+0

@michele更正,谢谢 –