Example usage for android.nfc NfcAdapter ACTION_TAG_DISCOVERED

List of usage examples for android.nfc NfcAdapter ACTION_TAG_DISCOVERED

Introduction

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

Prototype

String ACTION_TAG_DISCOVERED

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

Click Source Link

Document

Intent to start an activity when a tag is discovered.

Usage

From source file:Main.java

@SuppressLint("InlinedApi")
public static IntentFilter getTagFilter() {
    IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    return tag;/*from w ww.  j a  v  a2s.c  o  m*/
}

From source file:root.gast.playground.speech.activation.util.TagWriterExecutionActivity.java

private void doWriting() {
    Intent intent = getIntent();//from  w  w w.ja va 2  s . c  om
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NfcUtil.writeTag(this, createMimeTypeMessage(), detectedTag);
    }
    finish();
}

From source file:com.macleod2486.androidswissknife.components.NFCTool.java

private void setUpWrite(String message) {

    Log.i("NFCTool", "Message received " + message);
    Intent nfcIntent = new Intent(activity.getApplicationContext(), NFCActivity.class);
    nfcIntent.putExtra("NFCMode", "write");
    nfcIntent.putExtra("NFCMessage", message);
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, nfcIntent, 0);

    IntentFilter filter = new IntentFilter();
    filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);

    IntentFilter[] filterArray = new IntentFilter[] { filter };

    String[][] techListsArray = new String[][] { new String[] { Ndef.class.getName() },
            new String[] { Ndef.class.getName() } };

    adapter.disableReaderMode(activity);
    adapter.enableForegroundDispatch(activity, pendingIntent, filterArray, techListsArray);

    Toast.makeText(this.activity, "Please scan tag with device.", Toast.LENGTH_LONG).show();
}

From source file:org.ndeftools.NfcDetectorActivity.java

/**
 * /*from   ww  w.  j ava 2 s  . c  o m*/
 * Initialize Nfc fields
 * 
 */

protected void initializeNfc() {
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    writeTagFilters = new IntentFilter[] { ndefDetected, tagDetected, techDetected };
}

From source file:com.nfc.gemkey.MainActivity.java

@Override
public void onCreate(Bundle icicle) {
    if (DEBUG)//from   w  w  w .  j ava  2  s.co m
        Log.d(TAG, "onCreate");
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
    // will fill in the intent with the details of the discovered tag before delivering to
    // this activity.
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup an intent filter for all MIME based dispatches
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    mFilters = new IntentFilter[] { tagDetected };

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    if (getLastNonConfigurationInstance() != null) {
        Log.i(TAG, "open usb accessory@onCreate");
        mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
        openAccessory(mAccessory);
    }

    buttonLED = (ToggleButton) findViewById(R.id.nfc_btn);
    buttonLED.setBackgroundResource(
            buttonLED.isChecked() ? R.drawable.btn_toggle_yes : R.drawable.btn_toggle_no);
    buttonLED.setOnCheckedChangeListener(mKeyLockListener);

    tagId = (TextView) findViewById(R.id.nfc_tag);
    tagId.setText(R.string.nfc_scan_tag);

    // Avoid NetworkOnMainThreadException
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

From source file:mai.whack.StickyNotesActivity.java

/** Called when the activity is first created. */
@Override// w w  w  .j  a  va  2s  .com
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    setContentView(R.layout.main);
    findViewById(R.id.write_tag).setOnClickListener(mTagWriter);

    // Handle all of our received NFC intents in this activity.
    mNfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, 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 {
        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 };
}

From source file:org.sufficientlysecure.keychain.ui.PassphraseWizardActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getActionBar() != null) {
        getActionBar().setTitle(R.string.unlock_method);
    }//from   w w  w.  j a  va 2  s. co m

    selectedAction = getIntent().getAction();
    if (savedInstanceState == null) {
        SelectMethods selectMethods = new SelectMethods();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragmentContainer, selectMethods).commit();
    }
    setContentView(R.layout.passphrase_wizard);

    adapter = NfcAdapter.getDefaultAdapter(this);
    if (adapter != null) {
        pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, PassphraseWizardActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
        writeTagFilters = new IntentFilter[] { tagDetected };
    }
}

From source file:org.croudtrip.activities.MainActivity.java

@Override
protected void onNewIntent(Intent intent) {
    if (intent.getAction() != null && intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        //user scanned an NFC tag -> notify the passenger driving UI to save new status and maybe update UI
        Intent startingIntent = new Intent(Constants.EVENT_NFC_TAG_SCANNED);
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(startingIntent);
    } else {/*from w w  w  . jav a  2s .  c  om*/
        Timber.d("Another Intent or detected some other NFC stuff...");
    }
}

From source file:mai.whack.StickyNotesActivity.java

@Override
protected void onNewIntent(Intent intent) {
    // NDEF exchange mode
    if (!mWriteMode && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        onDataRead(intent);//  w w  w .  j  av a2  s . c  o  m
    }

    // Tag writing mode
    if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        onDataWrite(intent);
    }
}

From source file:mai.whack.StickyNotesActivity.java

private void onDataRead(Intent intent) {
    // Parse the intent
    NdefMessage[] msgs = null;/*from   w  w w  . j a va  2  s .  c  om*/
    String action = intent.getAction();
    byte[] tagId = null;
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        tagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        } else {
            // Unknown tag type
            byte[] empty = new byte[] {};
            NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
            NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
            msgs = new NdefMessage[] { msg };
        }
    } else {
        Log.d(TAG, "Unknown intent.");
        finish();
    }

    String msg = new String(msgs[0].getRecords()[0].getPayload());
    mHttpGetThread = new HttpGetThread("http://192.168.1.192/store/" + toHex(tagId));
    mHttpGetThread.start();
    // mHttpPostThread = new HttpPostThread("aaaaa", "bbbbb", "sdgsdfdsfs");
    // mHttpPostThread.start();
}