NFC Android Development Tutorial244


Near Field Communication (NFC) is a short-range wireless technology that allows two devices to communicate when they are brought within close proximity. NFC is often used for contactless payments, but it can also be used for other applications, such as data sharing and device control.

In this tutorial, we will show you how to develop an NFC application for Android. We will cover the basics of NFC development, including how to read and write NFC tags, and how to create custom NFC tags. We will also provide some tips and tricks for developing NFC applications.

Prerequisites

To develop NFC applications for Android, you will need the following:*
An Android device with NFC support
The Android SDK
An NFC development kit (optional)

Getting Started

To get started, create a new Android project in Android Studio. Then, add the following dependency to your project's file:dependencies {
implementation ':play-services-nfc:17.0.0'
}

Next, you need to enable NFC in your file. Add the following line to the element:

Reading NFC Tags

To read an NFC tag, you need to create an NfcAdapter object. The NfcAdapter object represents the NFC hardware on your device. Once you have an NfcAdapter object, you can use it to listen for NFC tags. Here is an example of how to read an NFC tag:NfcAdapter nfcAdapter = (this);
PendingIntent pendingIntent = (this, 0, new Intent(this, ), 0);
IntentFilter[] intentFiltersArray = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };
(this, pendingIntent, intentFiltersArray, null);

When an NFC tag is discovered, the onNewIntent() method of your activity will be called. In the onNewIntent() method, you can get the NfcEvent object and use it to get the NFC tag data. Here is an example of how to get the NFC tag data:protected void onNewIntent(Intent intent) {
(intent);
if ((())) {
NfcEvent nfcEvent = (intent);
NdefMessage ndefMessage = ();
if (ndefMessage != null) {
NdefRecord[] ndefRecords = ();
for (NdefRecord ndefRecord : ndefRecords) {
if (() == NdefRecord.TNF_TEXT) {
byte[] payload = ();
String text = new String(payload);
// Do something with the text
}
}
}
}
}

Writing NFC Tags

To write an NFC tag, you need to create an NdefMessage object and then write it to the tag. The NdefMessage object represents the data that you want to write to the tag. Once you have an NdefMessage object, you can use the NfcAdapter object to write it to the tag. Here is an example of how to write an NFC tag:NdefMessage ndefMessage = ("Hello, world!");
NfcAdapter nfcAdapter = (this);
PendingIntent pendingIntent = (this, 0, new Intent(this, ), 0);
(this, pendingIntent, null, null);

When an NFC tag is discovered, the onNewIntent() method of your activity will be called. In the onNewIntent() method, you can get the NfcEvent object and use it to write the NdefMessage object to the tag. Here is an example of how to write the NdefMessage object to the tag:protected void onNewIntent(Intent intent) {
(intent);
if ((())) {
NfcEvent nfcEvent = (intent);
NdefMessage ndefMessage = ();
if (ndefMessage != null) {
NdefRecord[] ndefRecords = ();
for (NdefRecord ndefRecord : ndefRecords) {
if (() == NdefRecord.TNF_TEXT) {
byte[] payload = ();
String text = new String(payload);
// Do something with the text
}
}
}
}
}

Tips and Tricks

Here are some tips and tricks for developing NFC applications:*
Use the NfcAdapter object to enable foreground dispatch. This will allow your application to receive NFC intents even when it is not running in the foreground.
Use the NdefMessage object to represent the data that you want to write to an NFC tag. The NdefMessage object can contain various types of data, including text, URI, and MIME.
Use the NdefRecord object to represent a single piece of data in an NdefMessage object. The NdefRecord object can contain various types of data, including text, URI, and MIME.
Use the NfcEvent object to get information about an NFC tag. The NfcEvent object can provide information about the tag's type, ID, and data.

2025-01-16


Previous:Dongying Property AI Video Tutorial: A Comprehensive Guide

Next:Python Programming: A Comprehensive Beginner‘s Guide