Sets up the NFC adapter to send a list of Uri with 'file' or 'content' scheme. - Android android.nfc

Android examples for android.nfc:NfcAdapter

Description

Sets up the NFC adapter to send a list of Uri with 'file' or 'content' scheme.

Demo Code

/*//from   w w w  .  ja va2 s  .  c  o  m
 * This file is part of Flying PhotoBooth.
 * 
 * Flying PhotoBooth is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Flying PhotoBooth is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Flying PhotoBooth.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Build;

public class Main {
    /**
     * Sets up the NFC adapter to send a list of {@link Uri} with 'file' or 'content' scheme. To clear the NFC adapter
     * of the list of {@link Uri}, null should be passed as the second parameter.
     *
     * @param activity the {@link Activity}.
     * @param uris     the list of {@link Uri} to beam. Pass null to clear NFC adapter.
     */
    public static void beamUris(Activity activity, Uri[] uris) {
        if (activity != null && !activity.isFinishing()) {
            NfcAdapter nfcAdapter = getNfcAdapter(activity);
            if (nfcAdapter != null) {
                try {
                    nfcAdapter.setBeamPushUris(uris, activity);
                } catch (Exception e) {
                    // Do nothing. An exception is thrown if a destroyed Activity is passed.
                }
            }
        }
    }

    /**
     * Gets the default NFC adapter.
     *
     * @param context the {@link Context}.
     * @return the NFC adapter; or null if the device does not support NFC.
     */
    private static NfcAdapter getNfcAdapter(Context context) {
        NfcAdapter nfcAdapter = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            nfcAdapter = NfcAdapter.getDefaultAdapter(context
                    .getApplicationContext());
        }

        return nfcAdapter;
    }
}

Related Tutorials