I wrote a nfc text tag like this
myscheme://company?page=2&poiId=140
Then i've created an Intent-filter like this in my manifest file in order to open my app (and then call a WS with informations from tag)
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" android:scheme="myscheme" android:host="company"/>
</intent-filter>
If i remove scheme
and host
params from data tag so everything works but by adding filtering it does not work anymore.
Why ?
Is there a solution ?
As corvairjo correctly wrote, "you are mixing the record type MIME type (here text/plain) with the record type URI into one intent filter. You should use only one."
The point is that an NDEF record consists of a type information and a data payload (actually there's more than just those two fields, but those two should be enough to understand the concept behind it):
+------+---------+
| TYPE | PAYLOAD |
+------+---------+
If you create a text record containing your URI, you will get something like this:
+------------+-------------------------------------+
| text/plain | myscheme://company?page=2&poiId=140 |
+------------+-------------------------------------+
The receiving device will interpret the record according to its type field. Therefore, it will treat the payload ("myscheme://company?page=2&poiId=140") as a piece of human-readable text -- and not as a URI!
On Android, this means that the record is detected as a piece of human-readable information with the MIME media type "text/plain". Hence, you can only catch it with an intent filter like this:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
As the data payload is human-readable text, the intent filter cannot filter on any specific parts of that text (intent filters on Android can only match type information and URIs).
So if you want to have an intent filter that matches on the URI, you have to wrap the URI into a record type that specifically identifies the data payload as URI. This is typically done using the NFC Forum URI record type:
+---------------+-------------------------------------+
| urn:nfc:wkt:U | myscheme://company?page=2&poiId=140 |
+---------------+-------------------------------------+
Tag writer apps typically offer an option to write URIs/URLs onto tags. The Android NFC API provides the method NdefRecord.createUri()
to create a URI record.
You can then use an intent filter like this to trigger upon a tag that contains the record:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="myscheme" android:host="company" />
</intent-filter>
You are mixing the record type MIME type (here text/plain) with the record type URI into one intent filter. You should use only one.
You can drop the MIME type definition and just go with the URI definition:
<data android:scheme="myscheme" android:host="company"/>