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:com.googlecode.android_scripting.facade.ui.NFCBeamTask.java

@SuppressLint("NewApi")
NdefMessage[] getNdefMessagesFromIntent(Intent intent) {
    // Parse the intent
    NdefMessage[] msgs = null;//from w ww .j  a  v a 2s  .co  m
    String action = intent.getAction();
    if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        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];
            }

        } 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.e(TAG, "Unknown intent.");
        finish();
    }
    return msgs;
}

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

@SuppressLint("NewApi")
@Override/*from   ww  w .java  2  s.  co  m*/
public void onNewIntent(Intent intent) {
    Log.d(TAG, "onNewIntent");

    if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        NdefMessage[] msgs = getNdefMessagesFromIntent(intent);
        NdefRecord record = msgs[0].getRecords()[0];
        byte[] payload = record.getPayload();

        String payloadString = new String(payload);

        //Toast.makeText(getActivity().getApplicationContext(), payloadString, Toast.LENGTH_SHORT).show();

        this.content = payloadString;
        setResult("slave", "ok");

    } else if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Toast.makeText(getActivity().getApplicationContext(), "This NFC tag has no NDEF data.",
                Toast.LENGTH_LONG).show();
    }
}

From source file:com.example.multi_ndef.Frag_Write.java

/**
 * Force this Activity to get NFC events first
 */// w w  w . jav  a  2  s  .  c o  m

//@SuppressLint("NewApi")
private void enableWriteMode() {
    mInWriteMode = true;

    try {
        // set up a PendingIntent to open the app when a tag is scanned
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter[] filters = new IntentFilter[] { tagDetected };

        mAdapter.enableForegroundDispatch(this, pendingIntent, filters, null);
    }

    catch (Exception e) {

        Toast toast = Toast.makeText(getApplicationContext(), "Problem capturing tag" + e.toString(),
                Toast.LENGTH_SHORT);
        toast.show();

    }
}

From source file:com.sigilance.CardEdit.MainActivity.java

@Override
public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        try {//ww w. j a v a 2 s .com
            handleNdefDiscoveredIntent(intent);
        } catch (IOException e) {
            handleNfcError(e);
        }
    }
}

From source file:com.piusvelte.taplock.client.core.TapLockSettings.java

@Override
protected void onListItemClick(ListView list, final View view, int position, final long id) {
    super.onListItemClick(list, view, position, id);
    mDialog = new AlertDialog.Builder(TapLockSettings.this)
            .setItems(R.array.actions_entries, new DialogInterface.OnClickListener() {
                @Override// ww  w.j a  v  a2 s  .c o  m
                public void onClick(DialogInterface dialog, int which) {
                    String action = getResources().getStringArray(R.array.actions_values)[which];
                    int deviceIdx = (int) id;
                    JSONObject deviceJObj = mDevices.get(deviceIdx);
                    dialog.cancel();
                    if (ACTION_UNLOCK.equals(action) || ACTION_LOCK.equals(action)
                            || ACTION_TOGGLE.equals(action)) {
                        String name = "uknown";
                        try {
                            name = deviceJObj.getString(KEY_NAME);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        startActivity(TapLock.getPackageIntent(getApplicationContext(), TapLockToggle.class)
                                .setAction(action).putExtra(EXTRA_DEVICE_NAME, name));
                    } else if (ACTION_TAG.equals(action)) {
                        // write the device to a tag
                        mInWriteMode = true;
                        try {
                            mNfcAdapter.enableForegroundDispatch(TapLockSettings.this,
                                    PendingIntent.getActivity(TapLockSettings.this, 0,
                                            new Intent(TapLockSettings.this, TapLockSettings.this.getClass())
                                                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra(
                                                            EXTRA_DEVICE_NAME, deviceJObj.getString(KEY_NAME)),
                                            0),
                                    new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) },
                                    null);
                        } catch (JSONException e) {
                            Log.e(TAG, e.getMessage());
                        }
                        Toast.makeText(TapLockSettings.this, "Touch tag", Toast.LENGTH_LONG).show();
                    } else if (ACTION_REMOVE.equals(action)) {
                        mDevices.remove(deviceIdx);
                        storeDevices();
                    } else if (ACTION_PASSPHRASE.equals(action))
                        setPassphrase(deviceIdx);
                    else if (ACTION_COPY_DEVICE_URI.equals(action)) {
                        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                        ClipData clip;
                        try {
                            clip = ClipData.newPlainText(getString(R.string.app_name), String
                                    .format(getString(R.string.device_uri), deviceJObj.get(EXTRA_DEVICE_NAME)));
                            clipboard.setPrimaryClip(clip);
                            Toast.makeText(TapLockSettings.this, "copied to clipboard!", Toast.LENGTH_SHORT)
                                    .show();
                        } catch (JSONException e) {
                            Log.e(TAG, e.getMessage());
                            Toast.makeText(TapLockSettings.this, getString(R.string.msg_oops),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }).create();
    mDialog.show();
}