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:com.evandroid.musica.MainLyricActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    String extra = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (action != null) {
        switch (action) {
        case NfcAdapter.ACTION_NDEF_DISCOVERED:
            Lyrics receivedLyrics = getBeamedLyrics(intent);
            if (receivedLyrics != null)
                updateLyricsFragment(0, 0, false, receivedLyrics);
            break;
        case "android.intent.action.SEND":
            LyricsViewFragment lyricsViewFragment = (LyricsViewFragment) getFragmentManager()
                    .findFragmentByTag(LYRICS_FRAGMENT_TAG);
            new IdDecoder(this, lyricsViewFragment).execute(getIdUrl(extra));
            break;
        case "com.evandroid.music.getLyrics":
            String[] metadata = intent.getStringArrayExtra("TAGS");
            if (metadata != null) {
                String artist = metadata[0];
                String track = metadata[1];
                LyricsViewFragment lyricsFragment = (LyricsViewFragment) getFragmentManager()
                        .findFragmentByTag(LYRICS_FRAGMENT_TAG);
                lyricsFragment.fetchLyrics(artist, track);
            }//from  ww w. j  a  v a2  s.  c o m
            break;
        case "com.evandroid.music.updateDBList":
            updateDBList();
            break;
        }
    }
}

From source file:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java

@SuppressLint("NewApi")
public void launchNFC() {
    Log.d(TAG, "launchNFC:" + beamStat);
    if (beamStat == 0) {
        _nfcAdapter.setNdefPushMessageCallback(this, getActivity());
        _nfcAdapter.setOnNdefPushCompleteCallback(this, getActivity());

        // start NFC Connection
        /*_nfcPendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0,
              new Intent(getActivity().getApplicationContext(), FutureActivity.class)
             .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
                //w ww.j a va  2 s. c  o  m
        IntentFilter ndefDetected = new IntentFilter(
              NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
           ndefDetected.addDataType("application/com.hipipal.qpy.nfc");
        } catch (MalformedMimeTypeException e) {
           throw new RuntimeException("Could not add MIME type.", e);
        }
                
        _readTagFilters = new IntentFilter[] { ndefDetected };*/
    } else if (beamStat == 1) {
        //_nfcAdapter.setNdefPushMessageCallback(this, getActivity());
        //_nfcAdapter.setOnNdefPushCompleteCallback(this, getActivity());

        // start NFC Connection
        _nfcPendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0,
                new Intent(getActivity().getApplicationContext(), FutureActivity.class)
                        .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                0);

        IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndefDetected.addDataType("application/com.hipipal.qpy.nfc");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("Could not add MIME type.", e);
        }

        _readTagFilters = new IntentFilter[] { ndefDetected };
    }
}

From source file:it.imwatch.nfclottery.MainActivity.java

/**
 * Handles an intent as received by the Activity, be it with a MAIN or an
 * NDEF_DISCOVERED action./* w ww .j  av a 2 s  . c o  m*/
 *
 * @param intent The intent to handle
 */
private void handleIntent(Intent intent) {
    String action = intent.getAction();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        // This is an "NFC tag scanned" intent
        String type = intent.getType();
        Log.d(TAG, "Read tag with type: " + type);

        if (MIME_VCARD.equals(type) || MIME_XVCARD.equals(type)) {
            if (mVCardEngine == null) {
                mVCardEngine = new VCardEngine();
            }

            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (tag != null) {
                Ndef ndefTag = Ndef.get(tag);
                NdefMessage ndefMessage;

                // ndefTag can be null if the tag was INITIALIZED
                // but not actually written to
                if (ndefTag != null) {
                    ndefMessage = ndefTag.getCachedNdefMessage();
                    parseAndInsertVCard(ndefMessage);
                }
            }
        } else {
            Log.i(TAG, "Ignoring NDEF with mime type: " + type);
        }
    }

    // Nothing else to do if this is a MAIN intent...
}

From source file:com.github.vgoliveira.panificadora.MainActivity.java

private void nfc_init() {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        NdefMessage[] msgs = null;/*from   w w w  .j a  v a  2s. com*/
        Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        String deviceAddress;

        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; ++i) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
            NdefRecord[] records = msgs[0].getRecords();
            deviceAddress = new String(records[1].getPayload(), 1, records[1].getPayload().length - 1,
                    Charset.forName("UTF-8")); // record 1 contains the MAC Address
            deviceAddress = deviceAddress.substring(2); // remove the language mark "en" coded in the NDEF text/plain record
            mDevice = mBtAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress); // no testa s retorna mDevice com o endereo passado
            ((TextView) findViewById(R.id.deviceName)).setText(mDevice.getName() + " - connecting");
            mService.connect(deviceAddress);
            Log.d(TAG, "... NFC_init mDevice= " + mDevice + " mService= " + mService);
        }
    }
}

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

void resolveIntent(Intent intent) {
    // Parse the intent
    String action = intent.getAction();
    Log.d("TAG_VIEWER", "in resolve intent");
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        // When a tag is discovered we send it to the service to
        // be save. We
        // include a PendingIntent for the service to call back
        // onto. This
        // will cause this activity to be restarted with
        // onNewIntent(). At
        // that time we read it from the database and view it.
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;/*  w  ww .java 2  s.c  o m*/
        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 };
        }
        // Setup the views
        setTitle(R.string.title_scanned_tag);
        buildTagViews(msgs);
    } else {
        Log.e(TAG, "Unknown intent " + intent);
        // finish();
        return;
    }
}

From source file:de.uni_koblenz_landau.apow.LoginActivity.java

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);//from   w ww . j a  v  a  2 s .  c o m
    String action = intent.getAction();

    // If tag is detected, start reading it.
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) && MIME_TEXT_PLAIN.equals(intent.getType())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        readNFCTag(tag);
    }
}

From source file:foundme.uniroma2.it.professore.HomeActivity.java

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

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

        String type = intent.getType();
        if (Variables_it.MIME_TEXT_PLAIN.equals(type)) {

            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            new NdefReaderTask().execute(tag);

        }//  w  ww .  ja v  a2s. c  o  m
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

        // In questo caso usermo la Tech Discovered Intent
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        String[] techList = tag.getTechList();
        String searchedTech = Ndef.class.getName();

        for (String tech : techList) {
            if (searchedTech.equals(tech)) {
                new NdefReaderTask().execute(tag);
                break;
            }
        }
    }
}

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

private void handleIntent(Intent intent) {
    String id = null;// w w  w.  j  a  va  2s . c o  m
    if (intent != null) {
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            // only one message sent during the beam
            NdefMessage msg = (NdefMessage) rawMsgs[0];
            // record 0 contains the MIME type, record 1 is the AAR, if present
            id = new String(msg.getRecords()[0].getPayload());
        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            id = StringUtils.defaultIfBlank(intent.getDataString(), intent.getStringExtra(RECORD_ID));
        }
        if (StringUtils.contains(id, "europeana.eu/")) {
            Uri uri = Uri.parse(id);
            List<String> paths = uri.getPathSegments();
            if ((paths != null) && (paths.size() == 4)) {
                String collectionId = paths.get(paths.size() - 2);
                String recordId = StringUtils.removeEnd(paths.get(paths.size() - 1), ".html");
                id = StringUtils.join("/", collectionId, "/", recordId);
            } else {
                // invalid url/id, cancel opening record
                id = null;
            }
        }
        if (StringUtils.isNotBlank(id)) {
            openRecord(id);
        }
    }
}

From source file:org.protocoderrunner.apprunner.AppRunnerActivity.java

public void initializeNFC() {

    if (nfcInit == false) {
        PackageManager pm = getPackageManager();
        nfcSupported = pm.hasSystemFeature(PackageManager.FEATURE_NFC);

        if (nfcSupported == false) {
            return;
        }/*w  w  w.  j  a  va2s .co  m*/

        // cuando esta en foreground
        MLog.d(TAG, "Starting NFC");
        mAdapter = NfcAdapter.getDefaultAdapter(this);
        /*
         * mAdapter.setNdefPushMessageCallback(new
         * NfcAdapter.CreateNdefMessageCallback() {
         *
         * @Override public NdefMessage createNdefMessage(NfcEvent event) {
         * // TODO Auto-generated method stub return null; } }, this, null);
         */

        // Create mContext 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 ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        mFilters = new IntentFilter[] { ndef, };

        // Setup mContext tech list for all NfcF tags
        mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
        nfcInit = true;
    }
}