我目前正在为支持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}
,但结果相同。当我在计算机上使用智能卡阅读器和软件时,这些命令有效。智能卡非空。
IsoDep
技术,但是当我尝试使用transceive()
时,出现了TagLostException
异常。你能给予任何建议吗? - ericn