Android Studio NFC Reader Card Program (ii)

Source: Internet
Author: User
Tags getmessage

The previous chapter simply introduces the understanding and configuration of NFC Android, and this time you know about NFC cards.

The card is divided into 16 sectors (sector), each sector is divided into 4 blocks (block) Each block contains 16 bytes,

such as "1234567890123456", "Asdfghjklqwertyu", can access some information,

Because the types of cards are different, you need to be aware of them when you access them.

For example, for the General Mifareclassic, the first sector of the first block will generally be occupied by the manufacturers, here is not written data, each sector of the last piece, that is, the fourth block, is used to store passwords or control bits, the remaining three blocks is the data area, where we can store our data.

For Mifareultralight, the general first four sectors are not able to write data, and there is no password.

When you write data in the first few blocks of the first sector, you need to read the first few blocks of the sector.

Write data flow as follows: Data (16-bit compliant)

 public void Writetag (tag tag) {Mifareclassic MFC = Mifareclassic.get (TAG);              try {mfc.connect ();              Boolean auth = false;              Short sectoraddress = 1;              Auth = Mfc.authenticatesectorwithkeya (sectoraddress, Mifareclassic.key_default);                  if (auth) {//the last block of the sector are used for Keya and KeyB cannot be overwritted                  Mfc.writeblock (4, "1313838438000000". GetBytes ());                  Mfc.writeblock (5, "ASDFGHJKLl000000". GetBytes ());                  Mfc.close ();              Toast.maketext (This, "Write succeeded", Toast.length_short). Show ();          }} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();              } finally {try {mfc.close (); } catch (IOException e) {//TODO auto-generated catch block e.printstAcktrace ();     

We write the data to the fourth and fifth blocks of the first sector,

and read it from sector four and fifth.

 Public String Readtag (tag tag) {Mifareclassic MFC = Mifareclassic.get (TAG);          For (String tech:tag.getTechList ()) {System.out.println (tech);          } Boolean auth = false;              Read tag try {String metainfo = "";              Enable I/O operations to the tag from this Tagtechnology object.              Mfc.connect ();  int type = Mfc.gettype ();//Gets the type of the tag int sectorcount = Mfc.getsectorcount ();//Gets the number of sectors contained in the tag String              TypeS = "";                      Switch (type) {case MifareClassic.TYPE_CLASSIC:typeS = "Type_classic";                  Break                      Case MifareClassic.TYPE_PLUS:typeS = "Type_plus";                  Break                      Case MifareClassic.TYPE_PRO:typeS = "Type_pro";                  Break Case MifareClassic.TYPE_UNKNOWN:typES = "Type_unknown";              Break } MetaInfo + = "card type:" + TypeS + "\ n Total" + sectorcount + "sectors \ n Total" + mfc.getblockcount () + "              A block \ n storage space: "+ mfc.getsize () +" b\n ";                  for (int j = 0; J < Sectorcount; J + +) {//authenticate A sector with key A.                  Auth = Mfc.authenticatesectorwithkeya (j, Mifareclassic.key_nfc_forum);                  int bcount;                  int bindex;                      if (auth) {metainfo + = "Sector" + j + ": Verify success \ n";                      Block Bcount = Mfc.getblockcountinsector (j) in the read sector;                      Bindex = Mfc.sectortoblock (j);                          for (int i = 0; i < Bcount; i++) {byte[] data = Mfc.readblock (Bindex); MetaInfo + = "Block" + Bindex + ":" + bytestohexstring (data) + "\ n ";                      bindex++;                  }} else {metainfo + = "Sector" + j + ": Authentication failed \ n";          }} return metainfo;              } catch (Exception e) {Toast.maketext (this, E.getmessage (), Toast.length_long). Show ();          E.printstacktrace ();                  } finally {if (MFC! = NULL) {try {mfc.close ();                              } catch (IOException e) {Toast.maketext (this, E.getmessage (), Toast.length_long)                  . Show ();     }}} return null;

  

The above is to read all the information on the card, if necessary fourth and fifth, you can change the value.

Because there is a 16 binary in the card (not the 16 string), you need to go to the 16 binary string, and then to your own string.

The code is not affixed, a search on the internet a lot, the key is to know what kind of type you need, do not use the wrong

Android Studio NFC Reader Card Program (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.