Android NFC java.io.IOException:收发失败 [英] Android NFC java.io.IOException: Transceive failed

查看:1017
本文介绍了Android NFC java.io.IOException:收发失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个可以在NFC标签上读写的android应用程序. 读取已经写过标签的标签没有问题,但是当我使用空白标签时,在十六进制代码中读取标签的UID会遇到困难.

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.

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

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.

推荐答案

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

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

这篇关于Android NFC java.io.IOException:收发失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆