Android NFC Programming

Source: Internet
Author: User

NFC Introduction

NFC is near Field communication abbreviation, that is, the close-range wireless communication technology. NFC, developed jointly by Philips and Sony, is a non-

Contact identification and interconnection technology, can be used in mobile devices, consumer electronics,

Near-distance wireless communication between PC and smart control tools.

The 13.56MHz frequency operates within 20 cm of the distance. Its transmission speed has 106 kbit/seconds, 212 kbit/seconds or 424 kbit/seconds three kinds. Near-field communication is now

ISO/IEC is 18092 International standards, ECMA-340 standards and ETSI

TS 102 190 Standard. NFC uses both active and passive modes of communication.

Mobile NFC supports three modes of operation: read/write tag mode, point-to-dot mode, analog card mode.


Basic types of NFC

The Android platform offers two android.nfc and Android.nfc.tech packages with APIs to enable the operation of NFC tags.

There are 4 main classes of ANDROID.NFC packages:

Nfcmanager class: can be used to manage all NFC Adapter indicated on Android devices, but since most Android devices support only one NFC

Adapter, you can use the Getdefaultaapater method directly to obtain system-supported Adapter.

Nfcadapter class: This device's NFC adapter, can be defined intent to request that the system detects tags to send reminders to your activity, and provide the party

Law to register the front desk tag reminder release and front desk ndef push.

Ndefmessage class: Ndef is the data structure defined by the NFC Forum, which is used to effectively store data in tags such as text, URLs, and other MIME classes

Type. A ndefmessage plays a container of what data is sent and read from this container. A Ndefmessage object contains 0 or more

Ndefrecord, each ndef record has one type, such as text, URL, smart Poster/AD, or other MIME data. In

The first type of Nfcrecord in Ndefmessage is used to send tags to an activity on an Android device.

Tag class: identifies a passive NFC target, such as Tag,card, key hook, or even a phone analog NFC card. The label can be supplied with the

Method of operation.

Android.nfc.tech Bag

The Android.nfc.tech contains those classes that query properties on the tag and perform I/O operations. These classes are labeled with a different NFC technology standard supported by tag.

tagtechnology: This interface must be implemented by all of the following tag technology classes.

NFCA: supports ISO 14443-3a-standard properties and I/O operations.

NFCB: Nfc-b (ISO 14443-3b) properties and I/O operations.

NFCF: Properties and I/O operations for Nfc-f (JIS 6319-4).

NFCV: Nfc-v (ISO 15693) properties and I/O operations.

ISODEP: ISO-DEP (ISO 14443-4) properties and I/O operations.

Ndef: Read and write to a label that supports NDEF format.

ndefformatable: provides a formatted operation for tags that can be formatted into the NDEF format.

Mifareclassic: If the Android device supports Mifare, provides properties and I/O operations on the Mifare Classic target.

mifareultralight: If the Android device supports Mifare, provides properties and I/O operations to the Mifare ultralight target.      


Basic NFC programming steps

Set permissions

Limit version number

Restricting installed devices

Define activity that can receive tags (information about the tag can be obtained)

Process business (depending on the notes of the specific business)


NFC Tag Filter system




Nfcmanager class:

Use Getsystemservice (java.lang.String) with Nfc_service to create a nfcmanager, then call Getdefaultadapter () to obtain T He nfcadapter.

Alternately, you can just call the static helper Getdefaultadapter (Android.content.Context).

public static Nfcadapter Getdefaultadapter (context context): Get the default NFC device on the phone

An NFC module, all calling this method.


Nfcadapter class:

Important constants (used to obtain information from intent, the intent is initiated by the system after NFC detects the tag, by Getintent (). Getparcelableextra (Nfcadapter. Constant name); method gets the corresponding object):

Extra_tag (required): It is a tag object that represents the tag being scanned;

Getparcelableextra (Nfcadapter.extra_tag) Gets the label object.

extra_ndef_messages (optional): It is an array that parses the NDEF message from the label. This additional information is enforced on the intent

The NDEF message can be obtained by Getparcelablearrayextra (nfcadapter.extra_ndef_messages).

extra_id (optional): low-level ID of the label.

The following three constants are used to determine the type of tag in the obtained intent:

action_ndef_discovered: Nfcadapter. action_ndef_discovered . Equals (Getintent (). Getaction ())

action_tag_discovered: Nfcadapter. action_tag_discovered . Equals (Getintent (). Getaction ())

action_tech_discovered: Nfcadapter. action_tech_discovered . Equals (Getintent (). Getaction ())

Important methods:

public boolean isenabled (): Used to determine whether the current NFC is in a usable state

public void Enableforegrounddispatch (activity activity, pendingintent intent, intentfilter[] filters, string[][]

techlists): Enable foreground dispatch to the given activity. Used to open the foreground scheduler (with the highest permissions) when the activity

Located in the foreground (foreground process), you can call this method to open the foreground scheduling, usually in the Onresume () callback method

public void Disableforegrounddispatch (activity activity): Closes the foreground schedule, typically in the OnStop () callback method


Ndefmessage class:

The following three methods are used to construct the tag data of a NDEF data structure (used for data transfer between activity and tag, read and write

The object is encapsulated)

Public Ndefmessage (byte[] data)

Public Ndefmessage (Ndefrecord record, Ndefrecord ... records)

Public Ndefmessage (ndefrecord[] Records)

Public byte[] Tobytearray ():

public int getbytearraylength ():

Public ndefrecord[] GetRecords (): Get the NDEF Records inside this NDEF Message.


Ndefrecord class:

The following two are construction methods:

Public Ndefrecord (short TNF, byte[] type, byte[] ID, byte[] payload):

Public Ndefrecord (byte[] data):

The following four methods get the type of the Ndefrecord object's corresponding field:

Public byte[] GetType ():

public short GETTNF ():

Public byte[] Getpayload ():

Public byte[] GetId ():


Tag class:

Public string[] Gettechlist (): Get The technologies available in this tag, as fully qualified class names.


Ndef class:

This class is used to package the read and write operation of the tag in the ndef format, and different tags are packaged in different classes in the Android.nfc.tech package.

public static Ndef get (Tag tag): Get a instance of Ndef for the given Tag. Building objects

public void Connect (): Enable I/O operations to the tag from this Tagtechnology object., open I/O operation

public boolean iswritable (): Determine if the tag is writable. Determine if writable

public int getmaxsize (): Get The maximum NDEF message, size in bytes.

public void Writendefmessage (Ndefmessage msg): Overwrite The ndefmessage in this tag. Write data to this tag

Public String GetType (): Get the NDEF tag type.

public boolean makereadonly (): Make a tag read-only.


Ndefformatable class :

Used to format other types of formats into NDEF format

public static ndefformatable get (Tag tag): Get a instance of ndefformatable for the given Tag. Returns NULL if

Ndefformatable was wasn't enumerated in Gettechlist (). This indicates the tag was not NDEF formatable by this Android

Device.

public void Connect (): Enable I/O operations to the tag from this Tagtechnology object.

public void Format (Ndefmessage firstmessage): Format a tag as NDEF, and write a ndefmessage.


NFC Foreground Scheduler

Give the right to handle NFC tags to a window (highest priority)

1, obtains the Nfcadapter object in the OnCreate ();

2. Create the pendingintent associated with the activity;

3. Specify a window to process the NFC tag, usually using Nfcadapter.enableforegrounddispatch () in the Onresume () method

to achieve;

4. Disable processing of NFC tags on Windows. Implemented using Nfcadapter.disableforegrounddispatch ().


Permission settings

Before you can access the NFC hardware of a device and properly handle the NFC intent, you need to declare the following key in Androidmanifest.xml first:

1.NFC access to NFC hardware using <uses-permission> elements:

<uses-permission android:name= "Android.permission.NFC"/>

2. The minimum SDK version needs to be set correctly, API Level 9 contains limited tag support, and the API level 10来 enables extensive read and write support for tag.  

<USES-SDK android:minsdkversion= "Ten"/>

3. uses-feature element definition: Your program can then display NFC hardware in the Android market.  

<uses-feature android:name= "ANDROID.HARDWARE.NFC" android:required= "true"/>

4. The NFC Intent filter tells the Android system that your activity can handle NFC data, and you can define 1 or more intent filter.



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.