Reputation: 161
I want to write a program that can store a unique ID and then share it as NFC tag. Totally I want write program that could use mobile instead of smart card.
I read about it in many sites and here but I don't understand how could I push an ID to my app and how to share this ID whit NFC reader device
I don't know what the code below do and what's their features
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
filters = new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) };
techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
}
public void onResume() {
super.onResume();
if (adapter != null) {
adapter.enableForegroundDispatch(this, pendingIntent, filters,
techLists);
}
}
public void onPause() {
super.onPause();
if (adapter != null) {
adapter.disableForegroundDispatch(this);
}
}
}
Upvotes: 0
Views: 1042
Reputation: 3434
I would advice to read something more about NFC in general and Android's ForegroundDispatcher
.
To make a start i will describe in baseline what this code is doing.
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
//Here you define an intent that will be raised when a tag is received.
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//These are the tag conditions to throw the intent of above.
//ACTION_TECH_DISCOVERED means the tag need to be of the type defined in the techlist.
filters = new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) };
//As the name already says, this is your techlist
techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
}
public void onResume() {
super.onResume();
if (adapter != null) {
//This enabled the foreground dispatching
//It means that when a tag is detected of the type `IsoPcdA`. The `pendingIntent` will be given to the current activity
adapter.enableForegroundDispatch(this, pendingIntent, filters,
techLists);
}
}
public void onPause() {
super.onPause();
if (adapter != null) {
//This is to disable the foreground dispatching. You don't want to send this intents to this activity when it isn't active
adapter.disableForegroundDispatch(this);
}
}
}
Since this ForegroundDispatching
throws a new intent
you need to override the onNewIntent
method.
Here you can read (and write) the tag.
@Override
public void onNewIntent(Intent intent)
{
// Get the tag from the given intent
Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
Good luck!
BTW: This isn't HCE! The code above is an example of reader/writer mode where you can read (or write) tags.
Upvotes: 1