Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intent.getAction() is null inside onNewIntent() during NFC tag scan

Tags:

android

nfc

This is my first time working with NFC Tags. I have declared the NFC scan activity like this in Manifest:

    <activity
        android:name=".main.NFCScanActivity"
        android:theme="@android:style/Theme.Holo.Light"
        android:launchMode="singleTop"
        android:exported="true">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc_tech_filter" />
    </activity>

Here is the nfc_tech_filter.xml file:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
    <tech>android.nfc.tech.IsoDep</tech>
    <tech>android.nfc.tech.NfcA</tech>
    <tech>android.nfc.tech.NfcB</tech>
    <tech>android.nfc.tech.NfcF</tech>
    <tech>android.nfc.tech.NfcV</tech>
    <tech>android.nfc.tech.Ndef</tech>
    <tech>android.nfc.tech.NdefFormatable</tech>
    <tech>android.nfc.tech.MifareClassic</tech>
    <tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>

Whenever I scan my tag, onNewIntent() is call but when I try to call intent.getAction() inside onNewIntent, the value is always null.

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}
private void handleIntent(Intent intent) {
    String action = intent.getAction(); // action here is null
}
like image 527
Bajigar Avatar asked Oct 24 '25 10:10

Bajigar


2 Answers

Make sure the pending intent flags are set to mutable for nfc.

PendingIntent.getActivity(
   activity, 
   KEY_PENDING_INTENT_REQUEST_CODE,  
   Intent(
      activity,
      activity.javaClass)
        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 
   PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
    
like image 67
adil abuwani Avatar answered Oct 25 '25 23:10

adil abuwani


It is more normal to use one of the 2 Foreground NFC API's when you want to handle NFC's when your App is in the Foreground. Usually the Manifest NFC entries are only used to start your App by NFC trigger.

enableReaderMode is the better and newer of the 2 Foreground NFC Api's or there is the older enableForegroundDispatch

An example of using enableReaderMode

If using manifest entries for NFC you should really process the return of getIntent of your Activity in onCreate to see if that is an Intent created from an NFC trigger.

like image 22
Andrew Avatar answered Oct 25 '25 23:10

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!