使用Nfca开发Android应用时出现TagLostException异常

4

我目前正在为支持NFC的智能手机开发一个安卓应用程序(使用JAVA语言)。我试图与Nfca标签进行通信,但无论我通过 transceive(byte[])发送的命令是什么,我都会收到一个 TagLostException 异常。该标签已经正确连接到设备上。

下面是重要的代码部分:

public void onNewIntent(Intent intent) {
    setIntent(intent);

    tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // get the tag object for the discovered tag

    nfca = NfcA.get(tag); // try and get the instance for this tag
    if(nfca == null) {
        Alert("tag","the tag is not NfcA");
    } else {
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1); //this will call handleMessage function and handle all error
            }
        }).start();
    }
}

final Handler handler = new Handler() {
    //handles the connection with the tag
    @Override
    public void handleMessage(final Message msgs) {
        // try to connect to the NfcA tag
        try {
            nfca.connect();
        } catch(IOException e) {
            Alert("nfca.connect","cannot open connection ><");
        }
        Toast.makeText(NfcaActivity.this, "tag connected", Toast.LENGTH_LONG).show();

    }
};

try {
    //nfca.setTimeout(3000);
    pageBuffer = nfca.transceive(command);                                                                      
} catch(IOException e) {
    Alert("pageBuffer","cannot perform transceive "+ e.toString());
} catch(Exception e) {
    Alert("exception!", e.toString());
}

我正在尝试使用以下命令:{00 CA 42 08 00}{00 A4 00 00 02 3F00},但结果相同。
当我在计算机上使用智能卡阅读器和软件时,这些命令有效。智能卡非空。
1个回答

3

我希望这张卡片也具备技术,如果是这样的话,应该使用它进行连接。你列出的命令之一看起来像是用于选择主文件的ISO 7816-4 APDU命令。这应该与ISO 14443-4卡一起使用,而这正是技术所代表的。


是的,该卡还支持IsoDep技术,而且我正在使用ISO 7816-4 APDU命令。那么我能否使用Nfca技术连接并与该卡通信?如果可以,那么ISO 7816-4是否是正确的命令集发送方式?或者我只能使用IsoDep来连接它? - Jinbonka
对于ISO 7816-4,您应该使用“IsoDep”(与您使用“NfcA”的方式相同;只需在代码中用“IsoDep”替换该字符串)。 - NFC guy
我的命令确实可以使用IsoDep,但我想使用NfcA,如果可能的话,我应该使用哪些协议? - Jinbonka
NfcA是IsoDep协议层下面的一个协议。如果您要使用NfcA进行标签通信,您需要在应用程序中实现ISO 14443-4(由于时间要求相当严格,这可能很困难),因为这是您的标签支持的协议。但是没有必要这样做,因为Android提供了与标签支持的内容相对应的IsoDep。 - NFC guy
嗨,我正在使用IsoDep技术,但是当我尝试使用transceive()时,出现了TagLostException异常。你能给予任何建议吗? - ericn
1
@fuzzybee 尝试使用 setTimeout() 将响应超时设置为更高的值。 - NFC guy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接