[NFC application API in Android]

Source: Internet
Author: User
About NFC in Android 2.3
Android 2.3 supports NFC (near-field communication. The official website pai.android.com is described in English as follows:
Near Field Communications (NFC)
Android 2.3 uplodes an NFC stack and framework API that lets developers read ndef tags that are discovered as a user touches an NFC-enabled device to tag elements embedded in stickers, smart posters, and even other devices.
The platform provides the underlying NFC services that work with the device hardware to discover tags when they come into range. on discovering a tag, the platform notifies applications by broadcasting an intent, appending the tag's ndef messages to the intent as extras. applications can create intent filters to recognize and handle targeted tags and messages. for example, after processing ing a tag by intent, applications extract the ndef messages, store them, alert the user, or handle them in other ways.


The nfc api is available in the Android. NFC package. The key classes are:
  • Nfcadapter, Which represents the NFC hardware on the device.
  • Ndefmessage, Which represents an ndef data message, the standard format in which "records" carrying data are transmitted between devices and tags. Applications can receive these messages from action_tag_discovered intents.
  • Ndefrecord, Delivered in an ndefmessage, which describes the type of data being shared and carries the data itself.

NFC communication relies on wireless technology in the device hardware, so support for the platform's NFC features on specific devices is determined by their manufacturers. to determine the NFC support on the current device, applications can callIsenabled ()To query the nfcadapter. The nfc api is always present, however, regardless of underlying hardware support.


To use the nfc api, applications must request permission from the user by declaring <Uses-Permission Android: Name = "android. Permission. NFC"> In their manifest files.
Additionally, developers can request filtering on Android Market, such that their applications are not discoverable to users whose devices do not support NFC. To request filtering, add <Uses-feature Android: Name = "android. Hardware. NFC" Android: required = "true"> To the application's manifest.

There are many NFC application scenarios, but currently the API of Android 2.3 only providesElectronic tag Reader. I believe that with the upgrade of Android, many application scenarios and APIs will be added in the future.
The function is a little less, but the first step from nothing is always not easy. In terms of architecture, there are at least the following changes:

  • Android framework layer, which adds APIs for application developmentFramework API).
  • Protocol layer to add the NFC protocol stackNFC Stack).
  • The hardware adaptation layer provides an adaptation interface for hardware manufacturers. This layer should communicate with the Linux driver of NFC hardware (implementation method to be verified ).

The workflow is that when users enable the NFC function on mobile phones or other electronic terminals (hereinafter referred to as mobile phones ),NfcadapterAfter scanning the electronic tag, it will send to the relevant user programAction_tag_discoveredThe extras architecture of itent contains the ndef (NFC data exchange format) message. How to handle this ndef message is a matter of the user program.
NFC APIs are available in Android. the NFC package provides three main categories: nfcadapter describes NFC hardware in mobile phones, and Android 2.3 can be understood as an electronic tag scanner for the moment. Messages in electronic tags and scanners are represented by ndefmessage. This class is very simple, but encapsulates ndefrecord. Each ndefmessage can contain multiple ndefrecord. You can use the ndefmessage-like method getrecords () to query all ndefrecord messages. Ndefrecord is the true carrier of information. Correct understanding of this class is a key point in understanding NFC technology. NFC technical specifications [url = link. php? Url = http://www.nfc-forum.org % 2 fspecs % 2fspec_list % 2f] http://www.nfc-forum.org/specs/spec_list/#/url.pdf is an indispensable reference of the category.
The application programming logic is as follows:

  • Use Android. NFC. nfcadapter. getdefaadapter adapter () to obtain the objnfcadapter of the mobile phone.
  • Use objnfcadapter. isenabled () to check whether NFC is supported for the mobile phone.
  • If the mobile phone supports NFC, apply to receive the intent of action_tag_discovered
  • If action_tag_discovered is received, ndefmessage is extracted and ndefrecord is then extracted. The entire message parsing process
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.