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.
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.
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
Smartphone as payment method
NFC is ISO 14443 compliant and integrates many contactless cards, including bank cards that support MasterCard PayPass or VISA PayWave. The plastic card contains a microprocessor with an OS and a payment application for interacting with the data of a specific client. As soon as the card is inserted into or swiped across the terminal, it receives power to run the operating system. In the case of contactless cards, power, as we have already said, is obtained using the electromagnetic field of the reader.
With the advent of contactless cards, in fact, not necessarily a card can act as a payment instrument, but a smartphone, watch, key fob, and so on – it is not the form that matters, but the presence of the necessary set of functions. Indeed, in smartphones with NFC support, as well as in plastic cards, there is a microprocessor. From Android 4.
To manage contactless applications remotely, smartphone manufacturers and service providers connect to TSM, a service that provides access to secure data on NFC-enabled terminals. It remotely controls the Secure Element chips in smartphones, while the smartphone itself is a kind of modem operating through a secure communication channel.
When making purchases over 1,000 rubles, you may be asked to enter a PIN code or sign a receipt – everything depends on the bank.
Contactless payment technology is the most obvious benefit of NFC. A traditional plastic card with a magnetic stripe is unsafe and time consuming. Your money depends on the numbers on the front of the card, an outdated and vulnerable magnetic stripe interface, and a three-digit security code. The card is easy to lose, and you regularly “shine” its data in front of the cashier when you give him the card for payment.
With the advent of Visa payWave and MasterCard PayPass contactless payment technologies, the transaction process has become faster, but the level of security has not increased. The rules of the game have really changed with the advent of NFC in smartphones and the technology of emulating a plastic card with the possibility of payment. You just bring your smartphone to the terminal, like a regular card, and that’s it – the purchase is paid.
NFC architecture
NFC technology is based on RFID 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 modes of operation: 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.
Tags
For example, when you get into your car, you usually turn up the screen brightness to the maximum and launch Google Maps to navigate. It takes a lot of work to do this. If you have an NFC tag programmed for these actions, then just touch it with your smartphone and everything will happen by itself.
Returning home, you go to bed and want to read. You need to change the color temperature of the screen, lower the brightness, open the reader app with a book and, of course, don’t forget to set the alarm. To do this in a fraction of a second, it is enough to have a label pre-programmed for these actions at hand and attach a smartphone to it. One or two – and you’re done!
However, let’s be objective. For the vast majority of users, the convenience of NFC tags will seem somewhat far-fetched, because on daily trips from home to work and back, a navigator is not needed, and you only need to program an alarm on your smartphone once – and that’s it.
This is another common way to use NFC on a smartphone. NFC tags have become very popular in shopping and advertising areas: they are embedded in posters, billboards, placed on goods in stores. Usually these are small stickers, the cost of which does not exceed 50 rubles. The costs are minimal, but what an effect! From getting more details about a concert or merchandise to a link to a trailer for a new movie.
You can create your own NFC tags, upgrade them, and then use them in your daily life. As a rule, third-party software like NFC TagWriter is used for this. The application is quite simple and allows you to write many types of data to the tag – from a contact and a phone number to a web browser bookmark.
In fact, NFC tags are a real gift when it comes to automation. For example, you can stick a tag on your laptop and use the Trigger app to set the hotspot to turn on by selecting “Wireless & LAN” as the action. The simpler solution is to write the Wi-Fi password on the tag, stick it on the router, and then whenever guests ask for the password, send them to the NFC tag. Or else: set a mark near the bed and make it so that when touched on the smartphone, airplane mode is turned on (again using Trigger).
It will also be useful for motorists – they wrote down the algorithm for starting the navigator on the label, pasted it on the smartphone holder, and every time you install the device in this place, the navigator will start automatically.
And there are still very, very many such examples of the use of NFC tags. Often, the emergence of new cases is not limited by technology at all, but by the user’s imagination.
Open all doors
Someone implements this at home, but mostly the use of NFC for user identification and control is typical for enterprises and organizations. Turnstiles and locks are configured in such a way that with the help of a label, which is a pass, they decide whether to give its owner permission to enter or not.
With NFC in a smartphone, all the doors really open – if not literally, then figuratively for sure. In fact, the user receives a universal tool that, despite the speed limits, finds its application in a variety of situations. Here we are faced with another problem – the degree of prevalence of technology among service providers. The only thing that an ordinary user can do in this case is to use NFC as often as possible to show its relevance.
Example: Developing an NFC Reading and Writing Application
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(); }
Depending on the information read from the tag, you can perform other actions: run various tasks, open websites, etc.
We use a Mifare card for the card swipe 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.
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.
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.
Conclusion
NFC-enabled smartphones allow users to easily and conveniently perform various useful tasks, rather than searching through their pockets for ticket stubs and parking passes. This technology also makes it possible to connect with friends for information exchange, games and data transfer. NFC technology is convenient for both work and entertainment. It will play a crucial role in making our lives more convenient in the very near future.
Perhaps the best part about NFC is that the technology is available to a wide range of users. More and more manufacturers do not leave their smartphones without NFC, so it is available in devices like
(for $321.99 with code Mi5SGBS until 31.12) and
, and in flagships like
. The variety is also pleasing
What about Apple and its iPhone 7, you ask? Alas, the manufacturer limits the operation of the NFC chip in its smartphone, and it can only be used for the Apple Pay payment system. In the fall, however, it launched in Russia, so at least some plus.
https://www.youtube.com/watch?v=Tvf5bvNuD_4
How do you use NFC in your daily life? Share your experience with us in the comments.