title: Apdu error while send TgInitAsTarget command to the PN532 (ACR122u)
body: <p>I'm trying to send the TgInitAsTarget command to the PN532 in card-emulation mode. When i try to send this:</p>
  
  <pre><code>FF000000 27 D48C 05 0400 123456 20 000000000000000000000000000000000000 00000000000000000000 00 00
  </code></pre>
  
  <p>I get an apdu error:</p>
  
  <ul>
  <li>Exception in thread "main" java.lang.IllegalArgumentException: apdu must be at least 2 bytes long</li>
  <li>at javax.smartcardio.ResponseAPDU.check(Unknown Source)</li>
  <li>at javax.smartcardio.ResponseAPDU.(Unknown Source)</li>
  <li>at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)</li>
  </ul>
  
  <p>The code below is the code i use. Is this code wrong?</p>
  
  <pre><code>//TgInitAsTarget
  byte[] bytes4           = { (byte) 0xD4,(byte) 0x8C,(byte) 0x05,(byte) 0x04,(byte) 0x00,
                              (byte) 0x12,(byte) 0x34,(byte) 0x56,(byte) 0x20,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                              (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00 };
  CommandAPDU command4    = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes4);
  ResponseAPDU response4  = channel.transmit(command4);
  System.out.println(bytesToHex(response4.getBytes()));
  </code></pre>
  
  <p>It looks like the Readregister, WriteRegister and SetParams parts give a good response back following <a href="https://stackoverflow.com/a/21107403/1852589">this</a> post. The hole code:</p>
  
  <pre><code>    List&lt;CardTerminal&gt;  terminals   = null; 
      TerminalFactory factory         = TerminalFactory.getDefault();
      terminals                       = factory.terminals().list();
  
      CardTerminal terminal   = terminals.get(0);
      Card card               = terminal.connect("*");
  
      CardChannel channel     = card.getBasicChannel();
  
      //Read register
      byte[] bytes1           = {(byte) 0xD4, 0x06, 0x63, 0x05, 0x63, 0x0D, 0x63, 0x38 };
      CommandAPDU command1    = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes1);
      ResponseAPDU response1  = channel.transmit(command1);
      System.out.println(bytesToHex(response1.getBytes()));       
  
      //Update registers
      int xx = 47;    
      int yy = 00;
      int zz = 01;
      xx = xx | 0x004; 
      yy = yy &amp; 0x0EF; 
      zz = zz &amp; 0x0F7;  
  
      byte xxByte = toByte(hexStringToByteArray("0x"+Integer.toHexString(xx)));
      byte yyByte = toByte(hexStringToByteArray("0x0"+Integer.toHexString(yy)));
      byte zzByte = toByte(hexStringToByteArray("0x0"+Integer.toHexString(zz)));
  
      //WriteRegister
      byte[] bytes2           = { (byte) 0xD4, 0x08, 0x63, 0x02, (byte) 0x80, 
                                  0x63, 0x03, (byte) 0x80, (byte) 0x63, (byte) 0x05,
                                  xxByte, 0x63, 0x0D,yyByte, 0x63, 0x38, zzByte};
      CommandAPDU command2    = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes2);
      ResponseAPDU response2  = channel.transmit(command2);
      System.out.println(bytesToHex(response2.getBytes()));   
  
      //SetParameters
      byte[] bytes3           = {(byte) 0xD4,(byte) 0x12,(byte) 0x30};
      CommandAPDU command3    = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes3);
      ResponseAPDU response3  = channel.transmit(command3);
      System.out.println(bytesToHex(response3.getBytes()));
  
  
      //TgInitAsTarget
      byte[] bytes4           = { (byte) 0xD4,(byte) 0x8C,(byte) 0x05,(byte) 0x04,(byte) 0x00,
                                  (byte) 0x12,(byte) 0x34,(byte) 0x56,(byte) 0x20,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,
                                  (byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00,(byte) 0x00 };
      CommandAPDU command4    = new CommandAPDU(0xFF,0x00,0x00,0x00, bytes4);
      ResponseAPDU response4  = channel.transmit(command4);
      System.out.println(bytesToHex(response4.getBytes()));
  </code></pre>
  
  <p>Any help, response or advice is appreciated!!</p>
  
  <p>Many thanks in advance!</p>