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:io.v.android.impl.google.services.beam.BeamActivity.java

@Override
public void onResume() {
    super.onResume();
    NdefMessage msgs[] = null;//from w w w .j a v  a  2  s. c  o  m
    Intent intent = getIntent();

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
            }
        }
    }
    if (msgs == null) {
        Log.d(TAG, "No ndef messages");
        finish();
        return;
    }
    VBeamManager.Data data = null;
    for (NdefMessage m : msgs) {
        data = VBeamManager.decodeMessage(m);
        if (data != null)
            break;
    }
    if (data == null) {
        Log.w(TAG, "Unable to deserialize data");
        finish();
        return;
    }
    Log.d(TAG, "connecting to " + data.name);
    VContext ctx = V.init(this).withTimeout(Duration.standardSeconds(2));
    Options opts = new Options();

    opts.set(OptionDefs.SERVER_AUTHORIZER, VSecurity.newPublicKeyAuthorizer(data.key));
    IntentBeamerClient client = IntentBeamerClientFactory.getIntentBeamerClient(data.name);
    ListenableFuture<IntentBeamerClient.GetIntentOut> out = client.getIntent(ctx, data.secret, opts);
    Futures.addCallback(out, new FutureCallback<IntentBeamerClient.GetIntentOut>() {
        @Override
        public void onSuccess(IntentBeamerClient.GetIntentOut result) {
            try {
                Log.d(TAG, "got intent " + result.intentUri);
                int flags = 0;
                if (result.intentUri.startsWith("intent:")) {
                    flags = Intent.URI_INTENT_SCHEME;
                } else {
                    flags = Intent.URI_ANDROID_APP_SCHEME;
                }
                Intent resultIntent = Intent.parseUri(result.intentUri, flags);
                resultIntent.putExtra(VBeamManager.EXTRA_VBEAM_PAYLOAD, result.payload);
                startActivity(resultIntent);
                finish();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace();
            finish();
        }
    });
}

From source file:com.saarang.samples.apps.iosched.ui.NfcBadgeActivity.java

@Override
public void onStart() {
    super.onStart();
    AnalyticsManager.initializeAnalyticsTracker(getApplicationContext());
    // Check for NFC data
    Intent i = getIntent();//ww  w .jav  a  2s .c o  m
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        LogUtils.LOGI(TAG, "Badge detected");
        /* [ANALYTICS:EVENT]
         * TRIGGER:   Scan another attendee's badge.
         * CATEGORY:  'NFC'
         * ACTION:    'Read'
         * LABEL:     'Badge'. Badge info IS NOT collected.
         * [/ANALYTICS]
         */
        AnalyticsManager.sendEvent("NFC", "Read", "Badge");
        readTag((Tag) i.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    } else if (ACTION_SIMULATE.equals(i.getAction()) && Config.IS_DOGFOOD_BUILD) {
        String simulatedUrl = i.getDataString();
        LogUtils.LOGD(TAG, "Simulating badge scanning with URL " + simulatedUrl);
        // replace https by Unicode character 4, as per normal badge encoding rules
        recordBadge(simulatedUrl.replace("https://", "\u0004"));
    } else {
        LogUtils.LOGW(TAG, "Invalid action in Intent to NfcBadgeActivity: " + i.getAction());
    }
    finish();
}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

@Override
public void onStart() {
    super.onStart();
    AnalyticsManager.initializeAnalyticsTracker(getApplicationContext());
    // Check for NFC data
    Intent i = getIntent();//from   w w w  .j  a va 2  s .  c o m
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        LOGI(TAG, "Badge detected");
        /* [ANALYTICS:EVENT]
         * TRIGGER:   Scan another attendee's badge.
         * CATEGORY:  'NFC'
         * ACTION:    'Read'
         * LABEL:     'Badge'. Badge info IS NOT collected.
         * [/ANALYTICS]
         */
        AnalyticsManager.sendEvent("NFC", "Read", "Badge");
        readTag((Tag) i.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    } else if (ACTION_SIMULATE.equals(i.getAction()) && Config.IS_DOGFOOD_BUILD) {
        String simulatedUrl = i.getDataString();
        LOGD(TAG, "Simulating badge scanning with URL " + simulatedUrl);
        // replace https by Unicode character 4, as per normal badge encoding rules
        recordBadge(simulatedUrl.replace("https://", "\u0004"));
    } else {
        LOGW(TAG, "Invalid action in Intent to NfcBadgeActivity: " + i.getAction());
    }
    finish();
}

From source file:com.commonsware.android.webbeam.WebBeamActivity.java

private void handleIntent(Intent i) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        Parcelable[] rawMsgs = i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        String url = new String(msg.getRecords()[0].getPayload());

        beamFragment.loadUrl(url);/*from   w ww .  j  a v a  2  s . c  om*/
    }
}

From source file:org.thinkfree.axihome.NFC.TagViewer.java

void resolveIntent(Intent intent) {

    Log.e(TAG, "Tag detected");
    String action = intent.getAction();

    Log.e(TAG, action);//from  www  .j a v  a2  s . c  o  m

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

        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage[] msgs;

        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
            Log.e(TAG, "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
        processMessage(msgs);
    } else {

        Log.e(TAG, "Unknown intent " + intent);
        finish();
        return;
    }
}

From source file:io.v.example.vbeam.vbeamexample.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    Intent intent = getIntent();/*from www. j a v a2  s.  co m*/
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            for (int i = 0; i < rawMsgs.length; i++) {
                for (NdefRecord r : ((NdefMessage) rawMsgs[i]).getRecords()) {
                    System.out.println("record " + r.toString() + "(" + r.toUri() + ")");
                }
            }
        }
    }
}

From source file:com.villetainio.travelcardreminder.activities.MainActivity.java

public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    final Intent intent = new Intent(activity.getApplicationContext(), ReadCardActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,
            0);//from  w w w .j a  va2  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("*/*"); //TODO Use only needed MIME based dispatches.
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("MIME type not supported.");
    }

    adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
}

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:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (SharedPreferencesManager.getLoggedInPrefs(getApplicationContext())) {
                new NdefReaderTask().execute(tag);
            } else {
                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);//w w  w  .  j av a  2  s  .  c  o m
            }
        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    }
}

From source file:com.sftoolworks.nfcoptions.SelectActivity.java

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

    if (!handledIntent) {
        if (getIntent().getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
            if (!launchIntent) {

                Log.d(TAG, "This is a second tap");
                writeSelection(getIntent());

            }/*from  www  . ja  va  2 s.c  o m*/
        }
        handledIntent = true;
    }

    nfcAdapter.enableForegroundDispatch(this, pendingIntent,
            new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED) }, null);

}