Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Mifare Tag read and write in Android NFC Development

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you a sample analysis of Mifare Tag reading and writing in Android NFC development, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Specific instructions for commonly used Mifare Tag.

Mifare Tag can have 1K, 2K, 4K, and its memory partition is more or less the same. The following figure shows the memory distribution of Tag with a capacity of 1K bytes:

The data is divided into 16 regions (Sector), each with 4 blocks (Block). Each block can store 16 bytes of data with a size of 16 X 4 X 16 = 1024 bytes.

Each zone * has a block called Trailer, which is mainly used to store the Key for reading and writing Block data in this area. There can be two Key of A Key B, each of which is 6 bytes long. The default key value is either full FF or 0. Defined by MifareClassic.KEY_DEFAULT.

Therefore, reading and writing Mifare Tag must first have the correct key value (for protection). If authentication is successful:

Auth = mfc.authenticateSectorWithKeyA (j, MifareClassic.KEY_DEFAULT)

Then you can read and write the data for the area.

This example defines several Mifare-related classes MifareClassCard, MifareSector, MifareBlock and MifareKey to facilitate reading and writing Mifare Tag.

The Android system detects the NFC Tag, encapsulates it into a Tag class, and stores it in the NfcAdapter.EXTRA_TAG Extra packet of Intent. You can use MifareClassic.get (Tag) to get the MifareClassic class of the object.

Tag tagFromIntent = intent.getParcelableExtra (NfcAdapter.EXTRA_TAG); / / 4) Get an instance of the Mifare classic card from this TAG / / intent MifareClassic mfc = MifareClassic.get (tagFromIntent)

Here is the main code for reading Mifare card:

/ 1) Parse the intent and get the action that triggered this intent String action = intent.getAction (); / / 2) Check if it was triggered by a tag discovered interruption. If (NfcAdapter.ACTION_TECH_DISCOVERED.equals (action)) {/ / 3) Get an instance of the TAG from the NfcAdapter Tag tagFromIntent = intent.getParcelableExtra (NfcAdapter.EXTRA_TAG); / / 4) Get an instance of the Mifare classic card from this TAG / / intent MifareClassic mfc = MifareClassic.get (tagFromIntent); MifareClassCard mifareClassCard=null; try {/ / 5.1) Connect to card mfc.connect (); boolean auth = false / / 5.2) and get the number of sectors this card has..and loop / / thru these sectors int secCount = mfc.getSectorCount (); mifareClassCard= new MifareClassCard (secCount); int bCount = 0; int bIndex = 0; for (int j = 0; j < secCount; jacks +) {MifareSector mifareSector = new MifareSector (); mifareSector.sectorIndex = j; / 6. 1) authenticate the sector auth = mfc.authenticateSectorWithKeyA (j, MifareClassic.KEY_DEFAULT); mifareSector.authorized = auth If (auth) {/ / 6.2In each sector-get the block count bCount = mfc.getBlockCountInSector (j); bCount = Math.min (bCount, MifareSector.BLOCKCOUNT); bIndex = mfc.sectorToBlock (j); for (int I = 0; I < bCount; iTunes +) {/ / 6.3Read the block byte [] data = mfc.readBlock (bIndex); MifareBlock mifareBlock = new MifareBlock (data); mifareBlock.blockIndex = bIndex; / / 7) Convert the data into a string from Hex / / format. BIndex++; mifareSector.blocks = mifareBlock;} mifareClassCard.setSector (mifareSector.sectorIndex, mifareSector);} else {/ / Authentication failed-Handle it}} ArrayList blockData=new ArrayList (); int blockIndex=0; for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report