I am working on an NFC-application. To start my app, I am using a NDEF-tag with an AAR NDEF Record inside.
This works fine. But now I want to read the tag content with the app directly. How can I do this?
(It already works, when I remove the tag from the phone and touch it again, but I want to eliminate this step.)
Update: Some more details Ok, to make it more clear, here are some parts of my current code.
private NfcAdapter nfcAdapter;
private static final int PENDING_INTENT_TECH_DISCOVERED = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
text_output = (TextView) findViewById(R.id.textView2);
NfcAdapter adapter = ((NfcManager) getSystemService(Context.NFC_SERVICE))
.getDefaultAdapter();
}
@Override
public void onResume() {
super.onResume();
nfcAdapter = ((NfcManager) this.getSystemService(Context.NFC_SERVICE))
.getDefaultAdapter();
PendingIntent pi = createPendingResult(PENDING_INTENT_TECH_DISCOVERED,
new Intent(), 0);
nfcAdapter.enableForegroundDispatch(this, pi,
new IntentFilter[] { new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED) }, new String[][] {
new String[] { "android.nfc.tech.NdefFormatable" },
new String[] { "android.nfc.tech.Ndef" } });
}
@Override
public void onPause() {
super.onPause();
nfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
switch (requestCode) {
case PENDING_INTENT_TECH_DISCOVERED:
Runnable runnable = new Runnable() {
@Override
public void run() {
doTagOperation(data);
}
};
new Thread(runnable).start();
break;
}
}
private void doTagOperation(Intent data) {
try {
String action = data.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndefTag = Ndef.get(tag);
}
} catch (Exception ex) {
...
}
}
To start the app I use an AAR-NDEF-Record on the tag, which can be created in this way: NdefRecord ndr = NdefRecord.createApplicationRecord("com.example.something");
So what I want is: Touch the tag when the app is not started makes the app starting (works already) and then it should directly read the tag without the need of moving the phone away from the tag and touch it again.