Skip to content

Most visited

Recently visited

navigation
BeamLargeFiles / src / com.example.android.beamlargefiles /

BeamLargeFilesFragment.java

1
/*
2
* Copyright (C) 2013 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*      http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
 
17
package com.example.android.beamlargefiles;
18
 
19
import android.app.Activity;
20
import android.net.Uri;
21
import android.nfc.NfcAdapter;
22
import android.nfc.NfcEvent;
23
import android.os.Bundle;
24
import android.support.v4.app.Fragment;
25
import android.util.Log;
26
 
27
/**
28
 * This class demonstrates how to use Beam to send files too large to transfer reliably via NFC.
29
 *
30
 * <p>While any type of data can be placed into a normal NDEF messages, NFC is not considered
31
 * "high-speed" communication channel. Large images can easily take > 30 seconds to transfer.
32
 * Because NFC requires devices to be in extremely close proximity, this is not ideal.
33
 *
34
 * <p>Instead, Android 4.2+ devices can use NFC to perform an initial handshake, before handing
35
 * off to a faster communication channel, such as Bluetooth, for file transfer.
36
 *
37
 * <p>The tradeoff is that this application will not be invoked on the receiving device. Instead,
38
 * the transfer will be handled by the OS. The user will be shown a notification when the transfer
39
 * is complete. Selecting the notification will open the file in the default viewer for its MIME-
40
 * type. (If it's important that your application be used to open the file, you'll need to register
41
 * an intent-filter to watch for the appropriate MIME-type.)
42
 */
43
public class BeamLargeFilesFragment extends Fragment implements NfcAdapter.CreateBeamUrisCallback {
44
 
45
    private static final String TAG = "BeamLargeFilesFragment";
46
    /** Filename that is to be sent for this activity. Relative to /assets. */
47
    private static final String FILENAME = "stargazer_droid.jpg";
48
    /** Content provider URI. */
49
    private static final String CONTENT_BASE_URI =
50
            "content://com.example.android.beamlargefiles.files/";
51
 
52
    /**
53
     * Standard lifecycle event. Registers a callback for large-file transfer, by calling
54
     * NfcAdapter.setBeamPushUrisCallback().
55
     *
56
     * Note: Like sending NDEF messages over standard Android Beam, there is also a non-callback
57
     * API available. See: NfcAdapter.setBeamPushUris().
58
     *
59
     * @param savedInstanceState Saved instance state.
60
     */
61
    @Override
62
    public void onCreate(Bundle savedInstanceState) {
63
        super.onCreate(savedInstanceState);
64
        setHasOptionsMenu(true);
65
        Activity a = getActivity();
66
 
67
        // Setup Beam to transfer a large file. Note the call to setBeamPushUrisCallback().
69
        NfcAdapter nfc = NfcAdapter.getDefaultAdapter(a);
70
        if (nfc != null) {
71
            Log.w(TAG, "NFC available. Setting Beam Push URI callback");
72
            nfc.setBeamPushUrisCallback(this, a);
73
        } else {
74
            Log.w(TAG, "NFC is not available");
75
        }
77
    }
78
 
79
    /**
80
     * Callback for Beam events (large file version). The return value here should be an array of
81
     * content:// or file:// URIs to send.
82
     *
83
     * Note that the system must have read access to whatever URIs are provided here.
84
     *
85
     * @param nfcEvent NFC event which triggered callback
86
     * @return URIs to be sent to remote device
87
     */
89
    @Override
90
    public Uri[] createBeamUris(NfcEvent nfcEvent) {
91
        Log.i(TAG, "Beam event in progress; createBeamUris() called.");
92
        // Images are served using a content:// URI. See AssetProvider for implementation.
93
        Uri photoUri = Uri.parse(CONTENT_BASE_URI + FILENAME);
94
        Log.i(TAG, "Sending URI: " + photoUri);
95
        return new Uri[] {photoUri};
96
    }
98
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.