Android NFC development practices

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! 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 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 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 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 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.