简体   繁体   English

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

[英]How to Construct a value block on MifareClassic via Android NFC API

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

You should just write properly formatted data to the tag. 您应该只将正确格式化的数据写入标签。 See section 8.6.2 of the MIFARE Classic datasheet for an example. 有关示例,请参见MIFARE Classic数据表的 8.6.2节。

Example Android code to store an integer value as value block in block blockIndex : 用于在块blockIndex中将整value存储为值块的示例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);

Keep in mind that the access control bits for the block need to be set correctly to allow the increment operation for the key you use to authenticate. 请记住,需要正确设置块的访问控制位,以允许对用于进行身份验证的密钥进行增量操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM