Android NFC Tutorial – Part 1
This post assumes basic familiarity with Java and the Android SDK. It gives you a brief intro on how to talk to the NFC API to read a tag and subscribe to an event that detects it.
Setting up the Activity
[java]
public class Main extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_tag);
}
@Override
protected void onResume() {
//Process the intent that started this activity. It should be a Tag Discovery related intent
this.parseIntent(this.getIntent());
}
[/java]
Processing the NFC Tag event
[java]
boolean parseIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
boolean success = FlomioNdefHelper.writeUrlToTag(url, tag);
} else {
return false;
}
}
[/java]
AndroidManifest.xml
[xml] <uses-permission android:name="android.permission.NFC" /> [/xml] [xml] <activity android:name=".ReadNfcTag" > <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:host="flomio.com" android:scheme="http" /></pre> </intent-filter> </activity> [/xml]




Leave a Reply
Want to join the discussion?Feel free to contribute!