setup NFC Intent - Android Network

Android examples for Network:NFC

Description

setup NFC Intent

Demo Code


//package com.java2s;

import android.app.Activity;
import android.app.PendingIntent;

import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;

import android.nfc.NfcAdapter;

public class Main {
    private static NfcAdapter mNfcAdapter;
    private static PendingIntent mNfcPendingIntent;
    private static IntentFilter[] mWriteTagFilters;
    private static IntentFilter[] mNdefExchangeFilters;

    public static void setup(Activity activity) {
        mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);

        // Handle all of our received NFC intents in this activity.
        mNfcPendingIntent = PendingIntent.getActivity(activity, 0,
                new Intent(activity, activity.getClass())
                        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

        // Intent filters for reading a note from a tag or exchanging over p2p.
        IntentFilter ndefDetected = new IntentFilter(
                NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {// w  w  w.j  a v  a2 s  . com
            ndefDetected.addDataType("text/plain");
        } catch (MalformedMimeTypeException e) {
        }
        mNdefExchangeFilters = new IntentFilter[] { ndefDetected };

        // Intent filters for writing to a tag
        IntentFilter tagDetected = new IntentFilter(
                NfcAdapter.ACTION_TAG_DISCOVERED);
        mWriteTagFilters = new IntentFilter[] { tagDetected };
    }
}

Related Tutorials