Example usage for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED

List of usage examples for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED

Introduction

In this page you can find the example usage for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED.

Prototype

String ACTION_NDEF_DISCOVERED

To view the source code for android.nfc NfcAdapter ACTION_NDEF_DISCOVERED.

Click Source Link

Document

Intent to start an activity when a tag with NDEF payload is discovered.

Usage

From source file:net.lp.hivawareness.v4.HIVAwarenessActivity.java

/** Called when the activity is first created. */
@Override/*w  w  w. ja va 2  s  .  co m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    //Save ApplicationContext
    applicationContext = this.getApplicationContext();

    enableStrictMode();

    if (DEBUG && mStrictModeAvailable) {
        StrictModeWrapper.allowThreadDiskWrites();//disables temporarily, reenables below
    }

    if (!DEBUG) {
        //BugSense uncaught exceptions analytics.
        BugSenseHandler.setup(this, BUGSENSE_API_KEY);
    }

    //Enable analytics session. Should be first (at least before any Flurry calls).
    if (!HIVAwarenessActivity.DEBUG)
        FlurryAgent.onStartSession(this, HIVAwarenessActivity.FLURRY_API_KEY);

    if (mBackupManagerAvailable) {
        mBackupManagerWrapper = new BackupManagerWrapper(this);//TODO: should this be app context?
        Log.i(TAG, "BackupManager API available.");
    } else {
        Log.i(TAG, "BackupManager API not available.");
    }

    //Load the default preferences values. Should be done first at every entry into the app.
    PreferenceManager.setDefaultValues(HIVAwarenessActivity.getAppCtxt(), PREFS, MODE_PRIVATE,
            R.xml.preferences, false);
    HIVAwarenessActivity.dataChanged();

    //Obtain a connection to the preferences.
    SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);

    mGender = Gender.valueOf(prefs.getString(PREFS_GENDER, "male"));

    String region = prefs.getString(PREFS_REGION, null);
    if (region == null) {
        mRegion = null;
    }
    caught = prefs.getInt(PREFS_INFECTED, -1);

    if (caught == -1) {
        calculateInitial(mRegion == null);
    } else {

    }

    // Check for available NFC Adapter
    mNfcAdapter = NfcAdapter.getDefaultAdapter(HIVAwarenessActivity.getAppCtxt());
    if (mNfcAdapter == null) {
        Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
        if (!DEBUG)
            finish();
    }
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/net.lp.hivawareness.beam");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
    mIntentFiltersArray = new IntentFilter[] { ndef };
    mTechListsArray = new String[][] { new String[] { NfcF.class.getName() } };
}

From source file:com.hivewallet.androidclient.wallet.ui.WalletActivity.java

private void handleIntent(@Nonnull final Intent intent) {
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final String inputType = intent.getType();
        final NdefMessage ndefMessage = (NdefMessage) intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
        final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);

        new BinaryInputParser(inputType, input) {
            @Override/*from   www .  j a va  2  s . co  m*/
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
            }
        }.parse();
    }
}

From source file:org.ndeftools.NfcDetectorActivity.java

/**
 * /*from  ww w .jav  a2  s  . c  o  m*/
 * Process the current intent, looking for NFC-related actions
 * 
 */

public void processIntent() {
    Intent intent = getIntent();
    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Log.d(TAG, "Process NDEF discovered action");

        nfcIntentDetected(intent, NfcAdapter.ACTION_NDEF_DISCOVERED);
    } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        Log.d(TAG, "Process APP_TAG discovered action");

        nfcIntentDetected(intent, NfcAdapter.ACTION_TAG_DISCOVERED);
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        Log.d(TAG, "Process TECH discovered action");

        nfcIntentDetected(intent, NfcAdapter.ACTION_TECH_DISCOVERED);
    } else {
        Log.d(TAG, "Ignore action " + intent.getAction());
    }
}

From source file:io.atrac613.AbstractNfcTagFragment.java

/**
 * ???/* w  w w  .  j a  va2  s. co  m*/
 * @param intent ?????????
 */
public void onNewIntent(Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, "incomming intent action = " + action);

    //TECHDISCOVERED
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
            || NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
        mNfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        if (mNfcTag != null) {
            String[] tagTechs = mNfcTag.getTechList();
            for (String[] filterTechs : mTechList) {
                if (ArrayUtil.containArray(tagTechs, filterTechs)) {
                    Log.d(TAG, "** nfcTag = " + mNfcTag.toString());
                    for (INfcTagListener listener : mListnerList) {
                        //?
                        listener.onTagDiscovered(intent, mNfcTag, this);
                    }
                }
            }
        }
    }
}

From source file:net.eledge.android.europeana.gui.activity.SearchActivity.java

@Override
protected void onResume() {
    super.onResume();
    getSupportActionBar().setTitle(searchController.getSearchTitle(this));
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        handleIntent(getIntent());//from   www  .j a v  a2  s . co  m
    }
}

From source file:de.schildbach.wallet.ui.WalletActivity.java

private void handleIntent(final Intent intent) {
    final String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        final String inputType = intent.getType();
        final NdefMessage ndefMessage = (NdefMessage) intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
        final byte[] input = Nfc.extractMimePayload(Constants.MIMETYPE_TRANSACTION, ndefMessage);

        new BinaryInputParser(inputType, input) {
            @Override//from  w  ww  . j  ava 2  s  .  co  m
            protected void handlePaymentIntent(final PaymentIntent paymentIntent) {
                cannotClassify(inputType);
            }

            @Override
            protected void error(final int messageResId, final Object... messageArgs) {
                dialog(WalletActivity.this, null, 0, messageResId, messageArgs);
            }
        }.parse();
    }
}

From source file:edu.cmu.mpcs.dashboard.TagViewer.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.tag_viewer);

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    Intent intent = this.getIntent();
    writeContent = intent.getStringExtra("writeContent");

    mLoginButton = new LoginButton(getApplicationContext());

    // if(flag == true)

    Log.d("TAG_VIEWER", "writeConent:" + writeContent);
    // setContentView(R.layout.tag_viewer);
    mNfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    // mTagContent = (LinearLayout) findViewById(R.id.list);
    // mTitle = (TextView) findViewById(R.id.title);

    Log.d("TAG_VIEWER", "Utility.mFacebook is " + Utility.mFacebook);
    if (Utility.mFacebook == null) {
        InitializeLogin = true;/*from  www .  j  a va  2 s  .  c  o  m*/

    }
    // Create the Facebook Object using the app id.
    edu.cmu.mpcs.dashboard.Utility.mFacebook = new Facebook(APP_ID);
    // Instantiate the asynrunner object for asynchronous api calls.
    edu.cmu.mpcs.dashboard.Utility.mAsyncRunner = new AsyncFacebookRunner(
            edu.cmu.mpcs.dashboard.Utility.mFacebook);

    Log.d("TAG_VIEWER", "in oncreate in TagViewer is session valid?:" + Utility.mFacebook.isSessionValid());

    mLoginButton.init(this, AUTHORIZE_ACTIVITY_RESULT_CODE, Utility.mFacebook, permissions);

    if (APP_ID == null) {
        Util.showAlert(this, "Warning",
                "Facebook Applicaton ID must be " + "specified before running this example: see FbAPIs.java");
        return;
    }

    SessionStore.restore(Utility.mFacebook, this);
    SessionEvents.addAuthListener(new FbAPIsAuthListener());
    SessionEvents.addLogoutListener(new FbAPIsLogoutListener());
    // SessionEvents.r

    if (Utility.mFacebook.isSessionValid()) {
        requestUserData();

    }

    // TODO do something about - dont call resolve intent by
    // default.
    if (writeContent == null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Log.d("TAG_VIEWER", "in OnCreate Before reading");
        loginIntent = intent;

        if (InitializeLogin) { /*
                               * This is if we come to tag viewer without
                               * having started dashboard activity
                               */
            mLoginButton.initializeFbLogin();
        } else { /* This is we if read the tag while in dashboard! */
            resolveIntent(intent);

        }
        // mLoginButton.performLogout();

    } else {
        Log.d("TAG_VIEWER", "Before creating alert dialog");
        builder = new AlertDialog.Builder(TagViewer.this);
        dialog = builder.create();
        builder.setTitle("Touch tag to write").setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                Log.d("TAG_VIEWER", "On cancel called");
                disableTagWriteMode();

            }
        }).create();
        dialog.setTitle("touch tag to write");
        dialog.show();
        dialog.setOnDismissListener(new OnDismissListener() {

            public void onDismiss(DialogInterface dialog) {
                // TODO Auto-generated method stub
                Log.d("TAG_VIEWER", "On dismiss called");
                disableTagWriteMode();

            }
        });
        // builder.setCancelable(true);
        // builder.setOnCancelListener(new
        // DialogInterface.OnCancelListener() {
        // public void onCancel(DialogInterface dialog) {
        // Log.d("TAG_VIEWER","On cancel called");
        // disableTagWriteMode();
        //
        // }
        // }).create().show();
    }

    // Tag writing mode
    if (writeContent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Log.d("TAG_VIEWER", "in Oncreate Before writing");

        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        try {
            WriteToTag.write(detectedTag, writeContent);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.d("TAG_VIEWER", "in on create, writing to tag");
    }
    Log.d("TAG_VIEWER", "in create");

}

From source file:de.stadtrallye.rallyesoft.ConnectionAssistantActivity.java

@Override
protected void onResume() {
    super.onResume();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processNfcIntent(getIntent());//from   w  w  w.  j  av a 2  s  .c o  m
    }
}

From source file:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);// w  ww.  j a v  a  2  s. co  m

    IntentFilter[] filters = new IntentFilter[1];
    String[][] techList = new String[][] {};
    filters[0] = new IntentFilter();
    filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filters[0].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        filters[0].addDataType(MIME_TEXT_PLAIN);
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Check your mime type.");
    }
    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

From source file:net.eledge.android.europeana.gui.activity.RecordActivity.java

@Override
protected void onResume() {
    super.onResume();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        handleIntent(getIntent());//from w w w.j  a  v  a 2  s  .  c o m
    }
}