RFID 卡 NFC 卡的分类

小区门禁卡,办一个50,2个就是100,太贵了。常用的卡是 M1 卡,有 16个扇区,每个扇区能存储 64个字节,正好就是 1024个字节,就是 1KB

卡的分类:
ID卡:最普通的卡,0扇区内容,只能读,不能写,写有卡号,出厂写死,不可修改,其它扇区可以读写
UID卡:0扇区内容能读能写,其它扇区可读写,也叫空白卡
CUID卡:带防火墙的卡,0扇区内容能读能写,其它扇区可读写,也叫空白卡

公交卡:卡中带芯片,高级卡

安卓平台操作 NFC 相关:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nejidev.nfctester">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

        </activity>
    </application>

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

</manifest>

添加 intent-filter 当扫到卡时,会拉起应用。

    public NfcAdapter NfcCheck() {
        NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if(null == mNfcAdapter){
            Toast.makeText(this,"设备不支持NFC功能!",Toast.LENGTH_LONG);
        }
        else{
            if (!mNfcAdapter.isEnabled()) {
                Toast.makeText(this, "请手动打开NFC功能", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(this, "NFC功能已打开!", Toast.LENGTH_SHORT).show();
            }
        }
        return mNfcAdapter;
    }

检查 NFC 功能是否支持或打开

2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: onResume
2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: action:android.nfc.action.TAG_DISCOVERED
2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: tech:android.nfc.tech.MifareClassic
2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: tech:android.nfc.tech.NfcA
2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: tech:android.nfc.tech.NdefFormatable
2022-12-07 14:39:29.418 3816-3816/com.nejidev.nfctester I/NFCTest: id:F5601234
2022-12-07 14:39:29.419 3816-3816/com.nejidev.nfctester I/NFCTest: metaInfo:卡片类型:TYPE_CLASSIC

实际读卡时的 LOG

        boolean auth = false;
        int bCount;
        int bIndex;
        for (int i = 0; i < sectorCount; i++) {
            metaInfo = "";
            try {
                auth = mfc.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT);

                if (auth) {
                    metaInfo += "Sector " + i + ":验证成功\n";
                    // 读取扇区中的块
                    bCount = mfc.getBlockCountInSector(i);
                    bIndex = mfc.sectorToBlock(i);
                    for (int j = 0; j < bCount; j++) {
                        byte[] data = mfc.readBlock(bIndex);
                        metaInfo += "Block " + bIndex + " : "
                                + bytesToHex(data) + "\n";
                        //test write
                        if(1==i && 0 == j){
                            mfc.writeBlock(bIndex, hexToByteArray("01020304000000000000000000000000"));
                        }
                        bIndex++;
                    }
                }
                else {
                    metaInfo += "Sector " + i + ":验证失败\n";
                }
                } catch (IOException e) {
                e.printStackTrace();
            }
            Log.i(TAG, "metaInfo:" + metaInfo);
        }

读写扇区的代码。

 

posted @ 2022-12-04 13:45  宁次  阅读(1402)  评论(0编辑  收藏  举报