NFC App Development for Android*

Introduction

NFC is a short-range wireless communication technology standard that enables two-way communication between electronic devices. Sharing information between two NFC-enabled devices is as easy as one touch. For example, if you have an NFC-enabled smartphone, you can purchase goods, exchange business cards, download discount coupons, and so on with one touch. In the near future, there will be many more ways to use NFC.

This article describes NFC technology and models for its use in the current market. It also describes the use of NFC in applications for the Android platform. Finally, two examples of developing applications for reading and writing NFC are considered.

https://www.youtube.com/watch?v=ytpress

Android supports NFC in two packages: android.nfc and android.nfc.tech.

NfcManager: Android devices can be used to manage all specified NFC adapters, but since in most cases Android devices only support one NFC adapter, NfcManager is usually called directly with getDefaultAdapter to get a specific adapter.

NfcAdapter: Works as an NFC agent (similar to a network adapter in a computer) that allows cell phones to access NFC equipment to initiate NFC communications.

NDEF: The NFC standards define a common data format. It’s called NFC Data Exchange Format (NDEF) and is used to store and transfer information ranging from MIME type objects to ultra-short over-the-air documents such as URLs. NdefMessage and NdefRecord are two kinds of NDEF for data formats defined by the NFC forum. They are used in our sample code.

Tag: As defined by Android, this class represents passive objects such as radio tags, cards, etc. When a device detects a tag, Android creates a tag object and puts it in an Intent object that is sent to the corresponding action.

The android.nfc.tech package also contains many important subclasses. These subclasses provide access to RFID functionality, including read and write operations. Depending on the type of technology used, these classes are divided into different categories, such as NfcA, NfcB, NfcF, MifareClassic, etc.

NFC App Development for Android*

NDEF_DISCOVERED, TECH_DISCOVERED, TAG_DISCOVERED

We use the intent-filter type here to handle all types from TECH_DISCOVERED to ACTION_TECH_DISCOVERED. The nfc_tech_filter.xml file is used for all types defined in the TAG file. See the Android documentation for details. The figure below shows the corresponding process action when a radio tag is detected by the phone.

Figure 6 Workflow when an NFC tag is detected

NFC architecture

NFC technology is based on RFID radio tag technology using a frequency of 13.56 MHz. The typical operating distance is up to 10 cm, and the data rate can reach 424 kbps. The main advantage of NFC over other data transfer technologies is its speed and ease of use. The following figure compares NFC with other communication technologies.

Figure 1. Comparison of short-range data transmission technologies.

NFC technology supports three operation modes: card emulation mode, communication mode, and read/write mode, as shown in the following figure.

Figure 2. NFC protocol families

In card emulation mode, NFC works as a contactless smart card with an RFID tag and a security module, allowing users to make secure purchases. In data sharing mode, you can transfer data between two nearby NFC-enabled devices. You can very quickly and conveniently create WiFi* or Bluetooth* connections using NFC, and then transfer large files over the WiFi or Bluetooth connection. In Read/Write mode, you can use NFC-enabled devices to read NFC tags and run various tasks.

All modes are described in more detail below.

Example: Developing an NFC-based application using a MifareClassic card

The next transition class callback shows the read function. If the system’s broadcast transition class is NfcAdapter. ACTION_TAG_DISCOVERED, then you can read the information in the radio tag and display it.

 @Override
protected void onNewIntent(Intent intent){
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // get the detected tag
Parcelable[] msgs =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefRecord firstRecord = ((NdefMessage)msgs[0]).getRecords()[0];
byte[] payload = firstRecord.getPayload();
int payloadLength = payload.length;
int langLength = payload[0];
int textLength = payloadLength - langLength - 1;
byte[] text = new byte[textLength];
System.arraycopy(payload, 1 langLength, text, 0, textLength);
Toast.makeText(this, this.getString(R.string.ok_detection) new String(text), Toast.LENGTH_LONG).show();
}
} 

The following code shows the recording function. Before determining the value of mytag, you need to find out if the radio tag is detected, and then write the information to mytag.

 If (mytag==Null){
……
}
else{
……
write(message.getText().toString(),mytag);
……
}
private void write(String text, Tag tag) throws IOException, FormatException {
NdefRecord[] records = { createRecord(text) };
NdefMessage message = new NdefMessage(records);
// Get an instance of Ndef for the tag.
Ndef ndef = Ndef.get(tag); // Enable I/O
ndef.connect(); // Write the message
ndef.writeNdefMessage(message); // close the connection
ndef.close();
} 

NFC App Development for Android*

Depending on the information read from the tag, you can perform other actions: launch various tasks, open websites, etc.

We use a Mifare card for the card swiping test and use the TAG type of the Mifare Classic card. The MifareClassic card is widely used for a variety of purposes: as an identity card, to pay for public transportation, etc. The memory of a traditional MifareClassic card is divided into 16 sectors, each sector includes 4 blocks, and each block can contain 16 bytes of data.

https://www.youtube.com/watch?v=https:accounts.google.comServiceLogin

The last block in each area is called a trailer, it is mainly used to store the local block key for reading and writing data. It contains two keys, A and B, each 6 bytes long, the default value is usually FF or 0 for the whole key as defined by MifareClassic. KEY_DEFAULT.

https://www.youtube.com/watch?v=ytcopyright

When writing to a Mifare card, you must first obtain the correct key value (for protection). Before a user can read and write data to this area, they must be authenticated.