The beam example provided in the Android 4.0 SDK is indeed a good template for NFC development. Consider the following code for NDEF message processing that understands NFC.
public class Beam extends activity implements Createndefmessagecallback,
Onndefpushcompletecallback {
Nfcadapter Mnfcadapter;
TextView Minfotext;
private static final int message_sent = 1;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Minfotext = (TextView) Findviewbyid (R.id.textview);
Mnfcadapter = Nfcadapter.getdefaultadapter (this); Instantiating NFC devices
if (Mnfcadapter = = null) {
Minfotext = (TextView) Findviewbyid (R.id.textview);
Minfotext.settext ("NFC isn't available on this device.");
}
Mnfcadapter.setndefpushmessagecallback (this, this); Registering NDEF callback messages
Mnfcadapter.setonndefpushcompletecallback (this, this);
}
@Override
Public ndefmessage Createndefmessage (Nfcevent event) {
Time is = new Time ();
Time.settonow ();
String Text = ("Beam Me Up!nn" +
"Beam Time:" + Time.format ("%h:%m:%s"));
Ndefmessage msg = new Ndefmessage (
New ndefrecord[] {Createmimerecord (
"Application/com.example.android.beam", Text.getbytes ())
});
return msg;
}
@Override
public void Onndefpushcomplete (Nfcevent arg0) {
A handler is needed to send messages to the
Callback occurs, because it happens from a binder thread
Mhandler.obtainmessage (message_sent). Sendtotarget ();
}
Private final Handler Mhandler = new Handler () {
@Override
public void Handlemessage (msg) {
Switch (msg.what) {
Case Message_sent:
Toast.maketext (Getapplicationcontext (), "Message sent!", Toast.length_long). Show ();
Break
}
}
};
@Override
public void Onresume () {
Super.onresume ();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals (Getintent (). Getaction ())) {
Processintent (Getintent ());
}
}
@Override
public void Onnewintent (Intent Intent) {
Onresume gets called to handle the intent
Setintent (Intent);
}
/**
* Parses the NDEF message from the intent and prints to the TextView
*/
void Processintent (Intent Intent) {
parcelable[] Rawmsgs = Intent.getparcelablearrayextra (
Nfcadapter.extra_ndef_messages);
Only one message sent during the beam
Ndefmessage msg = (ndefmessage) rawmsgs[0];
Record 0 contains the MIME type, record 1 is the AAR, if present
Minfotext.settext (New String (Msg.getrecords () [0].getpayload ()));
}
/**
* Creates a custom MIME type encapsulated in the Ndef record
*
* @param mimetype
*/
Public Ndefrecord Createmimerecord (String mimetype, byte[] payload) {
byte[] mimebytes = mimetype.getbytes (Charset.forname ("Us-ascii"));
Ndefrecord Mimerecord = new Ndefrecord (
Ndefrecord.tnf_mime_media, Mimebytes, new byte[0], payload);
return Mimerecord;
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
If NFC is not available, we won ' t being needing this menu
if (Mnfcadapter = = null) {
return Super.oncreateoptionsmenu (menu);
}
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.options, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
Switch (Item.getitemid ()) {
Case R.id.menu_settings:
Intent Intent = new Intent (settings.action_nfcsharing_settings);
StartActivity (Intent);
return true;
Default
return super.onoptionsitemselected (item);
}
}
}