Android NFC java.io.IOException:Transceive失败

时间:2013-07-01 09:27:05

标签: android exception nfc

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

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

   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";}
}

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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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是:您要验证的扇区。例如:0,1,2 .... 15为mifare Classic 1K 验证完成后,您可以阅读或写入。