Android NFC java.io.IOException: Transceive failed

依然范特西╮ 提交于 2019-12-07 01:45:19

问题


I am working on an android application which can read and write on an NFC tag. I have no problem reading a tag which I already wrote something on, but when I use a blank tag I have difficulties reading the UID of the tag in the HEX code.

I am using mifare classic tags and I read the UID directly in the hex with the readblock method. The strange thing is, it works perfectly on debugger mode where I get the UID. But when I am trying without debbuger I get the following exception:

   java.io.IOException: Transceive failed

Here's my method to read into the tag :

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

I have no idea how to fix this, since it works in debug mode.


回答1:


This code works for me. you have to check the authentication before you can read a block.

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();
}



回答2:


May be There is Authentication Issue. You can Authenticate this in this Way....

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

Here SectorNumber is : The sector you want to authenticate. eg:0,1,2....15 for mifare Classic 1K When Authentication is done Then you can read or write.



来源:https://stackoverflow.com/questions/17401154/android-nfc-java-io-ioexception-transceive-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!