Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getParcelableExtra return null NFC Tag

Tags:

java

android

nfc

I know this is a redundant question on StackOverflow but I tried a lot of the answers I've found and none of them worked. I'd like to send a message when I press a button but when I call my nfc writing function in a Fragment, but I've put the NDEF_Discovered in the AndroidManifest.xml. The intent.getParcelableExtra( NfcAdapter.EXTRA_TAG ) returns null and I can't find out why. Here's my code :

AndroidManifest.xml

<activity
    android:name="com.iiil.cccdm.ebar.gui.activity.MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

    </intent-filter>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <action android:name="android.nfc.action.TAG_DISCOVERED"/>

        <category android:name="android.intent.category.DEFAULT"/>

        <data android:mimeType="text/*"/>
    </intent-filter>

</activity>

ConsommerFragment (in onCreateView())

View rootView = inflater.inflate( R.layout.consommer_fragment, container, false );
ImageButton btn = (ImageButton) rootView.findViewById( R.id.drinkButton );
btn.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick( View v ) {
        Toast.makeText( getActivity(), "in listener", Toast.LENGTH_LONG );
                // Recupération de l'id
                UserData userData = new UserData( getActivity() );
                Integer idUser = userData.getId();

                Intent intent = this.getActivity().getIntent();
                if ( intent == null ) {
                    Log.e( this.getClass().getSimpleName(), "INTENT NULL" );
                }

                NFCTransfer nfcTransfer = NFCTransfer.getInstance( getActivity() );
                // TODO : Test avec Yohan et améliorer.
                nfcTransfer.writeToDevice( idUser.toString(), intent );
    }
} );

NFCTransfer

public void writeToDevice( String msgToWrite, Intent intent ) {
    Tag tag = intent.getParcelableExtra( NfcAdapter.EXTRA_TAG );
    if ( tag == null ) {
        Log.e(this.getClass().getSimpleName(), "tag == null");
        Toast.makeText( context, "No Tag. Waiting...", Toast.LENGTH_LONG )
             .show();
        return;
    }

    Ndef ndefref = Ndef.get( tag );
    /* ... the rest of the function is useless since tag is null*/
}
like image 283
Carvallegro Avatar asked Aug 14 '26 01:08

Carvallegro


1 Answers

You haven't described how you want your application to handle working with NFC cards, as there are several possible scenarios:

  1. When the application is not started and you swipe a NFC card, your application starts. If this is what you want, you will receive the proper intent in your activity's onCreate() method.

From the info you've provided it looks like you should probably also listen for a TECH_DISCOVERED intent and add a tech list meta-data block to your AndroidManifest.xml, like this:

<intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
        android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />

Also, see this for a sample tech list (i.e. what types of NFC tech should your app handle).

  1. Your app is already running in the foreground when you scan a NFC tag. In this case you should override the onNewIntent() method in your activity in order to receive the proper intent, and then handle your operations there.

_

@Override
protected void onNewIntent(Intent intent) {
    Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if (nfcTag == null) {
        Log.w(LOG_TAG, "Unable to obtain NFC tag from intent!");
    } else {
        String tagId = bytesToHex(nfcTag.getId());
        ...
    }

Also, note that you can use the foreground dispatch system in order to register/unregister listening for NFC intents directly from your activity code, instead of setting up the configuration in your AndroidManifest.xml

like image 193
Andrei Anischevici Avatar answered Aug 15 '26 14:08

Andrei Anischevici



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!