Issue
Thank's to everyone who take the time to read this. Sorry for my bad english. I learn, I promise.
I'm working on an Android application which used NFC. I have a problem with empty tag. I can't catch the intent when I read an empty tag, it always launch the Android Activity to read ta tag, with the mention empty tag. For other tag with something write, I have no problem.
I search but I don't find something good. I think my problem is in the manifest.
So, I add this, of course:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
And in the activity I add, in first time
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<data android:mimeType="text/plain" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc" />
The xml/nfc.xml resource file contains the following technology filters:
<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>
</resources>
I read in some website that it may be important to add
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
So I did.
But it didn't work.
I know I probably forgot something. But what?
Solution
Ok, I solved my problem! That was not the manifest but the java code.
In first time I read a tutorial about nfc and I use some part of the code. And there was some code that I had not adapted to the advice you gave me.
This is the old part of my code
IntentFilter[] filters = new IntentFilter[1];
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (MalformedMimeTypeException e) {
Log.e("App","Wrong mime")
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
Wrong action, mime type. Many errors... I change for
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);
And it works perfectly! I hope it can help someone like you helped me by your explanations.
Thank you again for your time!
Answered By - tatianag
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.