Listing 1: Beispielkonfiguration der AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="de.flavor.nfc"
  android:versionCode="1"
  android:versionName="1.0">


  <application android:icon="@drawable/icon" android:label="@string/app_name">
    ...
  </application>
    
  <uses-permission android:name="android.permission.NFC" />
  <uses-sdk android:minSdkVersion="10" />
  <uses-feature android:name="android.hardware.nfc" android:required="true" />    
</manifest>


Listing 2: Prfen, ob NFC bereits aktiviert ist

public void onResume() {
  super.onResume();
  if (!NfcAdapter.getDefaultAdapter(ctx).isEnabled())
  {
    Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
    startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
  }	        
}


Listing 3: NFC Foreground Dispatch aktivieren, damit Tags auch an die eigene Anwendung geleitet werden.

NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());
PendingIntent intent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
    filter.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
    throw new RuntimeException("Unable to speciy */* Mime Type", e);
}
intentFiltersArray = new IntentFilter[] { filter };
techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
nfc.enableForegroundDispatch(activity, intent, intentFiltersArray, techListsArray);


Listing 4:NDEF-formatierte Tags erkennen und deren Informationen bestimmen

String techs[] = tag.getTechList();
boolean containsNdef = false;
for (String tech : techs) {
  if (tech.equals("android.nfc.tech.Ndef"))
    containsNdef = true;
}

if (containsNdef) {
  //b is a StringBuilder...
  Ndef ndef = Ndef.get(tag);
  b.append("NdeF Tag Tech discovered\n");
  b.append("NFC Forum Tag Type: " + ndef.getType() + "\n");
  b.append("Max size in bytes: " + ndef.getMaxSize() + "\n");
  b.append("Can tag be made read only? " + ndef.canMakeReadOnly()
			+ "\n");
  b.append("Is writable?: " + ndef.isWritable() + "\n");
}
