This lesson teaches you to
- Declare Features in the Manifest
- Test for Android Beam File Transfer Support
- Create a Callback Method That Provides Files
- Specify the Files to Send
You should also read
This lesson shows you how to design your app to send large files to another device using Android Beam file transfer. To send files, you request permission to use NFC and external storage, test to ensure your device supports NFC, and provide URIs to Android Beam file transfer.
The Android Beam file transfer feature has the following requirements:
- Android Beam file transfer for large files is only available in Android 4.1 (API level 16) and higher.
- Files you want to transfer must reside in external storage. To learn more about using external storage, read Using the External Storage.
-
Each file you want to transfer must be world-readable. You can set this permission by
calling the method
File.setReadable(true,false)
. -
You must provide a file URI for the files you want to transfer. Android Beam file transfer
is unable to handle content URIs generated by
FileProvider.getUriForFile
.
Declare Features in the Manifest
First, edit your app manifest to declare the permissions and features your app needs.
Request Permissions
To allow your app to use Android Beam file transfer to send files from external storage using NFC, you must request the following permissions in your app manifest:
-
NFC
-
Allows your app to send data over NFC. To specify this permission, add the following element
as a child of the
<manifest>
element:<uses-permission android:name="android.permission.NFC" />
-
READ_EXTERNAL_STORAGE
-
Allows your app to read from external storage. To specify this permission, add the following
element as a child of the
<manifest>
element:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Note: As of Android 4.2.2 (API level 17), this permission is not enforced. Future versions of the platform may require it for apps that want to read from external storage. To ensure forward compatibility, request the permission now, before it becomes required.
Specify the NFC feature
Specify that your app uses NFC, by adding a
<uses-feature>
element as a child
of the <manifest>
element. Set the android:required
attribute to
true
to indicate that your app won't function unless NFC is present.
The following snippet shows you how to specify the
<uses-feature>
element:
<uses-feature android:name="android.hardware.nfc" android:required="true" />
Note that if your app only uses NFC as an option, but still functions if NFC isn't present, you
should set android:required
to false
, and test for NFC in code.
Specify Android Beam file transfer
Since Android Beam file transfer is only available in Android 4.1 (API level 16) and later,
if your app depends on Android Beam file transfer for a key part of its functionality you must
specify the <uses-sdk>
element with the
android:minSdkVersion="16"
attribute. Otherwise, you can set
android:minSdkVersion
to another value as necessary, and test for the platform
version in code, as described in the following section.
Test for Android Beam File Transfer Support
To specify in your app manifest that NFC is optional, you use the following element:
<uses-feature android:name="android.hardware.nfc" android:required="false" />
If you set the attribute
android:required="false"
, you must test for NFC support and Android Beam file
transfer support in code.
To test for Android Beam file transfer support in code, start by testing that the device
supports NFC by calling PackageManager.hasSystemFeature()
with the argument
FEATURE_NFC
. Next, check that the Android
version supports Android Beam file transfer by testing the value of
SDK_INT
. If Android Beam file transfer is supported, get an
instance of the NFC controller, which allows you to communicate with the NFC hardware.
For example:
public class MainActivity extends Activity { ... NfcAdapter mNfcAdapter; // Flag to indicate that Android Beam is available boolean mAndroidBeamAvailable = false; ... @Override protected void onCreate(Bundle savedInstanceState) { ... // NFC isn't available on the device if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)) { /* * Disable NFC features here. * For example, disable menu items or buttons that activate * NFC-related features */ ... // Android Beam file transfer isn't supported } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { // If Android Beam isn't available, don't continue. mAndroidBeamAvailable = false; /* * Disable Android Beam file transfer features here. */ ... // Android Beam file transfer is available, continue } else { mNfcAdapter = NfcAdapter.getDefaultAdapter(this); ... } } ... }
Create a Callback Method that Provides Files
Once you've verified that the device supports Android Beam file transfer, add a callback
method that the system invokes when Android Beam file transfer detects that the user wants
to send files to another NFC-enabled device. In this callback method, return an array of
Uri
objects. Android Beam file transfer copies the files represented by
these URIs to the receiving device.
To add the callback method, implement the
NfcAdapter.CreateBeamUrisCallback
interface and its method
createBeamUris()
. The
following snippet shows you how to do this:
public class MainActivity extends Activity { ... // List of URIs to provide to Android Beam private Uri[] mFileUris = new Uri[10]; ... /** * Callback that Android Beam file transfer calls to get * files to share */ private class FileUriCallback implements NfcAdapter.CreateBeamUrisCallback { public FileUriCallback() { } /** * Create content URIs as needed to share with another device */ @Override public Uri[] createBeamUris(NfcEvent event) { return mFileUris; } } ... }
Once you've implemented the interface, provide the callback to Android Beam file transfer by
calling setBeamPushUrisCallback()
. The
following snippet shows you how to do this:
public class MainActivity extends Activity { ... // Instance that returns available files from this app private FileUriCallback mFileUriCallback; ... @Override protected void onCreate(Bundle savedInstanceState) { ... // Android Beam file transfer is available, continue ... mNfcAdapter = NfcAdapter.getDefaultAdapter(this); /* * Instantiate a new FileUriCallback to handle requests for * URIs */ mFileUriCallback = new FileUriCallback(); // Set the dynamic callback for URI requests. mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this); ... } ... }
Note: You can also provide the array of Uri
objects
directly to the NFC framework through your app's NfcAdapter
instance. Choose
this approach if you can define the URIs to transfer before the NFC touch event occurs.
To learn more about this approach, see NfcAdapter.setBeamPushUris()
.
Specify the Files to Send
To transfer one or more files to another NFC-enabled device, get a file URI (a URI with a
file
scheme) for each file and then add the URI to an array of
Uri
objects. To transfer a file, you must also have permanent read access
for the file. For example, the following snippet shows you how to get a file URI from a file
name and then add the URI to the array:
/* * Create a list of URIs, get a File, * and set its permissions */ private Uri[] mFileUris = new Uri[10]; String transferFile = "transferimage.jpg"; File extDir = getExternalFilesDir(null); File requestFile = new File(extDir, transferFile); requestFile.setReadable(true, false); // Get a URI for the File and add it to the list of URIs fileUri = Uri.fromFile(requestFile); if (fileUri != null) { mFileUris[0] = fileUri; } else { Log.e("My Activity", "No File URI available for file."); }