Search code examples
androidtagsnfcapdu

TagLostException using Nfca for an Android application


I am currently developing an Android application (in JAVA) for NFC smartphones. I am trying to communicate to an Nfca tag but keep getting a TagLostException no matter the command I send via transceive(byte[]). The tag is well connected to the device.

Here is the important piece of code:

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

I am trying with the following command : {00 CA 42 08 00} or {00 A4 00 00 02 3F00} but have the same results.

Those commands are working when I am using a smart card reader and a software on my PC. The smart card is non-empty.


Solution

  • I expect that the card has the IsoDep technology available, too? If that is the case, you should connect using that. One of the commands you list looks like an ISO 7816-4 APDU to select the Master File. This is supposed to be used with ISO 14443-4 cards, which is exactly what the IsoDep technology represents.