title: How simulate NFC tag (not card) with smartphone on NFC antenna
body: <p>I use a small NFC antenna, connected on PC with USB interface, and delivering read-data on standard output (like keyboard). So I put a small sticker-tag (MIFARE Ultralight) and I receive tag id on console.<br>
  I wish to do the same thing with my smartphone (Samsung S4). For tag emulation, not for card emulation.<br>
  How to do that (java, Android Studio) ? I found sample for card emulation, but not for tag.<br>
  Do I need first to dump the tag content (NdefMessage and NdefRecord) in order to send back the same content ?<br>
  Which java sample is dedicated to this specific behavior ?</p>
  
  <p>I have found a small sample <a href="http://www.frugalprototype.com/android-nfc-hce/#comment-22" rel="nofollow">http://www.frugalprototype.com/android-nfc-hce/#comment-22</a> which seems to work... but not by me !<br>
  My NFC antenna triggers "beep", and display random id value on console, but I never enters the <code>processCommandApdu()</code> method. Where is the mistake ?</p>
  
  <p>MainActivity.java:  </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.d("MainActivity","&gt;&gt;&gt; onCreate...");
          setContentView(R.layout.activity_main);
      }
  }
  </code></pre>
  
  <p>HceActivity.java:  </p>
  
  <pre><code>public class HceService extends HostApduService {
      /*  Il s’agit en réalité ici de la commande SELECT AID + la taille en octet de l’AID + l’AID.
      Dans une application en production, il est préférable de déclarer la commande
      SELECT AID = {0x00, (byte) 0xA4, 0x04,0x00}
      et de déclarer l’AID séparément : AID = {0xF0, 0x46, 0x52, 0x55, 0x47, 0x41, 0x4c}.
      Sans oublier d’ajouter la taille de l’AID à la commande SELECT AID*/
      private static final byte[] SELECT_AID = {0x00,
              (byte) 0xA4, 0x04,0x00,0x07,
              (byte) 0xF0, 0x46, 0x52, 0x55, 0x47, 0x41, 0x4C};
      private static final byte[] MY_UID = {0x01, 0x02, 0x03, 0x04, (byte) 0x90, 0x00};
  
      private static final byte[] MY_ERROR = {0x6F, 0x00};
  
      @Override
      public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
  
          Log.d("HceService","&gt;&gt;&gt; processCommandApdu...");
          if (Arrays.equals(SELECT_AID, apdu)) {
              return MY_UID;
          } else return MY_ERROR;
      }
  </code></pre>
  
  <p>AndroidManifest.xml:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.pdf.cardemulation"&gt;
  
      &lt;!-- Demander la permission la permission à l'utilisateur pour l'utilisation du NFC --&gt;
      &lt;uses-feature
          android:name="android.hardware.nfc.hce"
          android:required="true" /&gt;
  
      &lt;uses-permission
          android:name="android.permission.NFC" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&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;/activity&gt;
  
          &lt;!-- Déclaration de notre service HceService --&gt;
          &lt;service
              android:name=".HceService"
              android:exported="true"
              android:permission="android.permission.BIND_NFC_SERVICE" &gt;
              &lt;intent-filter&gt;
                  &lt;action
                      android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" /&gt;
              &lt;/intent-filter&gt;
              &lt;!-- Fichier dans lequel est déclaré l’AID. Nous devons créer un dossier xml dans le dossier values
              et créer un fichier apduservice.xml dans lequel nous allons déclarer l’AID de notre application   --&gt;
              &lt;meta-data
                  android:name="android.nfc.cardemulation.host_apdu_service"
                  android:resource="@xml/apduservice" /&gt;
          &lt;/service&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>and finally:  </p>
  
  <pre><code>&lt;host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
                     android:description="@string/app_name"
                     android:requireDeviceUnlock="false"&gt;
      &lt;aid-group
          android:description="@string/membership_number"
          android:category="other"&gt;
          &lt;aid-filter android:name="F046525547414c"/&gt;
      &lt;/aid-group&gt;
  &lt;/host-apdu-service&gt;
  </code></pre>