Contact Us

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:

  1. Android Beam file transfer for large files is available only in Android 4.1 (API level 16) and higher.
  2. Files you want to transfer must have in external storage.
  3. Each file you want to transfer must have to be world-readable,So you can set this permission by File.setReadable(true, false);
  4. 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.

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.

You can allow your application to read data from external storage. By specifying following permission, add this permission in your manifest file.

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.

You can communicate with NFC via the NfcAdapter class.If NFCAdapter is null then device doesn’t support NFC.

package com.yudiz.nfcdemo;

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.

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:-

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}.

Disha Shah

Disha Shah | Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a leading mobile app development company and Android programming has been my passion since i started my carrier , learning new technology and doing more complex things has always been interesting part for mine.
mm

Disha Shah | Android Developer

I am an Android Developer at Yudiz Solutions Pvt. Ltd. - a leading mobile app development company and Android programming has been my passion since i started my carrier , learning new technology and doing more complex things has always been interesting part for mine.

Comments are closed.

Top
Yudiz Solutions Pvt. Ltd.