如何通过Android NFC API在MifareClassic上构建一个价值块

时间:2012-08-31 03:36:01

标签: android nfc mifare

我的问题是Android NFC API已经提供了“增量”和“递减”值块,但如果我有一个新的MifareClassic标签(它内部没有任何值块),我该如何使用Android NFC用于在此新标记上构造值块的API?

1 个答案:

答案 0 :(得分:2)

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

用于在块value中将整数blockIndex存储为值块的Android代码示例:

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

请记住,需要正确设置块的访问控制位,以允许对用于进行身份验证的密钥进行增量操作。