Android NFC Apps Made Easy. Near-field communication looks like it…
By: Andrei Elekes
Tags: Android, Java, Programming, Coding, Development

What is it? 🤷‍♂️

Near-field communication (NFC) is a set of communication protocols that enable two electronic devices, one of which is usually a portable device such as a smartphone, to establish communication by bringing them within 4 cm (1​1⁄2 in) of each other.

NFC allows you to share small payloads of data between an NFC tag and a device, or between two devices.

Tags can range in complexity. Simple tags offer just read and write semantics, sometimes with one-time-programmable areas to make the card read-only. More complex tags offer math operations and have cryptographic hardware to authenticate access to a sector. The most sophisticated tags contain operating environments, allowing complex interactions with code executing on the tag. The data stored in the tag can also be written in a variety of formats, but many of the Android framework APIs are based around an NFC Forum standard called NDEF (NFC Data Exchange Format).

Benefits 🙌

Eas y network access and data sharing — NFC makes the process of connecting devices easy and intuitive. There’s no lengthy handshaking or data entry requirements. Just tap and go.

— NFC makes the process of connecting devices easy and intuitive. There’s no lengthy handshaking or data entry requirements. Just tap and go. User control with expressed intent — NFC offers a simple, intuitive means of indicating the user’s intent to initiate action. A quick tap makes it clear.

— NFC offers a simple, intuitive means of indicating the user’s intent to initiate action. A quick tap makes it clear. Data security at multiple levels — Wide-open networks allow opportunities for hackers. NFC counters with built-in features that limit opportunities for eavesdropping, and easy-to-deploy options for additional protections to match each use case.

— Wide-open networks allow opportunities for hackers. NFC counters with built-in features that limit opportunities for eavesdropping, and easy-to-deploy options for additional protections to match each use case. The ability to connect the unconnected — NFC solves the problem of unpowered objects that lack network access. By embedding NFC tags in unpowered, unconnected objects, you can add intelligence anywhere. With a tap of an NFC-enabled device, it can open a URL and provide access to online information.

How does NFC work? 🤷‍♀️

The tech involved is deceptively simple: evolved from radio frequency identification (RFID) tech, an NFC chip operates as one part of a wireless link. Once it’s activated by another chip, small amounts of data between the two devices can be transferred when held a few centimeters from each other.

No pairing code is necessary to link up and because it uses chips that run on very low amounts of power (or passively, using even less), it’s much more power-efficient than other wireless communication types.

At its core, NFC works to identify us by our enabled cards and devices (and by extension, our bank accounts and other personal info.)

Vulnerability and security 🔐

The magic of NFC occurs in the free and open-air, so it’s easy to get wrapped up in the idea that your data can be nabbed by anyone who tries to intercept. It can really happen, but it doesn’t have to happen to you.

Let’s first tackle this scary issue with the point that NFC chips in your cards or phones can’t be skimmed unless they are within mere centimeters of a potential thief. That’s just the way the technology works. We don’t know about you, but there aren’t that many things that get close to us.

There are conmen out there who try to ruin it for everyone with NFC skimming apps that rip your personal information, including your address and account details. But they are having an increasingly tough time with that goal, as apps like Apple Pay and Android Pay are implementing some clever safeguards to protect users.

NFC Powered Devices 📶

Devices with NFC simultaneously support three main modes of operation:

Reader/writer mode, allowing the NFC device to read and/or write passive NFC tags and stickers. P2P mode, allowing the NFC device to exchange data with other NFC peers; this operation mode is used by Android Beam. Card emulation mode, allowing the NFC device itself to act as an NFC card. The emulated NFC card can then be accessed by an external NFC reader, such as an NFC point-of-sale terminal.

Does my device have NFC? 😐

Use this link below, and search for your device to find if you are NFC enabled.

Android Studio Application 🔨

With NFC gradually becoming a necessary technology nowadays, I decided to test out the reliability of NFC devices, so I tried to implement a simple Android application that will use the mobile devices NFC chip and will read other NFC powered devices.

Luckily I found an amazing developer develedio 🙏 and his repository at LINK . After importing the ed_NFC_Example project into your Android Studio, you can compile, transfer and run the application on an android mobile device. Below I will be explaining how to compile the ed_NFC_Example project onto your Android mobile device or onto a Virtual Device.

1️⃣˙ Compile to an Android Device ☎️

These project files, allowed me to use an application that consistently scans for surrounding NFC users and extract the data from those clients. The NFC client ID’s that are scanned are stored within the dumpTagData class. This is where I was able to use the ID from the device that was read, and compare it to a (known) already stored NFC tag.

private String dumpTagData(Parcelable p) {



StringBuilder sb = new StringBuilder();

Tag tag = (Tag) p;

byte[] id = tag.getId(); // 25555636771 = Andrei's NFC Card



if (getDec(id) == 25555636771L) { //do something }

}

This allowed me to identify the known card as my own // 25555636771 = Andrei’s NFC Card . The possibilities this creates are endless, being given the capability of reading a device and verifying if that device is registered or not can be very beneficial security-wise. 🔐

2️⃣˙ Compile on Virtual Device 💻

The project files as-is are not ready to be compiled on a virtual machine since it will not have the NFC chip and it will throw errors. But I did find a workaround with:

@Override

protected void onResume() {

super.onResume();

Log.d("onResume", "1"); //mTextView.setText("onResume:");

// creating pending intent:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

// creating intent receiver for NFC events:

IntentFilter filter = new IntentFilter();

filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);

filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);

filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);

// enabling foreground dispatch for getting intent from NFC event:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);

}

This initializes the NFC adapter that transfers the data read from the chip to the application. By commenting out:

// NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

// nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[]{filter}, this.techList);

}

You can compile and run the application on a virtual machine.

⚠️Note this project within a virtual environment is only for testing visuals and other functions implemented, this will NOT allow any NFC functions.⚠️