I have an NFC card with Smart Poster and AAR writen on it
NdefMessage message = new NdefMessage(smartPosterRecord, aarRecord);
When I attach this card to the device my demo application defined in AAR starts, but intent.getAction()
returns android.intent.action.MAIN
instead of NfcAdapter.ACTION_TAG_DISCOVERED
or NfcAdapter.ACTION_TECH_DISCOVERED
so I can't get Tag from it.
How should it be done so that I can read URL from the smart poster?
Ndef:
NdefRecord uriRecord = NdefRecord.createUri(uri);
NdefMessage message = new NdefMessage(uriRecord);
NdefRecord smartPosterRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_SMART_POSTER, new byte[0], message.toByteArray());
NdefRecord aarRecord = NdefRecord.createApplicationRecord(aar);
message = new NdefMessage(smartPosterRecord, aarRecord);
// Get instance of Ndef for the given tag
Ndef ndef = Ndef.get(tag);
// Enable I/O
ndef.connect();
// Write the message
ndef.writeNdefMessage(message);
// Close the connection
ndef.close();
Handling of intent:
public void handleIntent(Intent intent) {
if (((NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()))) || (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))) {
// do something
}
Manifest:
<activity
android:name="com.demo.app.DemoActivity"
android:label="GeneralDemo"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfcfilter" />
</activity>
Adding this into manifest did the trick:
The app is not starting because of the AAR however but as a result of "catching" the url of smart poster...
EDIT: In the end we decided to use custom MimeType that we are catching in our app (via intent-filter)