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