Android near Field Communication---Advanced NFC (ii)

Source: Internet
Author: User
Tags closing tag

Read and Write NFC tags
To read and write NFC tags, it involves getting tags from the intent object and opening the communication with the label. To read and write NFC tag data, you have to define your own protocol stack. However, keep in mind that you can still read and write ndef data when working directly with the NFC tag. This is the thing you want to build. The following example demonstrates how to use the Mifare ultrathin label to work:
Package COM.EXAMPLE.ANDROID.NFC;
Import Android.nfc.Tag;
Import Android.nfc.tech.MifareUltralight;
Import Android.util.Log;
Import java.io.IOException;
Import Java.nio.charset.Charset;
publicclassmifareultralighttagtester{
Privatestaticfinalstring TAG =mifareultralighttagtester.class.getsimplename ();
Publicvoid Writetag (Tag tag,string tagtext) {
Mifareultralight Ultralight =mifareultralight.get (tag);
try{
Ultralight.connect ();
Ultralight.writepage (4, "ABCD". GetBytes (Charset.forname ("Us-ascii"));
Ultralight.writepage (5, "Efgh". GetBytes (Charset.forname ("Us-ascii"));
Ultralight.writepage (6, "IJKL". GetBytes (Charset.forname ("Us-ascii"));
Ultralight.writepage (7, "Mnop". GetBytes (Charset.forname ("Us-ascii"));
}catch (IOException e) {
LOG.E (TAG, "ioexception while closing mifareultralight ...", e);
}finally{
try{
Ultralight.close ();
}catch (IOException e) {
LOG.E (TAG, "ioexception while closing mifareultralight ...", e);
}
}
}
Publicstring Readtag (tag tag) {
Mifareultralight mifare =mifareultralight.get (tag);
try{
Mifare.connect ();
byte[] Payload = mifare.readpages (4);
Returnnewstring (Payload,charset.forname ("Us-ascii"));
}catch (IOException e) {
LOG.E (TAG, "ioexception while writing Mifareultralight
Message ... ", e);
}finally{
if (Mifare!=null) {
try{
Mifare.close ();
}
catch (IOException e) {
LOG.E (Tag, "Error closing tag ...", e);
}
}
}
Returnnull;
}
}
Using the foreground dispatch system
The foreground dispatch system allows an activity to intercept the intent object and declares that the activity has a higher priority than other activity that handles the same intent object. Using this system involves building some data structures for the Android system so that the appropriate intent objects can be sent to your application, and the following are the steps to enable the foreground dispatch system:
1. Add the following code to your activity's OnCreate () method:
     A. Create a Pendingintent object so that the Android system can use it to encapsulate the details of the NFC tag when scanning to an NFC tag.
          Pendingintent pendingintent =pendingintent.getactivity (
      this,0,newintent (this, getclass ()). Addflags (Intent.flag_activity_single_top), 0);
     B. Declare the intent filter that you want to intercept the processed intent object. The foreground dispatch system checks the received intent object with the declared intent filter when the device scans the NFC tag. If the match will let your application handle this intent object, if it does not match, the foreground dispatch system will fall back to the intent dispatch system. If you specify null for an array of intent filters and technical filters, then you want to filter all the labels of intent objects that are returned to the tag_discovered type. The following code will be used to process all MIME types of the ndef_discovered. This is done only when needed:
Intentfilter ndef =newintentfilter (nfcadapter.action_ndef_discovered);
try{
Ndef.adddatatype ("*/*"); /* Handles all MIME based dispatches.
You should specify only the ones so you need. */
}
catch (Malformedmimetypeexception e) {
Thrownewruntimeexception ("fail", e);
}
Intentfiltersarray =newintentfilter[]{ndef,};
     C. Create an array of NFC tag technologies that the application wants to process. Call the Object.class.getName () method to get the class of the technology you want to support:

Techlistsarray = new string[][] {new string[] {NfcF.class.getName ()}};

2. Override the following activity lifecycle callback methods, and add logic to enable and disable foreground scheduling when activity hangs (OnPause ()) and gets Focus (Onresume ()). The Enableforegrounddispatch () method must be called in the main thread and only when the activity is in the foreground (ensure that the method is called in the Onresume () method). You also need to implement the Onnewintent callback method to handle the data of the scanned NFC tag:
Publicvoid OnPause () {
Super.onpause ();
Madapter.disableforegrounddispatch (this);
}
Publicvoid Onresume () {
Super.onresume ();
Madapter.enableforegrounddispatch (This, pendingintent, Intentfiltersarray, Techlistsarray);
}
Publicvoid onnewintent (Intent Intent) {
Tag tagfromintent = Intent.getparcelableextra (Nfcadapter.extra_tag);
Do something with Tagfromintent
}

Android near Field Communication---Advanced NFC (ii)

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.