title: Communicating to an iso 14443 card with android nfc
body: <p>I have been trying to write a basic android app that could send APDUs to an ISO 14443 card and receive response APDUs. I already have my applet in the smart card with ID F000000A. So far I have written/modified other codes but I know im doing something wrong as my app crashes on my phone as I try to launch it. Below is my code just to mention. I have my Isodep transceiver in a different class and also my isodep adapter in a different class.</p>
  
  <p><strong>IsodepAdapter</strong>:</p>
  
  <pre><code>package kitkat.com.example.kaudi;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.TextView;
  
  public class IsoDepAdapter extends BaseAdapter {
  
      private LayoutInflater layoutInflater;
      private List&lt;String&gt; messages = new ArrayList&lt;String&gt;(100);
      private int messageCounter;
  
      public IsoDepAdapter(LayoutInflater layoutInflater) {
          this.layoutInflater = layoutInflater;
      }
  
      public void addMessage(String message) {
          messageCounter++;
          messages.add("Message [" + messageCounter + "]: " + message);
          notifyDataSetChanged();
      }
  
      @Override
      public int getCount() {
          return messages == null ? 0 : messages.size();
      }
  
      @Override
      public Object getItem(int position) {
          return messages.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
          }
          TextView view = (TextView)convertView.findViewById(android.R.id.text1);
          view.setText((CharSequence)getItem(position));
          return convertView;
      }
  }
  </code></pre>
  
  <p><strong>Isodeptransceive:</strong></p>
  
  <pre><code>package kitkat.com.example.kaudi;
  
  import java.io.IOException;
  import android.nfc.tech.IsoDep;
  
  public class IsoDepTransceiver implements Runnable {
      public interface OnMessageReceived {
          void onMessage(byte[] message);
          void onError(Exception exception);
      }
  
      private IsoDep isoDep;
      private OnMessageReceived onMessageReceived;
  
      public IsoDepTransceiver(IsoDep isoDep, OnMessageReceived onMessageReceived) {
          this.isoDep = isoDep;
          this.onMessageReceived = onMessageReceived;
      }
  
      private static final byte[] CLA_INS_P1_P2 = { 0x00, (byte)0xA4, 0x04, 0x00 };
      private static final byte[] AID_ANDROID = { (byte)0xF0, 0x00, 0x00, 0x0A };
  
      private byte[] createSelectAidApdu(byte[] aid) {
          byte[] result = new byte[6 + aid.length];
          System.arraycopy(CLA_INS_P1_P2, 0, result, 0, CLA_INS_P1_P2.length);
          result[4] = (byte)aid.length;
          System.arraycopy(aid, 0, result, 5, aid.length);
          result[result.length - 1] = 0;
          return result;
      }
  
      @Override
      public void run() {
          int messageCounter = 0;
          try {
              isoDep.connect();
              byte[] response = isoDep.transceive(createSelectAidApdu(AID_ANDROID));
              while (isoDep.isConnected() &amp;&amp; !Thread.interrupted()) {
                  String message = "Message from IsoDep " + messageCounter++;
                  response = isoDep.transceive(message.getBytes());
                  onMessageReceived.onMessage(response);
              }
              isoDep.close();
          }
          catch (IOException e) {
              onMessageReceived.onError(e);
          }
      }
  }
  </code></pre>
  
  <p><strong>NfcReader:</strong></p>
  
  <pre><code>package kitkat.com.example.kaudi;
  
  import kitkat.com.example.kaudi.IsoDepTransceiver.OnMessageReceived;
  import android.app.Activity;
  import android.nfc.NfcAdapter;
  import android.nfc.NfcAdapter.ReaderCallback;
  import android.nfc.Tag;
  import android.nfc.tech.IsoDep;
  import android.nfc.tech.Ndef;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  
  public class NfcReader extends Activity implements OnMessageReceived, ReaderCallback {
      private static String TAG = NfcReader.class.getSimpleName();
      private NfcAdapter nfcAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          TextView result = (TextView) findViewById(R.id.refTextView);
  
          nfcAdapter = NfcAdapter.getDefaultAdapter(this);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          nfcAdapter.disableReaderMode(this);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A, null);
      }
  
      public void onTagDiscovered(Tag tag) {
          Log.d(TAG, "HCEfound"); 
          IsoDep isoDep = IsoDep.get(tag);
          IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
          transceiver.run();    
      }
  
  
      @Override
      public void onMessage(final byte[] message) {
          runOnUiThread(new Runnable() {
  
              @Override
              public void run() {
                  String readFromHce = new String(message);
                  TextView result = (TextView) findViewById(R.id.refTextView);
                  result.setText(readFromHce);
              }
          });
      }
  
      @Override
      public void onError(Exception exception) {
          onMessage(exception.getMessage().getBytes());
      }
  }
  </code></pre>
  
  <p><strong>Manifest:</strong>  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="kitkat.com.example.kaudi"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="19"
          android:targetSdkVersion="19" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.NFC"/&gt;
      &lt;uses-feature android:name="android.hardware.nfc" android:required="true"/&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name="kitkat.com.example.kaudi.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.nfc.action.TECH_DISCOVERED"/&gt;
                  &lt;action android:name="android.nfc.action.NDEF_DISCOVERED"/&gt;
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                  &lt;data android:mimeType="text/plain" /&gt;
               &lt;/intent-filter&gt;
               &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                          android:resource="@xml/filter_nfc"/&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>LogCat (error)</strong></p>
  
  <pre><code>05-03 19:54:58.713: E/AndroidRuntime(5247): FATAL EXCEPTION: main
  05-03 19:54:58.713: E/AndroidRuntime(5247): Process: kitkat.com.example.kaudi, PID: 5247
  05-03 19:54:58.713: E/AndroidRuntime(5247): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{kitkat.com.example.kaudi/kitkat.com.example.kaudi.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "kitkat.com.example.kaudi.MainActivity" on path: DexPathList[[zip file "/data/app/kitkat.com.example.kaudi-2.apk"],nativeLibraryDirectories=[/data/app-lib/kitkat.com.example.kaudi-2, /vendor/lib, /system/lib]]
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.os.Handler.dispatchMessage(Handler.java:102)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.os.Looper.loop(Looper.java:136)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.reflect.Method.invoke(Method.java:515)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at dalvik.system.NativeStart.main(Native Method)
  05-03 19:54:58.713: E/AndroidRuntime(5247): Caused by: java.lang.ClassNotFoundException: Didn't find class "kitkat.com.example.kaudi.MainActivity" on path: DexPathList[[zip file "/data/app/kitkat.com.example.kaudi-2.apk"],nativeLibraryDirectories=[/data/app-lib/kitkat.com.example.kaudi-2, /vendor/lib, /system/lib]]
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
  05-03 19:54:58.713: E/AndroidRuntime(5247):     ... 11 more
  </code></pre>