In depth analysis of Android NFC technology and Android NFC development

The concept, implementation principle and the most popular source code will help you have a deeper understanding of NFC technology.

NFC is the abbreviation of near field communication, that is, short-range wireless communication technology. It can carry out short-range wireless communication between mobile devices, consumer electronics, PCs and intelligent control tools. To put it simply, what is the NFC function? What's the use of NFC? In fact, NFC provides a simple, touch based solution that allows consumers to simply and intuitively exchange information and access content and services. NFC technology allows non-contact point-to-point data transmission between electronic devices. Data can be exchanged within ten centimeters (3.9 inches). Its transmission speeds are 106kbit / s, 212kbit / s or 424kbit / s. Next, let's take a more detailed look at the application of NFC.

1、 What is NFC and how it works?

There are three main working modes of NFC: card emulation mode, P2P mode and reader / writer mode.

(1) Reader mode http://www.jianshu.com/p/e89cc9bba8a6

Data in NFC chip can be simply understood as "brushing labels". In essence, it is to read and write information from labels, stickers, business cards and other media with NFC chips through NFC enabled mobile phones or other electronic devices. Generally, NFC tags do not need external power supply. When a peripheral supporting NFC reads and writes data to NFC, it will send a certain magnetic field, which will automatically power the NFC tag.

(2) Emulation card mode

Data in NFC enabled mobile phones or other electronic devices can be simply understood as "brushing mobile phones". In essence, NFC enabled mobile phones or other electronic devices are used as IC cards such as debit cards, bus cards, access control cards, etc. The basic principle is to package the information voucher in the corresponding IC card into a data packet and store it in the peripheral supporting NFC. When in use, an NFC RF device (equivalent to card reader) is also required. When the mobile phone is close to the NFC RF device, the mobile phone will receive the signal sent by the NFC RF device. After passing a series of complex verification, the corresponding information of the IC card will be transmitted to the NFC RF device. Finally, these IC card data will be transmitted to the computer connected to the NFC RF device and processed accordingly (such as electronic transfer, door opening, etc.).

(3) Point to point mode

This mode is similar to Bluetooth and infrared. It is used for data exchange between different NFC devices, but this mode has no "brush" feeling. Its effective distance generally cannot exceed 4cm, but the transmission establishment speed is much faster than infrared and Bluetooth technologies, and the transmission speed is much faster than infrared blocks. If both sides use Android 4.2, NFC will directly use Bluetooth for transmission. This technology is called androidbeam. Therefore, the two devices that use Android beam to transmit data are no longer limited to 4 cm.

2、 Android support for NFC

There are great differences between different NFC tags. Some only support simple read-write operations, and sometimes use chips that support one-time write to design NFC tags as read-only. Of course, there are some complex NFC tags. For example, some NFC tags can restrict access to a certain area through hardware encryption. Some tags have their own operating environment, which allows NFC devices to interact more complex with these tags. The data in these tags will also be in different formats. However, the Android SDK API mainly supports the NFC forum standard, which is called nDef (NFC data exchange format).

NDef format is actually similar to NTFS of hard disk. Let's take a look at nDef data:

(1) Operation of nDef data

The Android SDK API supports the following three types of nDef data operations:

1) Read data in nDef format from NFC tag.

2) Write data in nDef format to NFC tag.

3) Send nDef data to another NFC device through Android beam technology.

Two classes are used to describe nDef format data:

1) Ndefmessage: describes the information in nDef format. In fact, we write the NFC tag with the ndefmessage object.

2) Ndevrecord: a message segment describing nDef information. An ndefmessage may contain one or more ndevrecords.

Ndefmessage and ndefrecord are the core classes of Android NFC technology. These two classes are required for reading and writing NFC tags in nDef format or transmitting data in nDef format through Android beam technology.

(2) Operation of non nDef data

For some special needs, arbitrary data may be saved. For these data, we need to customize the format. These data formats are actually ordinary byte streams. What the data in the byte stream represents is defined by the developers themselves.

(3) Basic steps of writing NFC program

1) Set permissions to restrict Android version and installed devices:

2) Define the activity that can receive tags, and configure the launchmode property as follows:

3、 NFC tag used in actual combat

(1) Use NFC tags to let Android run programs automatically

The scenario is as follows: now write the application package to the NFC program, and then we put the NFC tag close to the Android phone, and the phone will automatically run the program corresponding to the package. This is a basic NFC application. The following is an example of automatically running Android's own "SMS" close to the tag.

Writing data to NFC tags is generally divided into three steps:

1) Get tag object

2) Determine the data type of NFC tag (through the nDef. Get method)

3) Write data

The actual operation effect is as follows:

Operation steps:

Click to automatically open the SMS interface or Baidu page, enter the corresponding activity, close the NFC tag to the back of the mobile phone, and prompt that the writing is successful. Return to the main interface, and then close the NFC tag to the back of the mobile phone, you can see the automatic opening of SMS or Baidu page.

4、 NDef text format deep parsing

Obtaining the data in the NFC tag is completed through the ndefrecord.getpayload method. Of course, before processing these data, it is best to judge whether the nDef text format data is stored in the ndefrecord object.

(1) Judge whether the data is in nDef format

1) TNF (type name format) must be nderecord.tnf_ WELL_ KNowN。

2) Variable length type must be ndefrecord.rtd_ TEXT。

If the two standards are met at the same time, it is in nDef format.

(2) NDef text format specification

Data in any format is essentially composed of some bytes. For nDef text format, the first byte of these data describes the state of the data, then several bytes describe the language coding of the text, and the last remaining bytes represent the text data. These data formats are defined by relevant specifications of NFC forum and can be accessed through http://members.nfc-forum.org/specs/spec_ Download relevant specifications from dashboard.

The following two tables are the relatively important translation parts in Section 3.2 of the specification:

Read and write text data in NFC tag:

Click write text data in the NFC tag, jump to the corresponding activity, put the NFC tag card close to the mobile phone, prompt after successful writing, return to the main page, and select read text data to read.

5、 Non nDef format (the operation steps are shown in the figure above, and refer to the project code for specific implementation)

6、 NDef URI format storage (the operation steps are shown in the figure above, and the specific implementation can refer to the project code)

Mifareultrahigh t data format: the storage area of NFC tag is divided into 16 pages, each page can store 4 bytes, and one can store 64 bytes (512 bits). Page numbers start at 0 (0 to 15). The first 4 pages (0 to 3) store information related to NFC tag (such as serial number, control bit, etc. of NFC tag). Store the actual data from page 5 (pages 4 to 15). Use the mifareUltralight.get method to get the mifareUltralight object, then call the mifareUltralight.connect method to connect, and use the mifareUltralight.writePage method to write 1 pages (4 bytes) each time.

You can also use the mifareultralight.readpages method to read 4 consecutive pages at a time. If the sequence number of the page read exceeds 15, it is read from the beginning. For example, start reading from page 15 (No. 14). The readpages method reads 14, 15, 0, and 1 pages of data.

GitHub link

Project source code address: https://github.com/RickyYu/Nfc-Android

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>