title: Reading phantom NFC tags via javax.smartcardio
body: <p>I have an an old NFC reader for the tikitag web service (which was later renamed <a href="http://en.wikipedia.org/wiki/Touchatag" rel="nofollow">touchatag</a>, and finally abandoned around 2012). Since the website is no longer available, I could no longer find the original tikitag/touchatag drivers. After some searching, I found this NFC reader is a generic ACS ACR122U USB reader, and installed a suitable driver from <a href="http://www.acs.com.hk/index.php?pid=product&amp;id=ACR122U" rel="nofollow">here</a>. My system is Windows 7 (64-bits). </p>
  
  <p>First, I tried the <a href="https://github.com/grundid/nfctools" rel="nofollow">NFC Tools library</a> for high-level read and write access to NFC tags. I got an error saying an unsupported tag was encountered; although no tag was present on the reader, or even remotely nearby. It seems other developers also encountered the same error with this library, as shown <a href="https://code.google.com/p/nfctools/issues/detail?id=12" rel="nofollow">here</a>. Note  this tag is detected ad infinitum (so, it does not just disappear after being detected once). </p>
  
  <p>I copied the required low-level code into a separate class (i.e., independent from the NFC Tools library). You can find this code below (similar code can also be found in tutorials):</p>
  
  <pre><code>import java.util.List;
  
  import javax.smartcardio.Card;
  import javax.smartcardio.CardTerminal;
  import javax.smartcardio.TerminalFactory;
  
  import org.nfctools.utils.NfcUtils;
  
  public class NdefTest {
  
      public static void main(String[] args) throws Exception {
          TerminalFactory factory = TerminalFactory.getDefault();
          List&lt;CardTerminal&gt; terminals = factory.terminals().list();
          CardTerminal terminal = terminals.get(0);
  
          if (terminal.waitForCardPresent(5000)) {
              Card card = terminal.connect("T=0");
              System.out.println(NfcUtils.convertBinToASCII(card.getATR().getHistoricalBytes()));
          }
      }
  }
  </code></pre>
  
  <p>This code detects the exact same "phantom" tag as when using the NFC Tools library. Therefore, this issue seems unrelated to the NFC Tools library (as implied by the library developer in response to the error report). Either I'm missing something, or the issue is either related to the installed driver, the NFC reader hardware, or some unfixed bug in javax.smartcardio (listed in order of likelihood).</p>
  
  <p>I have tried uninstalling the aforementioned driver and letting Windows 7 install a suitable driver on its own (called "Microsoft Usbccid Smartcard Reader (WUDF)"), which results in the same errors as described above. I have not tried another reader, since I only have the one.</p>
  
  <p>(Note: the name of this NFC reader in the Windows device overview is "CCID USB Reader", instead of "ACS ACR122" or something related. Don't know whether this is important, just thought I would mention it.).</p>
  
  <p>Has anyone encountered this issue, and managed to resolve it? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Ok, I've tried sending a CLF command to the reader after the simulated tag has been detected; namely, getting the ATS of the connected PICC (p. 11 of the <a href="http://www.acs.com.hk/drivers/eng/API-ACR122U-2.02.pdf" rel="nofollow">ACR122U manual</a>):</p>
  
  <pre><code>TerminalFactory factory = TerminalFactory.getDefault();
  List&lt;CardTerminal&gt; terminals = factory.terminals().list();
  
  // (this is the correct terminal)
  CardTerminal terminal = terminals.get(0);
  
  if (terminal.waitForCardPresent(5000)) {
      Card card = terminal.connect("*");
  
      CardChannel channel = card.getBasicChannel();
  
  // (I tried both 0x00 and 0x01 as P1, as well as 0x05 for Le)
      CommandAPDU getAts = new CommandAPDU(0xFF, 0xCA, 0x00, 0x00, 0x04);
      ResponseAPDU response = channel.transmit(getAts);
  
      System.out.println(response.getSW1());
      System.out.println(response.getSW2());
  }
  </code></pre>
  
  <p>But I keep getting an error response code (0x63 0x00). Any ideas on what I could be doing wrong?</p>