Transfer Data Using Android NFC (Near field communication)
Near field communication, abbreviated NFC, is a form of contactless communication between devices like smartphones or tablets. Contactless communication allows a user to wave the smartphone over a NFC compatible device to send information without needing to touch the devices together or go through multiple steps setting up a connection.
How it work
Bluetooth and Wi-Fi seem similar to near field communication on the surface. All three allow wireless communication and data exchange between digital devices like smartphones. Yet near field communication utilizes electromagnetic radio fields while technologies such as Bluetooth and Wi-Fi focus on radio transmissions instead.
How it work
Bluetooth and Wi-Fi seem similar to near field communication on the surface. All three allow wireless communication and data exchange between digital devices like smartphones. Yet near field communication utilizes electromagnetic radio fields while technologies such as Bluetooth and Wi-Fi focus on radio transmissions instead.
Near field communication, or NFC for short, is an offshoot of radio-frequency identification (RFID) with the exception that NFC is designed for use by devices within close proximity to each other. Three forms of NFC technology exist: Type A, Type B, and FeliCa. All are similar but communicate in slightly different ways. FeliCa is commonly found in Japan.
Devices using NFC may be active or passive. A passive device, such as an NFC tag, contains information that other devices can read but does not read any information itself. Think of a passive device as a sign on a wall. Others can read the information, but the sign itself does nothing except transmit the info to authorized devices.
Active devices can read information and send it. An active NFC device, like a smartphone, would not only be able to collect information from NFC tags, but it would also be able to exchange information with other compatible phones or devices and could even alter the information on the NFC tag if authorized to make such changes.
To ensure security, NFC often establishes a secure channel and uses encryption when sending sensitive information such as credit card numbers. Users can further protect their private data by keeping anti-virus software on their smartphones and adding a password to the phone so a thief cannot use it in the event that the smartphone is lost or stolen. For more information on the specifications and different forms of NFC technology, view the rest of our technology pages
OK Lets Start Coding ....
This example send Uri between Android devices using NFC.
To using NFC on your Android app, modify AndroidManifest.xml.
To using NFC on your Android app, modify AndroidManifest.xml.
- This example target minSdkVersion="16"
- Add permission of "android.permission.NFC"
- Add intent-filter of "android.nfc.action.NDEF_DISCOVERED"
Make A new project in your android studio and then Add Permission in Manifest file
<uses-permission android:name="android.permission.NFC"/>
After Add permission Add Intent Filter in MainActivity in manifest file
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Complete manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rgg.rahulgoswami.nfcdemo">
<uses-permission android:name="android.permission.NFC"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
Now Start Java Class Code . we have to use these three java class so just import it on top your project
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcA;
Complete Code For MainActivity.java
MainActivity.java
package com.rgg.rahulgoswami.nfcdemo;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.NfcA;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
NfcAdapter mAdapter;
IntentFilter[] mFilters;
PendingIntent mPendingIntent;
String tagID;
Activity activity;
void resolveIntent(Intent intent) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
// 1) Parse the intent and get the action that triggered this intent
String action = intent.getAction();
// 2) Check if it was triggered by a tag discovered interruption.
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
// 3) Get an instance of the TAG from the NfcAdapter
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] extraID = tagFromIntent.getId();
StringBuilder sb = new StringBuilder();
for (byte b : extraID) {
sb.append(String.format("%02X", b));
};
tagID = sb.toString();
Log.e("nfc ID", tagID);
Toast.makeText(activity,"NFC TAG IS :"+tagID,Toast.LENGTH_SHORT).show();
};
}// End of method
// Setup a tech list for all NfcF tags String[][] mTechLists = new String[][]
{ new String[] { NfcA.class.getName() } };
Intent intent = getIntent();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity=MainActivity.this;
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef1 = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
//IntentFilter ndef2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
mFilters = new IntentFilter[] {
ndef1,
//ndef2, };
try {
ndef1.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mAdapter = NfcAdapter.getDefaultAdapter(this);
if(mAdapter==null){
Toast.makeText(MainActivity.this,
"nfcAdapter==null, no NFC adapter exists",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"Set Callback(s)",
Toast.LENGTH_LONG).show();
}
if (getIntent() != null){
resolveIntent(getIntent());
}
}
}
Finnally Design Activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.rgg.rahulgoswami.nfcdemo.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
Transfer Data Using Android NFC (Near field communication)
Reviewed by Unknown
on
9:22 PM
Rating:
No comments: