I am trying to create a sample to read and write data to NFC Tag. I am writing data to the NFC tag with an AAR and other timestamp info as follows:
Message message = new Message();
AndroidApplicationRecord aar = new AndroidApplicationRecord(AAR);
message.add(aar);
TextRecord textRecord = new TextRecord(String.valueOf(currServiceDate));
message.add(textRecord);
Now if I go out of the app and scan the NFC tag, my activity gets launched due to the AAR entry but I am not able to read the NDEF message. To read the NDEF message I have to scan the NFC tag again and only after that I am able to view the stored data on the tag.
my onResume code:
@Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
Log.e(TAG , "detect message");
}
enableForegroundMode();
}
public void enableForegroundMode() {
if (nfcAdapter != null) {
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); // filter for all
IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); // filter for all
IntentFilter[] writeTagFilters = new IntentFilter[] {tagDetected, ndefDetected};
nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, writeTagFilters, null);
} else {
Toast.makeText(this, "NFC is not supported on this device", Toast.LENGTH_LONG).show();
}
}
code inside onCreate:
public void readTagIntent() {
Intent intent = getIntent();
Log.i(TAG + " EXTRA_TAG called ", currentTagId+" "+intent.getAction());
if(intent != null) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
currentTagId = SenseMeUtils.toHexString(currentTag.getId());
Log.i(TAG + " EXTRA_TAG readTagIntent ", currentTagId+"");
}
}
}
}