androidnfc

How can I reconnect to IsoDep tag?


I'm trying to send some apdu commands from android phone to the nfc tag and get answers. The type of used technology is IsoDep. Everything works fine, but sometimes, when the time between sending commands is too large, the tag switches to the disabled state and after that every reconnection fails.

My code:

public byte[] transferCommand(byte[] command) throws Exception {
        byte[] result = null;
        if (iso == null)   {
            iso = IsoDep.get(tag);
            iso.connect();
        }
        if (!iso.isConnected()) {
            try {
                iso.close();
                iso.connect();
                result = iso.transceive(command);
            } catch (Exception ex) {
                iso.close();
            }
        }            
        return result;
    }

Could anybody help me please? Thank so much.


Solution

  • The connect and related commands are just managing a logical connection to the tag. That is: They grant your thread and application exclusive access to the tag object. They don't do anything with the physical tag connection. (At least as far as I know, it's been a while since I last read the NfcService code).

    Therefore connecting and reconnecting will not help you once a tag stops to answer to your requests. All you can do in this case is to physically remove the tag and present it to the reader again.

    If you run into timeout problems try raising the timeout value by calling setTimeout on the tag object.