Ksenia
Ksenia

Reputation: 3723

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.

Upvotes: 3

Views: 1865

Answers (2)

Peng
Peng

Reputation: 21

I know this is an old thread. But I happened to get the same issue. Constantly, if the connection idles for a long time, say about 1min, it is closed. The phone couldn't get re-connected even after a re-touching the tag. I have to disable and enable the NFC again from the setting to get the connection back.

From what I can tell during all those tests, I believe that the phone or the OS caused NFC hardware into a malfunction state. Or maybe it was in sleep mode to save power. Because when this happens, a different phone can still connect to the tag. But you have to switch off/on the NFC on the original phone to get the connection again. This could caused by a watch dog timer either in the hardware or in the driver.

One workaround is to ping the NFC tag, such as check the tag status, every 10 seconds or so to maintain the connection. But then, you are draining the power of the phone.

I'm also trying the ignore method in the NfcAdapter to see if it is a more grace way to handle this.

Tag: NFC Connection Disconnected Timeout

Upvotes: 1

Nils Pipenbrinck
Nils Pipenbrinck

Reputation: 86393

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.

Upvotes: 4

Related Questions