Preparation of the Android NFC near-field communication----Read-write card

Source: Internet
Author: User
Tags nfca

Preparation of the Android NFC near-field communication----Read-write card

because the company picked up a project that sounded more like NB. To a system for the field of labor. And by the small woman responsible for the development of the Androidclient ~ ~ When the Android device development when the look is relatively tall ha, explosion-proof, in fact, many times colleagues are trying to joke that try to fall on the ground, are explosion-proof. That's definitely not going to be a bad one. NFC technology is needed in the development process. So just learn and organize your Android NFC-related knowledge online-well, cut into the topic. This paper prepares the card before reading and writing:

PS: This series has some of the results of online finishing. Thanks for thanking ~ ~ Of course also includes small woman part code ~ ~ ~

NFC is a set of short-distance wireless communications, usually 4 centimeters or less.

The NFC operating frequency is 13.56M Hz and the transfer rate is 106kbit/s to 848kbit/s. NFC always occurs between an initiator and a passive target. The initiator emits near-field radio waves, which can power the passive target. These passive targets include labels that do not require a power supply. Card, can also be a power-supply device.

Compared to other wireless communication technologies, such as Bluetooth and WiFi. NFC offers lower loan and distance, low cost, no power supply, no matching, and the entire communication process can be completed in just one second.

An Android device with NFC support is typically an initiator.

can also be used as an NFC reading and writing device.

He will detect NFC tags and open an activity to deal with them. Android 2.3.3 also has limited peer support.

Tags are very diverse, the simple only to provide read and write segments, and some can only read. Complex tags can support a number of operations. Encryption to control the reading and writing of data segments in tags. Even some tags have simple operating systems that agree with some complex interactions and are able to run some code.

The code sample for this article is based on API10.

To use NFC in your Android phone. Must be configured in Androidmanifest.xml such as the following:

<uses-featureAndroid:name= "ANDROID.HARDWARE.NFC"android:required= "true" /><uses-permissionAndroid:name= "Android.permission.NFC" />

Tag Announcement System

When Android devices scan to an NFC tag, the general behavior is to proactively find the most appropriate activity to handle the tag intent without the user having to choose which activity to handle.

Since the device scans NFC tags in a very short range and time, assuming that the user chooses, it is possible to have a mobile device, which will interrupt the scanning process. You should develop the activity that you only deal with tags that need to be handled to prevent the user from choosing which activity to use to handle the situation. Android provides two systems to help you correctly identify whether an NFC tag is what your activity wants to handle: The intent announcement system and the foreground activity announcement system.

Intent announced that the system checks all activities Intent filters to find out which activity has been defined to handle this tag, assuming that multiple activity has been configured to handle the same tag Intent. The activity selector will then be used to let the user choose which activity to use.

After the user chooses. The selected activity will be used to process this intent.

The front desk announcement system agreed to an activity covering the Intent announcement system and first dealing with this tag Intent. This requires that you will be dealing with the activity of the tag intent in the foreground, so that when an NFC tag is scanned, the system detects whether the activity in the foreground supports processing this intent, assuming support, and this intent is passed on to this activity, assuming it is not supported. Then go to intent to announce the system.

As an example, the following code is required to write the system:

1. Defining variables

Private nfcadapter madapter;     Private string[][] techlist;     Private intentfilter[] intentfilters;     Private pendingintent pendingintent;     Private Tag tag;

2. Add the following code to the activity's OnCreate () method:

//get an NFC adapterMadapter = Nfcadapter.getdefaultadapter ( This); //defines the NFC protocol that the program can be compatible with, such as NFCA and NFCV//in Intent filters you want to deal with the intent, a tag was detected when the first check the front desk announcement system,//assuming that the foreground activity complies with the requirements of intent filter, then the activity of the foreground will handle this intent. //assuming that the non-conformance, the front desk announcement system will be intent to intent announcement system.        Assume that a null intent filters is specified. //When random tag is detected, you will receive tag_discovered intent.

So please note that you should only deal with the intent you want.Techlist =Newstring[][] {NewString[] {Android.nfc.tech.NfcV.class. GetName ()},NewString[] {Android.nfc.tech.NfcA.class. GetName ()}}; Intentfilters=NewIntentfilter[] {NewIntentfilter (nfcadapter.action_tech_discovered),}; //Create a Pendingintent object so that the Android system can navigate to the object when a tag is detectedPendingintent = Pendingintent.getactivity ( This, 0,NewIntent ( This, GetClass ()). Addflags (intent.flag_activity_single_top),0);

3. In the Onnewintent method:

     Public void onnewintent (Intent Intent) {        = Intent.getparcelableextra (nfcadapter.extra_tag);                 return ;    }

4. In the OnPause method:

    @Override    protectedvoid  onPause () {        super. OnPause ( );        Madapter.disableforegrounddispatch (this);    }

4. In the Onresume method:

protected  void " Span style= "Line-height:1.5!important" > Onresume () { super  .onresume ();  //  Use the front desk to announce the system  Madapter.enableforegrounddispatch (this   Pendingintent, Intentfilters, Tech        List); }    }

After defining these methods, the execution of the program, without the lock screen, the use of NFCV or NFCA NFC card close to the phone when the onnewintent will be triggered. Tag can be obtained, can use the obtained tag to query the card of some specific information and data.

Preparation of the Android NFC near-field communication----Read-write card

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.