3

I created an app that reads the ID of an NFC card. How I want to emulate that card with my Android device, so that an NFC reader can read it as if it was that previously read NFC card. So eventually, I want to replace the NFC card with the Android device.

This is my code for reading the ID from the card:

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

    TextView txt;
    NfcAdapter nfcAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt = (TextView) findViewById(R.id.textView);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Toast.makeText(this,"Hello, I'm NFCTESTER", Toast.LENGTH_LONG).show();
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        txt.setText(tag.getId().toString());
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        IntentFilter[] intentFilter = new IntentFilter[]{};

        nfcAdapter.enableForegroundDispatch(this,pendingIntent,intentFilter,null);
    }

    @Override
    protected void onPause() {
        super.onPause();

        nfcAdapter.disableForegroundDispatch(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The card (school card) is MIFARE Classic and contains an 8 digit hex code (we got this information from IT Manager of school):

  • Tag ID (hex): d3 72 f5 24
  • Tag ID (dec): 3547526436
  • Tag ID (reserved): 620065491
  • Tag ID (string): 24:f5:72:d3
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
miragessee
  • 313
  • 1
  • 4
  • 14
  • What did you expect? The above code listens for NFC **tags** and displays their ID. What makes you think that this code would be usable to communicate with a card **reader**? – Michael Roland Nov 29 '15 at 15:20
  • I want to send the card tag to the reader on the phone? So read the phone without school card. My expectations in my code. "Hello, I'm the NFC TEST" 's work. Then onNewIntent into, writeNdefMessage... This is my final project. Thank you for your help. Briefly, application "Hello" does not even show. – miragessee Nov 29 '15 at 20:44
  • It's not very clear. Do you want to replace NFC school card by an emulated card on a NFC phone ? – LaurentY Nov 30 '15 at 13:21
  • That's exactly right. Sorry for my bad english. How I can use my phone same as nfc tag? – miragessee Nov 30 '15 at 20:07
  • Do you have access to specification of school cards ? it's necessary to reproduce functionalities on emulated card on phone. – LaurentY Dec 01 '15 at 08:41
  • Tag ID (hex): d3 72 f5 24 Tag ID (dec): 3547526436 ID (reserved): 620065491 Tag ID (string): 24:f5:72:d3 Technologies: NfcA Yes we know. School card specifications are mifare classic and 8 digit hex code. We got this information from IT Manager of school. – miragessee Dec 01 '15 at 10:12

1 Answers1

3

If you can emulate such a card with your Android phone depends on what information is contained in the card/used by the NFC reader:

  • The reader uses only the anti-collision identifier (UID). You can read that ID with Tag.getId() and that's what your app currently does. The Android NFC/HCE API does not allow you to set an arbitrary anti-collision identifer for the phone. Thus, you cannot emulate the ID with your phone. However, the NFC hardware in some Android devices would allow you to modify the ID with a customized ROM (or a modification of a configuration file on a rooted device). See Editing Functionality of Host Card Emulation in Android and Host-based Card Emulation with Fixed Card ID.

  • The reader reads and/or writes to sectors of the MIFARE Classic card. Android HCE does not support emulation of MIFARE Classic. Thus, you cannot emulate such a card with your phone.

  • The reader reads an NDEF message from the card and supports just any NFC tag containing a properly formatted NDEF message. (Note that this is very unlikely!) You could read the NDEF message with Android and use Android HCE to emulate an NFC Forum type 4 tag containing that NDEF message.

Community
  • 1
  • 1
Michael Roland
  • 39,663
  • 10
  • 99
  • 206