title: How to communicate with HostApduService from an Activity
body: <blockquote>
    <p>I have asked this question <a href="https://stackoverflow.com/questions/24647941/how-to-communicate-with-a-service-from-activity-launched-by-this-service-itself">here</a> but it was marked as duplicate -
    however I didn't find any solution helpful mentioned in comments.
    Here, I am asking again with more details ...</p>
  </blockquote>
  
  <p>I am doing a sample app (PoC) on <a href="https://developer.android.com/guide/topics/connectivity/nfc/hce.html" rel="nofollow noreferrer">HCE</a> and using <a href="https://developer.android.com/reference/android/nfc/cardemulation/HostApduService.html" rel="nofollow noreferrer">HostApduService</a> as per Android user guide. I have created two apps<br>
  1) ReaderApp - acting as card reader
  2) HCEApp - emulating a card</p>
  
  <p>In HCEApp, I have created a class 'MyService' extending HostApduService</p>
  
  <pre><code>public class MyService extends HostApduService {
  
  private int messageCounter;
  private final String TAG = "MyService";
  
  Intent mIntent;
  
  @Override
  public void onCreate() {
      super.onCreate();
      Log.i(TAG, "onCreate");
  
      mIntent = new Intent(this, MyActivity.class);
      mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(mIntent);
  }
  
  /**
   * returned bytes will be sent as response. This method runs in Main thread
   * so return ASAP.
   */
  
  @Override
  public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
      if (selectAidApdu(apdu)) {
          Log.i(TAG, "Application selected");
          return getWelcomeMessage();
      } else {
          Log.i(TAG, "Received: " + new String(apdu));
          return getNextMessage();
      }
  }
  
  private byte[] getWelcomeMessage() {
      return "Hello Desktop!".getBytes();
  }
  
  private byte[] getNextMessage() {
      return ("Message from android: " + messageCounter++).getBytes();
  }
  
  private boolean selectAidApdu(byte[] apdu) {
  
      if (apdu != null) {
          for (byte b : apdu) {
              System.out.printf("0x%02X", b);
          }
      }
  
      return apdu.length &gt;= 2 &amp;&amp; apdu[0] == (byte) 0
              &amp;&amp; apdu[1] == (byte) 0xa4;
  }
  
  @Override
  public void onDeactivated(int reason) {
      Log.i(TAG, "Deactivated: " + reason);
  }
  
  @Override
  public boolean onUnbind(Intent intent) {
      return super.onUnbind(intent);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>As you can see in <strong>onCreate()</strong>, I am launching <strong>MyActivity</strong> provides user to enter some information and needs to be sent back to <strong>MyService</strong>. </p>
  
  <p>I think I can not use binding as '<strong>onBind()</strong>' is declared final in HostApduService as below</p>
  
  <pre><code>@Override
  public final IBinder onBind(Intent intent) {
      return mMessenger.getBinder();
  }
  </code></pre>
  
  <p>Please let me know if I am understading it correctly. Appreciate any help.</p>
  
  <p>Thanks<br>
  iuq</p>