How to use NFC Tags: Detect, Read and Write NFCs with Android Studio
Kickstart your NFC project with this simple guide and starter code.
Introduction
Near-Field-Communication (NFC) isn’t new technology. With the events of Covid 19, there has been a surge in contactless NFC usage as seen by the new trend of mobile payment like Google Pay and Apple Pay. This is similar to its well-known brother, QR codes.
It has come a long way. First used in 1997 for Hasbro Star Wars toys, the now highly accessible tech which currently exists in around 73% of smartphones, is an untapped source for innovation.
Similar to my previous article on installing TF lite for Raspberry Pi, finding useful NFC material was a chore. This is meant to be a quick and clear guide for the general-use cases of NFCs.
Complete Code on my Github: starter code
Prerequisites
- Android Studio — Installation guide here.
- An Android Phone with NFC capabilities (How to check if I have NFC?)
- NFC Device (Note: I used an NFC card)
NFC Detection
We will first detect the NFC device information:
1. NFC id
2. NFC Technologies
3. NFC type (Mifare Classic/Ultralight)
Note: We will be using the Mifare Ultralight C for this guide.
Step 0: AndroidManifest.xml, Allow NFC permissions for our app
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
Step 1: onCreate, Initialize the NFC adapter and define the Pending Intent
public class MainActivity extends AppCompatActivity { //Intialize attributes
NfcAdapter nfcAdapter;
PendingIntent pendingIntent; final static String TAG = "nfc_test"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)…