In the mobile platform development class, the teacher gave us a source (in fact, let us write it out, she is to give us a reference to haha), let us add her requirements of the function, in accordance with the blogger urine sex This blog will certainly not introduce the development of this application, I will be the knot course of the paper sent up.
Development of NFC application in Android system
Sun Linan
Absrtact: Near Field Communication (English: Near Field COMMUNICATION,NFC), also known as proximity wireless communication, is a short-range high-frequency wireless communication technology that allows contactless point-to-point data transmission between electronic devices, exchanging data within 10 centimeters (3.9 inches). The Android system supports the development of NFC applications.
Key words: Android; NFC; Support development
1NFC Introduction
Near Field Communication (English: Near Field COMMUNICATION,NFC), also known as proximity wireless communication, is a short-range, high-frequency wireless communication technology that allows contactless point-to-point data transmission between electronic devices, exchanging data within 10 centimeters (3.9 inches).
The technology evolved from contactless radio frequency identification (RFID), developed jointly by Philips Semiconductor (now NXP Semiconductors), Nokia and Sony, based on RFID and interconnect technology. Near Field communication is a short-range high frequency radio technology that operates at a distance of 20 centimeters at 13.56MHz. Its transmission speed has 106 kbit/seconds, 212 kbit/seconds or 424 kbit/seconds three kinds. The current near field communication has been adopted as ISO/IEC is 18092 International standard, EMCA-340 Standard and ETSI TS 102 190. NFC uses both active and passive read modes.
There are now three main types of NFC working modes:
Card emulation Mode: This mode is actually equivalent to an IC card using RFID technology. [Source request] can replace the current large number of IC cards (including credit cards) occasions shopping malls card, leisurely travel cards, access control, tickets, tickets and so on. In this way, there is a great advantage, that is, the card through the contactless reader's RF domain to power, even if the host device (such as mobile phones) can work without electricity. NFC Devices for card Emulation applications, you must have an NFC chip built into the security element, SE);
Point-to-point mode: This mode is similar to infrared, it can be used for data exchange, but the transmission distance is short, the transmission is created faster, the transmission speed is faster and the power consumption is low (Bluetooth is similar). Link two NFC-enabled devices for data point-to-point transmission, such as downloading music, swapping pictures, or syncing device address book. As a result, multiple devices such as digital cameras, PDAs, computers and mobile phones can be exchanged for information or services via NFC;
Reader/writer mode: Used as a contactless reader, such as reading information from posters or electronic labels on exhibition information.
2API Overview
The 1.ANDROID.NFC package contains the top-level classes used to interact with the local NFC adapter. These classes can represent the detected tags and use the NDEF data format.
Class |
Describe |
Nfcmanager |
An NFC Adapter manager that can list all the NFC adapter supported by this Android device. Just because most Android devices have only one NFC adapter, you can use the static method Getdefaultadapter (context) to fetch the adapter in most cases. |
Nfcadapter |
Represents the NFC adapter of this device, you can define intent to request reminders that the system detects tags to be sent to your activity. and provide a way to register the foreground tag reminder release and front desk ndef push. Front desk ndef Push is the only supported NFC communication mode for the current Android version. |
Ndefmessage and Ndefrecord |
Ndef is the data structure defined by the NFC Forum, which is used to effectively store data to NFC tags. such as text, URLs, and other MIME types. A ndefmessage plays a container of what data is sent and read from this container. A Ndefmessage object contains 0 or more ndefrecord, each with a type such as text, URL, smart Poster/AD, or other MIME data. The first type of Nfcrecord in Ndefmessage is used to send tags to an activity on an Android device. |
Tag |
Flag a passive NFC target, such as Tag,card, key hook, or even a phone analog NFC card. When a tag is detected, a tag object is created and encapsulated in a intent, and then the NFC publishing system sends the intent startactivity to an activity registered to accept the intent. You can use the Gettechlist () method to get the technical details of this tag support and create a corresponding Tagtechnology object provided by the Android.nfc.tech. |
The 2.android.nfc.tech package contains classes for tag query properties and I/O operations. These classes are labeled with a different NFC technology standard supported by tag.
Class |
Describe |
Tagtechnology |
This interface is required for all of the following tag technology classes. |
Nfca |
Supports operation of ISO 14443-3a standard. Provides access to Nfc-a (ISO 14443-3a) properties and I/O operations. |
Nfcb |
Provides access to Nfc-b (ISO 14443-3b) properties and I/O operations. |
Nfcf |
Provides access to Nfc-f (JIS 6319-4) properties and I/O operations. |
Nfcv |
Provides access to Nfc-v (ISO 15693) properties and I/O operations. |
Isodep |
Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations. |
Ndef |
Provides access to and other operations on data that are formatted as NDEF tags. Provides access to NDEF data and operations on the NFC tags that has been formatted as NDEF. |
Ndefformatable |
Provides a formatted operation for tags that can be formatted into the NDEF format |
Mifareclassic |
If the Android device supports MIFARE, provide properties and I/O operations to the Mifare Classic target. |
Mifareultralight |
If the Android device supports MIFARE, provide properties and I/O operations to the Mifare ultralight target. |
3NFC Tag Scheduling system
In general, unless NFC is disabled in the device's Settings menu, the Android device searches for NFC in a non-lock screen state. When an Android device discovers an NFC tag, the desired behavior is to handle the intent with the most appropriate activity, Instead of asking the user what application to use? Because the device can only scan the NFC tag within a very short range, forcing the user to manually select an activity causes the device to leave the NFC tag and interrupt the connection. Therefore, you should take care to block the operation of the selector when you develop your activity. Android provides a special label scheduling system to analyze the scanned NFC tag, by parsing the data and trying to find an application of interest in the data being scanned, as follows:
1. Resolve the NFC tag and figure out the MIME type or URI that identifies the data payload in the tag;
2. Encapsulate the MIME type or URI and the data payload into a intent?
3. Start activity based on intent.
3.1How to map NFC tags to mime types and URIs
When an Android device scans for an NFC tag that contains ndef format data, it resolves the message and attempts to figure out the MIME type or URI identifier of the data? First the system reads the first Ndefrecord in the message (Ndefmessage), To determine how to interpret the entire NDEF message (a NDEF message can have multiple Ndef records)? In a well-formed NDEF message, the first Ndefrecord contains the following field information:
1) 3-bit TNF (type name format) indicates how variable length type fields are interpreted, and the valid values are described in table 1 below?
2) Variable length type describes the type of record, and if you use Tnf_well_known, then use this field to specify the type definition (RTD) for the record? The valid RTD values are defined in table 2 below?
3) variable length ID uniquely identifies the record? This field is not used frequently, but if you need to uniquely identify a tag, you can create an ID for that field?
4) variable-length load do you want to read/write the actual data load? A NDEF message can contain multiple NDEF records, so don't assume that all the load is included in the first ndef record of the NDEF message?
The tag dispatch system uses the TNF and type fields to attempt to map a MIME type or URI to a NDEF message? If successful, it encapsulates the information with the actual payload into the action_nedf_discovered type of intent? However, There will be a label dispatch system can not judge the data type according to the first ndef record, so that there will be ndef data can not be mapped to MIME type or URI, or the NFC tag does not contain ndef start data of the situation occurs? In this case, The tag object associated with a label technical information is replaced with the payload encapsulated within the Action_tech_discovered type intent object.
3.2How an application dispatches an NFC tag
When the label dispatch system completes the creation of an intent object encapsulated by an NFC tag and its identity information, it sends the intent object to the application of interest. If more than one application is able to process the intent object, the activity selector is displayed, allowing the user to select activity. The label dispatch system defines three intent objects, and the following three intent objects are listed as high-to-low priority:
1. Action_ndef_discovered: This intent is used to initiate activity that contains NDEF loads and known types of labels. This is the highest priority intent, and the label dispatch system attempts to start the activity with this type of intent as much as possible before any other intent.
2. action_tech_discovered: If there is no activity registered to handle intent of the action_ndef_discovered type, the label dispatch system will attempt to use this type of intent to start the application. If the scanned label contains NDEF data that cannot be mapped to a MIME type or URI, or does not contain NDEF data, but is known as a label technology, this type of intent object is also started directly (instead of starting action_ndef_ Discovered type of intent)
3. action_tab_discovered: This type of intent is initiated if the activity of the action_ndef_discovered or action_tech_discovered type intent is not processed.
The basic working methods of the label dispatch system are as follows:
1. Try to start the activity by using the intent object (action_ndef_discovered or action_tech_discovered) created by the label dispatch system when parsing the NFC tag;
2. If there is no corresponding activity to process intent, then the next priority intent (action_tech_discovered or action_tag_discovered) will be attempted to start the activity, Until there is a corresponding application to handle this intent, or until the label dispatch system tries all possible intent.
3. If there is no application to handle any type of intent, then do nothing.
Where possible, the NDEF message and the action_ndef_discovered type of intent are used to work because it is the most standard of the three intent. This intent, compared to the other two intent, allows the application to be launched at a more appropriate time, giving the user a better experience.
4Request NFC access in Android manifest
Before you access your device's NFC hardware and properly handle NFC intent, make the following statement in the Androidmanifest.xml file:
1. Declare access to the NFC hardware in the <uses-permission> element:
<uses-permission android:name= "Android.permission.NFC"/>
2. Depends on the minimum SDK version supported by the application. API Level 9 supports limited label scheduling only through action_tag_discovered, and can only access NDEF messages through Extra_ndef_messages. No other label properties or I/O operations are available. API level 10 includes extensive read-write support to better drive Ndef's application prospects, and API Leve 14 provides an easier way to push ndef messages to other devices with Android Beam and an additional convenient way to create Ndef records.
<USES-SDK android:minsdkversion= "Ten"/>
3. Use the Uses-feature element on Google Play so that the app can be displayed only for devices with NFC hardware.
<uses-feature android:name= "ANDROID.HARDWARE.NFC" android:required= "true"/>
If your application uses NFC functionality, but the related functionality is not a key feature of your application, you can ignore the uses-feature element and check that NFC is valid at run time by calling the Getdefaultadapter () method
5Intent for filtering NFC
To start the application when you want to process the scanned NFC tag, you can filter the intent for one, two, or all three types of NFC in the app's Android manifest.
1. You typically want to control the intent of the most commonly used action_ndef_discovered type when the application starts.
2. Intent from action_ndef_discovered type intent back to Action_tech_ when no applications are filtered for the action_ndef_discovered type, or if the data payload is not NDEF Discovered type of intent.
3. Usually the action_tab_discovered is the most generalized filter classification. Many applications filter action_ndef_discovered or action_tech_discovered before filtering action_tag_discovered, which reduces the likelihood that an application will be started. Action_tag_discovered is only the last resort to use if there are no applications that handle action_ndef_discovered or action_tech_discovered types of intent.
6Send ndef messages to other devices.
Android beam allows for simple peer data exchange between two Android devices, applications that want to send data to another device must be in the foreground, and the device that receives the data must not be locked. When the transmitting device is close enough to the receiving device, the transmitting device will display the "Touch to Beam" UI. The user can then choose whether to transmit the message to the receiving device.
You can enable Android Beam for your application by calling any of the following two methods:
1. Setndefpushmessage (): This method sets the received Ndefmessage object as a message to beam. When two devices are close enough, the message is automatically sent.
2. Setndefpushmessagecallback (): Receives a callback containing the Createndefmessage () method, which is called when the device is within the range of the emitted data. Callbacks will let you create ndef messages only when you need them.
An activity can only push one ndef message at a time, so if both methods are used at the same time, the Setndefpushmessagecallback () method takes precedence over the Setndefpushmessage () method. To use Android Beam, you typically must meet the following criteria:
1. The activity of transmitting data must be at the foreground. The screen of both devices must not be locked;
2. The data to be emitted must be encapsulated into a Ndefmessage object;
3. The NFC device receiving the transmitting data must support the COM.ANDROID.NPP ndef push protocol or the SNEP Protocol of the NFC Organization (Simple ndef Exchange protocol). COM.ANDROID.NPP protocol is required on devices with API Level9 (Android2.3) to API level (Android3.2). On API level (Android4.0) and later devices, both COM.ANDROID.NPP and SNEP are required.
Note: If the activity in the foreground is enabled for Android Beam, then the standard intent dispatch system will be disabled. However, if the activity also has foreground scheduling enabled, it can still scan the NFC tag that matches the intent filter in the foreground dispatch system.
7Thesis Summary
This article, through reference materials, gives readers a detailed introduction to the current development of a wide range of NFC applications in Android smartphones and even Windows phones, which is not easy for beginners like paper writers, but I hope that you will be like the author After reading this paper, I can learn more knowledge.
Reference documents:
[1] Wikipedia, the free encyclopedia, the Near field communication entry.
[2] Introduction to NFC application development on Android.
[3] "in-depth Android application development: Core technology Analysis and best practices", Miaozhongliang, Zengxu, Huanbin, first edition, mechanical industry press.
Mobile Application Development Knot course paper