What is NFC?
Near Field Communication is full form of NFC. NFC is a contactless exchange of data. Wireless LAN and Bluetooth are wide range of technologies but in NFC, maximum distance of two devices is 10 cm.
NFC Data Exchange Format (NDEF) is used in following two manners:
- Reading NDEF data from an NFC tag
- Beaming NDEF messages from one device to another with Android Beam
NFC tag contains NDEF data. NFC tag can be ready by tag dispatch system, which analyzes discovered NFC tag, categorizes data and start application that is interested in categorized data. Interested application can handle scanned NFC tag and declare an intent filter and request to handle the data.
The Android Beam feature allows a device to send an NDEF message to another device by gathering two devices together. This is an easier way to send data compared to other wireless technologies like Bluetooth, because when we are using NFC there is no need of pairing or device discovery. Two devices connect automatically when they come into range. Any application is able to transmit data between devices using android beam which is available through a set of NFC.For example, the Browser, contacts, and YouTube applications use Android Beam to share web pages, and contacts with other devices.
Now we will discuss about How to share file or data using Android Beam :-
In this blog, you will learn the basics about P2P (peer to peer) communication and create an application which can share large files, such as videos and images, from one device to another using NFC.
Following are requirements of the Android Beam file transfer:
- Android Beam file transfer for large files is available only in Android 4.1 (API level 16) and higher.
- Files you want to transfer must have in external storage.
- Each file you want to transfer must have to be world-readable,So you can set this permission by File.setReadable(true, false);
- You can transfer a file by providing file URI.
We can start with creating new project and selecting blank activity in it. You should have to select a minimum SDK version of API level 16, because Android Beam file transfer is supported from Android 4.1 (API level 16) and above.Don’t forget to choose your own package name, I’ve chosen com.yudiz.nfcdemo,because Yudiz.com is the domain of my website(http://www.yudiz.com)and the other part refers to the topic of this application which is nfcdemo.
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19" />
To get access of the NFC, you must have to set permission in manifest.If the NFC is compulsory for an application then you can also provide uses-feature tag in manifest.If NFC is compulsory for an application then it can not be installed on Non NFC devices as google play displays that application to users who have an NFC supported devices.
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
You can allow your application to read data from external storage. By specifying following permission, add this permission in your manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your activity_main.xml file put following code.There is nothing in it. Just one simple button is placed in relative layout and user can share file via clicking that share file button.
<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:padding="16dp" > <Button android:id="@+id/act_main_btn_sendfile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dp" android:text="Send File" /> </RelativeLayout>
You can communicate with NFC via the NfcAdapter class.If NFCAdapter is null then device doesn’t support NFC.
package com.yudiz.nfcdemo;
…
public class MainActivity extends Activity implements OnClickListener { private NfcAdapter nfcAdapter; private Button btnSendFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); PackageManager pm = this.getPackageManager(); // Check if NFC is available on device if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) { // NFC isn’t available on the device. Toast.makeText(this, "The device does not has NFC hardware.", Toast.LENGTH_SHORT).show(); } // Check if device is running Android 4.1 or higher else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { // Android Beam feature isn’t supported. Toast.makeText(this, "Android Beam isn’t supported.", Toast.LENGTH_SHORT).show(); } else { // NFC and Android Beam file transfer is supported. Toast.makeText(this, "Android Beam is supported.", Toast.LENGTH_SHORT).show(); } } private void initUI() { btnSendFile = (Button) findViewById(R.id.act_main_btn_sendfile); btnSendFile.setOnClickListener(this); } @Override public void onClick(View v) { }
If we run our app now then we can find the text whether NFC is enabled or disabled and Android Beam is supported on your device or not.
Now after this if NFC is not enabled on your device then you can make it enable. Android Beam will automatically enable once you enable NFC. If Android Beam is not enabled then you can make it enable from settings of your device.
Once you are sure whether your device supports Android Beam file transfer,By adding callback method system will know that android beam want to send file to another android device. In this callback method, return an array of Uri objects. Android beam send a copy of file given by these uris and send it to receiving device.You must also have permanent read access for the file to transfer a file.
public void sendFile() { nfcAdapter = NfcAdapter.getDefaultAdapter(this); // Check if NFC is enabled on device if (!nfcAdapter.isEnabled()) { // NFC is disabled, open settings using intent to enable NFC. Toast.makeText(this, "Please enable NFC.", Toast.LENGTH_SHORT) .show(); startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); } // Check if Android Beam feature is enabled on device else if (!nfcAdapter.isNdefPushEnabled()) { /* Android Beam is disabled, open settings using intent to enable AndroidBeam */ Toast.makeText(this, "Please enable Android Beam.", Toast.LENGTH_SHORT).show(); startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS)); } else { // Android Beam and NFC both are enabled String fName = "yudizNFCDemo.jpg"; // Get path of user's public pictures directory File fDirectory = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // Create a file using the specified directory and name File fToTransfer = new File(fDirectory, fName); fToTransfer.setReadable(true, false); nfcAdapter.setBeamPushUris( new Uri[] { Uri.fromFile(fToTransfer) }, this); } }
Android Beam can send one or more uris using setBeamPushUris.Each file you want to transfer must have to be world-readable,So you can set this permission by File.setReadable(true, false);
Here is full source code of file sharing using Android beam:-
package com.yudiz.nfcdemo; ... public class MainActivity extends Activity implements OnClickListener { private NfcAdapter nfcAdapter; private Button btnSendFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); PackageManager pm = this.getPackageManager(); // Check if NFC is available on device if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) { // NFC is not available on the device. Toast.makeText(this, "The device doesn’t have NFC.", Toast.LENGTH_SHORT).show(); } // Check if device is running on Android API 4.1 or higher else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { // Android Beam is not supported. Toast.makeText(this, "Android Beam isn’t supported.", Toast.LENGTH_SHORT).show(); } else { // NFC and Android Beam file transfer is supported. Toast.makeText(this, "Android Beam is supported.", Toast.LENGTH_SHORT).show(); } } private void initUI() { btnSendFile = (Button) findViewById(R.id.act_main_btn_sendfile); btnSendFile.setOnClickListener(this); } public void sendFile() { nfcAdapter = NfcAdapter.getDefaultAdapter(this); // Check if NFC is enabled on device if (!nfcAdapter.isEnabled()) { // NFC is disabled, open settings using intent to enable NFC Toast.makeText(this, "Please enable NFC.", Toast.LENGTH_SHORT) .show(); startActivity(new Intent(Settings.ACTION_NFC_SETTINGS)); } // Check if Android Beam is enabled on device else if (!nfcAdapter.isNdefPushEnabled()) { /* Android Beam is disabled, open settings using intent to enable AndroidBeam */ Toast.makeText(this, "Enable Android Beam.", Toast.LENGTH_SHORT).show(); startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS)); } else { // Android Beam and NFC both are enabled String fName = "yudizNFCDemo.jpg"; // Get path of user's public pictures directory File fDirectory = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); // Create a file using the specified directory and name File fToTransfer = new File(fDirectory, fName); fToTransfer.setReadable(true, false); nfcAdapter.setBeamPushUris( new Uri[] { Uri.fromFile(fToTransfer) }, this); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.act_main_btn_sendfile: sendFile(); break; } } }
Conclusion
In this tutorial I have shown you a very easy way to exchange of data (called Android Beam).You can find more useful tutorials on {Link}.