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):
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.