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 :
<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>
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 );
}
} );
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*/
}
You haven't described how you want your application to handle working with NFC cards, as there are several possible scenarios:
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).
_
@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
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