Android Studio NFC 讀寫卡程式 (二)

來源:互聯網
上載者:User

標籤:generate   ultra   驗證   exception   密碼   toast   can   字元   儲存空間   

上一章簡單的介紹了 一下NFC android 中的認識 和配置,這次認識一下NFC 卡片。

卡片一共 分為16個扇區(sector),每個扇區又分為4個塊(block) 每個塊 包含16個位元組,

 

比如“1234567890123456”, “ASDFGHJKLQWERTYU”,可以存取一些資訊,

因為卡片的類型不同,所以在存取的時候需要注意一下,

比如 對於一般的 MifareClassic 來說,  第一扇區 第一塊 一般會被廠家佔用,這裡是不能被寫入資料的,每一扇區的 最後一塊,也就是第四block,是用來存放密碼或控制位的,其餘的三個塊是資料區,這裡可以存放我們的資料。

而對於 MifareUltralight 來說, 一般 前四扇區 是不能夠 寫資料的,而且沒有密碼。

 

當你在第幾扇區第幾塊中寫的資料,就需要 在第幾扇區第幾塊中讀出來。

寫資料流程為: data (符合要求的16位) 

 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 is used for KeyA and KeyB cannot be overwritted                  mfc.writeBlock(4, "1313838438000000".getBytes());                  mfc.writeBlock(5, "ASDFGHJKLl000000".getBytes());                  mfc.close();                  Toast.makeText(this, "寫入成功", 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();    

我們把資料寫入了第一扇區的第四和第五塊,

然後從第四區和第五區讀出來

 public String readTag(Tag tag) {          MifareClassic mfc = MifareClassic.get(tag);          for (String tech : tag.getTechList()) {              System.out.println(tech);          }          boolean auth = false;          //讀取TAG            try {              String metaInfo = "";              //Enable I/O operations to the tag from this TagTechnology object.              mfc.connect();              int type = mfc.getType();//擷取TAG的類型              int sectorCount = mfc.getSectorCount();//擷取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 += "卡片類型:" + typeS + "\n共" + sectorCount + "個扇區\n共"                      + mfc.getBlockCount() + "個塊\n儲存空間: " + 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 + ":驗證成功\n";                      // 讀取扇區中的塊                      bCount = mfc.getBlockCountInSector(j);                      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 + ":驗證失敗\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;    

  

上面是把 卡片的所有資訊 讀出來,如果需要第四和第五的,自己可以改動取值。

因為存在卡片中的為16進位(不是16進位字串),所以需要 先轉為16進位字串,然後再轉為自己用的字串就可以了

 

代碼就不貼了,網上一搜一大把,關鍵是 認識 自己需要什麼樣的 類型,不要用錯了

 

Android Studio NFC 讀寫卡程式 (二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.