I used this code for integrating NFC
reading in my android
application. Write a plain text to the NFC
tag and read using app it is perfectly working. Now my requirement is to read URL
from the NFC
tag.When reading value from NFC
tag it automatically open the browser and loading the URL
.So what changes needed to achieve the reading content and open my app?
Asked
Active
Viewed 2,171 times
2

Renjith Krishnan
- 2,446
- 5
- 29
- 53
3 Answers
1
Add to your Manifest
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data
android:host="your host name"
android:scheme="http" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
in the activity you want to open

user3458049
- 70
- 6
-
can you please clarify me how to add the hostname? – Rosemary Feb 01 '19 at 06:25
0
Here I assume that results returned will be only url and no other data with it, so just modify onPostExecute
as :
@Override
protected void onPostExecute(String result) {
if (result != null) {
String url = result;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
If other data also included than parse results to get only URL.

Satty
- 1,352
- 1
- 12
- 16
0
You can use filter if you want to start the app when get closer to NFC tag, but be aware if your app is running it won't be notified about the tag. You have to register it inside your app:
protected void onCreate(Bundle savedInstanceState) {
...
Intent nfcIntent = new Intent(this, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
nfcPendingIntent =
PendingIntent.getActivity(this, 0, nfcIntent, 0);
IntentFilter tagIntentFilter =
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
tagIntentFilter.addDataType("text/plain");
intentFiltersArray = new IntentFilter[]{tagIntentFilter};
}
catch (Throwable t) {
t.printStackTrace();
}
}
and remember to enable it in onResume:
nfcAdpt.enableForegroundDispatch(
this,
nfcPendingIntent,
intentFiltersArray,
null);
handleIntent(getIntent());
and unregister it in onPause:
nfcAdpt.disableForegroundDispatch(this);
..be aware that the data can be stored in SmartPoster structure in your NFC tag. In this case you have to read it in another way. In my blog you can find a post about reading SmartPoster and more

FrancescoAzzola
- 2,666
- 2
- 15
- 22