Android NFC common tag reading summary

I haven't updated my blog for a few days, but this article has been prepared for a long time. I hope it can bring every developer the most simple and efficient way to learn. That's all the nonsense. Let's start the text.

NFC (near field communication) is a data transmission technology. A major difference with Wi Fi, Bluetooth, infrared and other data transmission technologies is that the effective distance generally cannot exceed 4cm. But NFC transmits faster than infrared. At present, NFC has some applications, such as electronic tag recognition, mobile phone brushing, point-to-point payment, identity recognition, information recording, etc. The purpose of this article is to unveil the NFC tag for everyone.

Next, let's start with the working mode of NFC, and we must understand NFC before developing NFC.

1. Working mode of NFC

NFC supports the following three working modes: reader / writer mode, card emulation mode and P2P mode.

Let's take a look at these three modes:

(1) Reader mode

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 Android beam. Therefore, the two devices that use Android beam to transmit data are no longer limited to 4 cm. The typical application of point-to-point mode is two NFC enabled mobile phones or tablets to realize point-to-point data transmission, such as exchanging pictures or synchronizing device contacts. Therefore, through NFC, multiple devices such as digital cameras, computers and mobile phones can quickly connect and exchange data or services.

Let's take a look at the differences between NFC, Bluetooth and infrared:

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 activities that can receive tags

The launchmode property of the activity list needs to be configured as follows:

In the activity, we also extracted a general basenfcactivity, as follows (the later activity implementations inherit from basenfcactivity):

Note: Generally speaking, all activities handling NFC should set the launchmode property to singletop or singletask, which ensures that there is only one activity instance no matter how many times the NFC tag is close to the mobile phone.

Next, let's look at several specific NFC tag application examples to quickly master NFC technology through situational learning:

3. Simple examples of two NFC Tags

1. Use NFC tag to make 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 detailed implementation code is as follows:

Note: set the launchmode property of runappactivity to singletop.

Now look at the renderings:

Close the NFC tag to the back of the mobile phone and automatically write data. At this time, exit all programs and return to the desktop. Then close the NFC tag to the back of the mobile phone and you will see that the "SMS" is automatically turned on.

Let's take another interesting example:

2. Use NFC tag to let Android automatically open web pages

How to make the NFC tag close to the mobile phone, and the mobile phone can automatically open a web page?

First, we create an ndefrecord. Android has provided us with this method:

Implementation code comparison "3. Use NFC tag to make Android run programs automatically" only modifies the writenfctag method

by

The rest remains unchanged.

The above function is still useful. For example, we attach NFC tags to some goods, which write the detailed introduction web page URI of the goods. When the user is close to the goods, the detailed introduction of the goods will be opened automatically.

Through the study of the above two cases, I believe many people have become interested in NFC. Then come down and analyze the nDef text format to see what nDef is.

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:

NDef text data format:

Status byte encoding format:

Let's start reading and writing text data in NFC Tags:

1. Read NFC tag text data

Note: the launchmode property needs to be configured in the activity list (note the same later):

2. Write NFC tag text data

We put the mobile phone close to the NFC tag. When the writing is successful, the toast of "successful writing" will pop up. Next, let's verify whether it is written successfully:

We see that the data has been written successfully, indicating that we have successfully read and written the text data in the NFC tag.

5. NDef URI format depth resolution

Like nDef text format, the URI stored in NFC tag also has a certain format, http://members.nfc-forum.org/specs/spec_ dashboard

(1) Uri format specification is simpler than text format:

(2) The prefix of URI is as follows (both are hexadecimal numbers):

Each protocol is stored in the identification code position in hexadecimal (1 byte).

Is it relatively simple? Let's parse a URI.

(3) Predefined known URI prefix

Here, we define a urirefix class to easily obtain the URI prefix:

Then let's take a look at the related configuration of the activity in the manifest file:

OK, then you can read and write URI data in NFC tag:

1. Read URI data in NFC tag

2. Write URI data in NFC tag

We put the mobile phone close to the NFC tag and verify whether it is successfully written:

We see that the data has been written successfully, indicating that we have successfully read and written the URI data in the NFC tag.

Here, the nDef format is almost finished. Next, let's take a look at the data in non nDef format.

6. Deep parsing of non nDef format

1. Mifareultralight 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.

2. Read mifareultralight format data

3. Write mifareultralight format data

We put the mobile phone close to the NFC tag and verify whether it is successfully written:

We see that "Beijing, Shanghai, Guangzhou and Tianjin" pops up, indicating that the data has been written successfully, indicating that we have successfully read and written NFC non nDef format data.

Source download: Demo

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>