7

我正在开发一个可以在 NFC 标签上读写的 android 应用程序。我在阅读我已经写过一些东西的标签时没有问题,但是当我使用空白标签时,我很难在 HEX 代码中读取标签的 UID。

我正在使用 mifare 经典标签,我使用 readblock 方法直接以十六进制读取 UID。奇怪的是,它在我获得 UID 的调试器模式下完美运行。但是当我在没有调试器的情况下尝试时,我得到以下异常:

   java.io.IOException: Transceive failed

这是我读入标签的方法:

    static String getUID(Intent intent) {

    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    MifareClassic mc = MifareClassic.get(tagFromIntent);

    try {
        mc.connect();
        Log.i("connect", "ok");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.i("connect", "nok");
        e.printStackTrace();
    }
    try {
        boolean secA = mc.authenticateSectorWithKeyA(0, mc.KEY_DEFAULT);
        Log.i("secA", "ok");
    } catch (IOException e) {
        Log.i("secA", "nok");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        boolean secB = mc.authenticateSectorWithKeyB(0, mc.KEY_DEFAULT);
        Log.i("secB", "ok");
    } catch (IOException e) {
        Log.i("secB", "nok");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] uidBytes = null;

    try {

        uidBytes = mc.readBlock(0);
        Log.i("bytes", "ok");

    } catch (IOException e) {
        Log.i("bytes", "nok");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        mc.close();
        Log.i("close", "ok");
    } catch (IOException e) {
        Log.i("close", "nok");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (uidBytes != null) {
    String uid = HexToString(uidBytes);

    return uid;
    }
    else { return "Repasser le tag";}
}

我不知道如何解决这个问题,因为它在调试模式下工作。

4

3 回答 3

3

这段代码对我有用。您必须先检查身份验证,然后才能读取块。

MifareClassic mif = MifareClassic.get(detectedTag);

int ttype = mif.getType();
Log.d(TAG, "MifareClassic tag type: " + ttype);

int tsize = mif.getSize();
Log.d(TAG, "tag size: " + tsize);

int s_len = mif.getSectorCount();
Log.d(TAG, "tag sector count: " + s_len);

int b_len = mif.getBlockCount();
Log.d(TAG, "tag block count: " + b_len);
try {
    mif.connect();
    if (mif.isConnected()){

        for(int i=0; i< s_len; i++){

            boolean isAuthenticated = false;

            if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {
               isAuthenticated = true;
            } else if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)) {
               isAuthenticated = true;
            } else if (mif.authenticateSectorWithKeyA(i,MifareClassic.KEY_NFC_FORUM)) {
               isAuthenticated = true;
            } else {
                Log.d("TAG", "Authorization denied ");
            }

            if(isAuthenticated) {
                int block_index = mif.sectorToBlock(i);

                byte[] block = mif.readBlock(block_index);
                String s_block = NfcUtils.ByteArrayToHexString(block);
                Log.d(TAG, s_block);
            }
        }
    }
    mif.close();

} catch (IOException e) {
    e.printStackTrace();
}
于 2016-10-19T09:29:36.540 回答
0

可能存在身份验证问题。您可以通过这种方式对此进行身份验证....

if (mfc.authenticateSectorWithKeyA(sectorNumber,
                MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {
            Log.d("TAG", "Authorized sector with MAD key");

        } else if (mfc.authenticateSectorWithKeyA(
                sectorNumber, MifareClassic.KEY_DEFAULT)) {
            Log.d("TAG",
                    "Authorization granted to sector  with DEFAULT key");

        } else if (mfc
                .authenticateSectorWithKeyA(sectorNumber,
                        MifareClassic.KEY_NFC_FORUM)) {
            Log.d("TAG",
                    "Authorization granted to sector with NFC_FORUM key");

        } else {
            Log.d("TAG", "Authorization denied ");

            return false;
        }

这里 SectorNumber 是:您要验证的扇区。eg:0,1,2....15 for mifare Classic 1K 认证完成后就可以读写了。

于 2015-01-24T10:57:42.540 回答
-1

也许您必须先对您的卡进行身份验证..

于 2022-02-28T12:15:39.143 回答