Welcome to our in-depth guide on Near Field Communication (NFC)! 🎉 In this tutorial, we'll explore the fascinating world of NFC, learn its applications, and even write some code examples. Let's dive in!
NFC (Near Field Communication) is a short-range wireless connectivity technology that allows devices to exchange information by simply touching or bringing them close together. NFC enables contactless interactions, making it a versatile technology used in various everyday applications.
In this section, we'll write some simple code examples using Android's NFC library.
// Import necessary libraries
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.NdefWritable;
import android.util.Log;
// Create an instance of NfcAdapter
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
// Create a PendingIntent for the activity that handles NFC events
PendingIntent pintent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Check if NFC is available on the device
if (nfcAdapter != null) {
// Enable foreground dispatch for this activity
nfcAdapter.enableForegroundDispatch(this, pintent, null, PendingIntent.getBroadcast(this, 0, new Intent(NfcAdapter.ACTION_TECH_DISCOVERED), 0));
}
// In the onNewIntent() method of your activity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Extract NFC tag data
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// Check if the tag is NDEF-formatted and readable
if (tag != null && tag.getTechList()[0] instanceof Ndef) {
// Get the NDEF messages from the tag
Ndef ndef = tag.getTechList()[0].asNdef();
if (ndef != null) {
// Get the first NDEF message and its records
NdefMessage[] messages = ndef.getNdefMessages();
if (messages.length > 0) {
NdefRecord[] records = messages[0].getRecords();
for (NdefRecord record : records) {
// Log the data contained in the NDEF record
Log.d("NFC Data", new String(record.getPayload()));
}
}
}
}
}// Import necessary libraries
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.NdefWritable;
import android.nfc.tech.MifareClassic;
import android.util.Log;
// Create an instance of NfcAdapter
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
// Create a PendingIntent for the activity that handles NFC events
PendingIntent pintent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Check if NFC is available on the device
if (nfcAdapter != null) {
// Enable foreground dispatch for this activity
nfcAdapter.enableForegroundDispatch(this, pintent, null, PendingIntent.getBroadcast(this, 0, new Intent(NfcAdapter.ACTION_TAG_DISCOVERED), 0));
}
// In the onNewIntent() method of your activity
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Extract NFC tag data
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// Check if the tag is NDEF-formatted and writable
if (tag != null && tag.getTechList()[0] instanceof NdefWritable) {
// Get the NDEF writable technology from the tag
NdefWritable ndef = tag.getTechList()[0].asNdefWritable();
// Create an NDEF message containing the text "Hello, World!"
NdefMessage message = new NdefMessage(new NdefRecord[] {
new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], "Hello, World!".getBytes(Charset.forName("UTF-8")))
});
// Write the NDEF message to the NFC tag
ndef.connect();
ndef.writeNdefMessage(message);
ndef.close();
Log.d("NFC Data", "Hello, World! written to NFC tag");
}
}What is the main purpose of NFC in everyday applications?
That's it for our in-depth guide on NFC! We hope you've found this tutorial helpful. Stay tuned for more educational content on CodeYourCraft. Happy coding! 🤖🎓