Preparation of the Android NFC near-field communication----Read-write card
because the company took a sound to feel more NB project, to an oilfield pie System. And by the small woman responsible for the development of the Android client ~ ~ Development when the Android device looks relatively tall ha, explosion-proof, in fact, many times colleagues are trying to joke said try to fall on the ground, are explosion-proof, that certainly fall not bad ha ~ ~ development process need to use NFC technology, So on the Internet to learn and organize a bit about the Android NFC-related knowledge ~ Well, cut into the topic, this article write read card before the preparation work:
PS: This series has some of the online finishing, thank you ~ ~ Of course also contains a 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 power, cards, or devices that have a power supply.
Compared to other wireless communication technologies, such as Bluetooth and WiFi, NFC offers lower loan and distance, low cost, no need for power supply, no matching required, and the entire communication process is just a few minutes close to one second.
An Android device with NFC support is usually an initiator. It can also be used as an NFC reading and writing device. He will detect NFC tags and open an activity to handle it. Android 2.3.3 also has limited peer support.
Tags are a lot of kinds, which simply provide read and write segments, and some can only read. Complex tags can support a number of operations, encryption to control the data segment in tags read and write. Even some tags have simple operating systems that allow some complex interaction and can execute some code.
The code examples in this article are based on API10.
To use NFC in your Android phone, you must configure the following in Androidmanifest.xml:
<uses-featureAndroid:name= "ANDROID.HARDWARE.NFC"android:required= "true" /><uses-permissionAndroid:name= "Android.permission.NFC" />
Tag Publishing System
When an Android device scans to an NFC tag, the general behavior is to automatically find the most appropriate activity to handle the tag intent without requiring the user to choose which activity to handle. Because the device scans NFC tags in a very short range and time, if the user chooses, then it is possible to need a mobile device, which will interrupt the scanning process. You should develop the activity that you only deal with tags that need to be processed 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 publishing system and the foreground activity release system.
The Intent release system checks all activities Intent filters to find out which activity has been defined to handle this tag, and if more than one activity is 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 publishing system allows an activity to overwrite the Intent publishing system and first process this tag Intent, which requires that you will be handling the activity of tag Intent in the foreground, so that when an NFC tag is scanned, The system first detects whether the activity in the foreground supports processing this intent, if it is supported, this intent is passed to this activity, if not supported, go to intent release system.
As an example of the previous platform publishing system, you need to write the following code:
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, for example NFCA and NFCV//in Intent filters you want to deal with the intent, a tag is detected when the first check the foreground publishing system,//if the foreground activity complies with the requirements of intent filter, then the activity of the foreground will handle this intent. //if not, the foreground publishing system will intent to the intent publishing system. If a null intent filters is specified,//when any 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 foreground publishing system Madapter.enableforegrounddispatch (this Pendingintent, Intentfilters, Tech List); } }
After defining these methods, run the program and, without the lock screen, the onnewintent will be triggered when using the NFCV or NFCA NFC card near the phone. Tag can be obtained by using the obtained tag to query some details and data of the card.