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.)
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nfctagscanner.mobileapps.lt.nfctagscanner" >
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NFCScanActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="nfctagscanner.mobileapps.lt.nfctagscanner" android:host="ext"
android:pathPrefix="/android.com:pkg" />
</intent-filter>
</activity>
</application>
</manifest>
And I just want to get NDEF_DISCOVERED intent. But I always get action.MAIN/category.LAUNCHER Intent with my debugger.
Any help will be appreciated. I was doing my work based on this: Android / NFC: Get Tag in onCreate() without new Intent
Many Android-powered devices that offer NFC functionality already support NFC card emulation. In most cases, the card is emulated by a separate chip in the device, called a secure element.
To read the NFC tag, the app needs to register for handling ACTION_NDEF_DISCOVERED intent. Registering this intent will let your app handle any NFC tag that is tapped to the Android device. If you wish to handle only those tags that belong to your application then you can add a filter.
If you want your app to be started by your tag and want to receive the parsed NDEF message, your tag should contain the following:
Once, you have your tag containing a proper NDEF message ready, you have to create an appropriate intent filter for the activity that should receive the tag discovery event and the NDEF message that triggered it:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
Or if you used your custom NFC Forum external type:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="vnd.android.nfc"
android:host="ext"
android:pathPrefix="/yourdomain.com:yourtypename"/>
</intent-filter>
If your app does not have an NDEF_DISCOVERED
intent filter that matches the first record of the NDEF message on your tag and your tag contains an Android Application Record, the first activity declared in your manifest that has an intent filter for android.intent.action.MAIN
and with android.intent.category.LAUNCHER
will be started. As that activity, therefore, does not declare to expect an NFC intent it will be passed the android.intent.action.MAIN
(which does not contain the NDEF message/tag handle).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With