Android Studio NFC 讀寫卡程序 (二)


上一章簡單的介紹了 一下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進制字符串,然后再轉為自己用的字符串就可以了

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM