title: Android NFC communication with Mifare DESFire EV1
body: <p>Using a Nexus 4 and the latest Android API level 18 to communicate with a Mifare DESFire EV1 AES tag is giving me a headache. Following the NXP native protocol in order to write and read this type of tag, these steps must be followed:</p>
  
  <ol>
  <li>Select application</li>
  <li>Authenticate</li>
  <li>Write or Read</li>
  </ol>
  
  <p>To do it so, I use Android's <a href="http://developer.android.com/reference/android/nfc/tech/IsoDep.html" rel="nofollow">IsoDep</a> class which provides access to <strong>ISO 14443-4</strong> properties and I/O operations. The very weird thing about it is that once I send the select application native command I get an unexpected response. Imagine I have the AID <code>F4013D</code> so I send:</p>
  
  <pre><code>-&gt; 5AF4013D
  &lt;- 6E00
  </code></pre>
  
  <p>All possible responses must be one byte length (success <code>0x00</code> or error_code) and never two or more. Thus, the <code>0x6E</code> before the success response is absolutely unexpected. It does not happen always, and when it does not and works fine, the select application and authentication processes work fine. However once authenticated the write command does not have a correct behavior, all write commands finishes with a <code>0xAF</code> from the PICC instead of a success <code>0x00</code>. It seems like the PICC expect some extra data when it should not (I send the correct length payload). If I send any other command I get a <code>0xCA</code> (Command Aborted) error code. </p>
  
  <pre><code>-&gt; 5AF4013D
  &lt;- 00 /*Success*/
  -&gt; AA01
  &lt;- AFA8394ED57A5E83106B4EE72FD2BB0CC4
  -&gt; AF148F525E1DDE0AD6AB60B4B615552475C91F2E8D89B8523E4465113DD5BD19C6 
  &lt;- 0066D255C93F2F492AFE3715C88964F1BD /*Authentication success*/
  -&gt; 3D02000000030000222222 /*Write 3 bytes to file nº2*/
  &lt;- AF /*Unexpected, 0x00 was expected*/
  </code></pre>
  
  <p>As it is normal, if I send these type of commands with a personal reader (non Android NFC) it always works fine. It seems that something in the Android NFC API is going strange, when it should just be a raw data transporter which never interprets or modifies data.</p>
  
  <p>I have also tried with <strong>ISO 7816-4</strong> APDU structure with the same result. As a curiosity, with a Galaxy Nexus does not happen the select application strange response, but yes the write command one always.</p>