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?

有帮助吗?

解决方案 2

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!

其他提示

If you want your activity to launch (or the activity chooser to be shown) for tags that do not contain an NDEF message (or that do contain an NDEF message that is not filterable on Android), the android.nfc.action.TECH_DISCOVERED intent is the way to go.

The android.nfc.action.TAG_DISCOVERED intent filter is meant as a fall-back only. Thus it will only trigger if no other activity (not only your app) is registered for an intent filter that matches the tag.

The android.nfc.action.NDEF_DISCOVERED intent filter will only trigger if the first NDEF record on the tag matches a given MIME type or if that record matches a given URI (including URI mapping for NFC Forum external types).

The android.nfc.action.TECH_DISCOVERED intent filter should be used in the form:

<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_filter" />

No <data ... /> or <category ... /> tags should be added.

The resource file containing the tag list (xml/filter_nfc.xml) must contain a vaild technology filter that matches your tag. Each <tech>...</tech> entry specifies a tag technology that your intent filter will trigger upon. Multiple tag technology entries can be grouped to form either a logical AND or a logical OR. All <tech> entries within one <tech-list>...</tech-list> group are combined with logical AND. Multiple <tech-list>...</tech-list> groups within the XML file are combined with logical OR. So if you want to trigger upon any NFC tag technology, your XML file would look like this:

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

Your XML file, on the other hand, would require a tag to have all tag technologies at the same time, which is impossible as some technologies are mutually exclusive to each other (e.g. a tag can't be detected as NfcA and NfcB at the same time).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top