Android NFC Development Instance

Source: Internet
Author: User

Beam examples provided in Android 4.0 SDK are indeed a good template for NFC development. For the ndef message processing process that understands NFC, see the following code.

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. getdefaadapter adapter (this); // instantiate an NFC Device
If (mnfcadapter = NULL ){
Minfotext = (textview) findviewbyid (R. Id. textview );
Minfotext. settext ("NFC is not available on this device .");
}

Mnfcadapter. setndefpushmessagecallback (this, this); // registers the ndef callback message
Mnfcadapter. setonndefpushcompletecallback (this, this );
}

@ Override
Public ndefmessage createndefmessage (nfcevent event ){
Time time = new time ();
Time. settonow ();
String text = ("Beam me up! \ N "+
"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 activity when this
// Callback occurs, because it happens from a binder thread
Mhandler. obtainmessage (message_sent). sendtotarget ();
}

Private Final handler mhandler = new handler (){
@ Override
Public void handlemessage (Message 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 ){
// Onresume gets called after this to handle the intent
Setintent (intent );
}

/**
* Parses the ndef message from the intent and prints to the textview
*/
Void processintent (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 an 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 ){
// If NFC is not available, we won't be 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 );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.