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.matthewmitchell.nubits_android_wallet.ui.SendCoinsFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);/*from  w  w  w  .j  av  a 2s . c om*/
    setHasOptionsMenu(true);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();
        final String action = intent.getAction();
        final Uri intentUri = intent.getData();
        final String scheme = intentUri != null ? intentUri.getScheme() : null;
        final String mimeType = intent.getType();

        if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && intentUri != null && "nu".equals(scheme)) {
            initStateFromNubitsUri(intentUri);
        } else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final NdefMessage ndefMessage = (NdefMessage) intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
            final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST,
                    ndefMessage);
            initStateFromPaymentRequest(mimeType, ndefMessagePayload);
        } else if ((Intent.ACTION_VIEW.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final byte[] paymentRequest = NubitsIntegration.paymentRequestFromIntent(intent);

            if (intentUri != null)
                initStateFromIntentUri(mimeType, intentUri);
            else if (paymentRequest != null)
                initStateFromPaymentRequest(mimeType, paymentRequest);
            else
                throw new IllegalArgumentException();
        } else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
            initStateFromIntentExtras(intent.getExtras());
        } else {
            updateStateFrom(PaymentIntent.blank());
        }
    }
}

From source file:com.hamradiocoin.wallet.ui.send.SendCoinsFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);//from ww w  .j a  va 2  s  . co  m
    setHasOptionsMenu(true);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();
        final String action = intent.getAction();
        final Uri intentUri = intent.getData();
        final String scheme = intentUri != null ? intentUri.getScheme() : null;
        final String mimeType = intent.getType();

        if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && intentUri != null && CoinDefinition.coinURIScheme.equals(scheme)) {
            initStateFromBitcoinUri(intentUri);
        } else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final NdefMessage ndefMessage = (NdefMessage) intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
            final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST,
                    ndefMessage);
            initStateFromPaymentRequest(mimeType, ndefMessagePayload);
        } else if ((Intent.ACTION_VIEW.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final byte[] paymentRequest = BitcoinIntegration.paymentRequestFromIntent(intent);

            if (intentUri != null)
                initStateFromIntentUri(mimeType, intentUri);
            else if (paymentRequest != null)
                initStateFromPaymentRequest(mimeType, paymentRequest);
            else
                throw new IllegalArgumentException();
        } else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
            initStateFromIntentExtras(intent.getExtras());
        } else {
            updateStateFrom(PaymentIntent.blank());
        }
    }
}

From source file:com.matthewmitchell.peercoin_android_wallet.ui.SendCoinsFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);/*from  w ww.  j a  va2  s.co  m*/
    setHasOptionsMenu(true);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();
        final String action = intent.getAction();
        final Uri intentUri = intent.getData();
        final String scheme = intentUri != null ? intentUri.getScheme() : null;
        final String mimeType = intent.getType();

        if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && intentUri != null && "ppcoin".equals(scheme)) {
            initStateFromPeercoinUri(intentUri);
        } else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final NdefMessage ndefMessage = (NdefMessage) intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
            final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST,
                    ndefMessage);
            initStateFromPaymentRequest(mimeType, ndefMessagePayload);
        } else if ((Intent.ACTION_VIEW.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final byte[] paymentRequest = PeercoinIntegration.paymentRequestFromIntent(intent);

            if (intentUri != null)
                initStateFromIntentUri(mimeType, intentUri);
            else if (paymentRequest != null)
                initStateFromPaymentRequest(mimeType, paymentRequest);
            else
                throw new IllegalArgumentException();
        } else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
            initStateFromIntentExtras(intent.getExtras());
        } else {
            updateStateFrom(PaymentIntent.blank());
        }
    }
}

From source file:com.hivewallet.androidclient.wallet.ui.send.SendCoinsFragment.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);/* w  w w .j  a v  a2  s  .c  om*/
    setHasOptionsMenu(true);

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
    backgroundThread.start();
    backgroundHandler = new Handler(backgroundThread.getLooper());

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();
        final String action = intent.getAction();
        final Uri intentUri = intent.getData();
        final String scheme = intentUri != null ? intentUri.getScheme() : null;
        final String mimeType = intent.getType();

        if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && intentUri != null && "bitcoin".equals(scheme)) {
            initStateFromBitcoinUri(intentUri);
        } else if ((NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final NdefMessage ndefMessage = (NdefMessage) intent
                    .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)[0];
            final byte[] ndefMessagePayload = Nfc.extractMimePayload(PaymentProtocol.MIMETYPE_PAYMENTREQUEST,
                    ndefMessage);
            initStateFromPaymentRequest(mimeType, ndefMessagePayload);
        } else if ((Intent.ACTION_VIEW.equals(action))
                && PaymentProtocol.MIMETYPE_PAYMENTREQUEST.equals(mimeType)) {
            final byte[] paymentRequest = BitcoinIntegration.paymentRequestFromIntent(intent);

            if (intentUri != null)
                initStateFromIntentUri(mimeType, intentUri);
            else if (paymentRequest != null)
                initStateFromPaymentRequest(mimeType, paymentRequest);
            else
                throw new IllegalArgumentException();
        } else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_PAYMENT_INTENT)) {
            initStateFromIntentExtras(intent.getExtras());
        } else {
            updateStateFrom(PaymentIntent.blank());
        }
    }
}

From source file:com.geecko.QuickLyric.MainActivity.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.SEARCH":
            search(intent.getStringExtra(SearchManager.QUERY));
            break;
        case "android.intent.action.SEND":
            LyricsViewFragment lyricsViewFragment = (LyricsViewFragment) getFragmentManager()
                    .findFragmentByTag(LYRICS_FRAGMENT_TAG);
            new IdDecoder(this, lyricsViewFragment).execute(getIdUrl(extra));
            selectItem(0);/* w  ww. j  a  v a 2  s .  c  o m*/
            break;
        case "android.intent.action.VIEW":
            if (intent.getDataString().contains("spotify"))
                Spotify.onCallback(intent, this);
            break;
        case "com.geecko.QuickLyric.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);
                selectItem(0);
            }
            break;
        case "com.geecko.QuickLyric.updateDBList":
            updateDBList();
            break;
        }
    }
}

From source file:be.brunoparmentier.wifikeyshare.ui.activities.WifiNetworkActivity.java

private void setupForegroundDispatch() {
    /* initialize the PendingIntent to start for the dispatch */
    nfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    /* initialize the IntentFilters to override dispatching for */
    nfcIntentFilters = new IntentFilter[3];
    nfcIntentFilters[0] = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    nfcIntentFilters[0].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[1] = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilters[1].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[2] = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    nfcIntentFilters[2].addCategory(Intent.CATEGORY_DEFAULT);
    try {/*from w ww .  jav  a 2 s  .c om*/
        nfcIntentFilters[0].addDataType("*/*"); // Handle all MIME based dispatches.
    } catch (IntentFilter.MalformedMimeTypeException e) {
        Log.e(TAG, "setupForegroundDispatch: " + e.getMessage());
    }

    /* Initialize the tech lists used to perform matching for dispatching of the
     * ACTION_TECH_DISCOVERED intent */
    nfcTechLists = new String[][] {};
}

From source file:com.kncwallet.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//www.  j  ava2s  . c  o  m
            protected void bitcoinRequest(final Address address, final String addressLabel,
                    final BigInteger amount, final String bluetoothMac) {
                cannotClassify(inputType);
            }

            @Override
            protected void directTransaction(final Transaction transaction) {
                processDirectTransaction(transaction);
            }

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

    } else if (Constants.WIDGET_START_SEND_QR.equals(action)) { //started by widget

        ActionBar actionBar = getSupportActionBar();
        actionBar.setSelectedNavigationItem(0);
        handleScan();

    } else if (Constants.WIDGET_START_RECEIVE.equals(action)) {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setSelectedNavigationItem(2);

    } else if (Constants.WIDGET_START_SEND.equals(action)) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setSelectedNavigationItem(0);
    }
}

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

/**
 * Sets up the dispatching of NFC events a the foreground Activity.
 *
 * @param activity The {@link android.app.Activity} to setup the foreground dispatch for
 * @param adapter  The {@link android.nfc.NfcAdapter} to setup the foreground dispatch for
 *//* w  ww.ja v a 2 s  . c  om*/
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
    if (adapter != null) {
        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);
        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][] {};

        // Notice that this is the same filter we define in our manifest.
        // This ensures we're not stealing events for other types of NDEF.
        filters[0] = new IntentFilter();
        IntentFilter filter = filters[0];
        filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            filter.addDataType(MIME_VCARD);
            filter.addDataType(MIME_XVCARD);
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("Mime type not supported.");
        }

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

From source file:devcoin.wallet.ui.SendCoinsFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.send_coins_fragment, container);

    receivingAddressView = (AutoCompleteTextView) view.findViewById(R.id.send_coins_receiving_address);
    receivingAddressView.setAdapter(new AutoCompleteAddressAdapter(activity, null));
    receivingAddressView.setOnFocusChangeListener(receivingAddressListener);
    receivingAddressView.addTextChangedListener(receivingAddressListener);

    receivingStaticView = view.findViewById(R.id.send_coins_receiving_static);
    receivingStaticAddressView = (TextView) view.findViewById(R.id.send_coins_receiving_static_address);
    receivingStaticLabelView = (TextView) view.findViewById(R.id.send_coins_receiving_static_label);

    receivingStaticView.setOnFocusChangeListener(new OnFocusChangeListener() {
        private ActionMode actionMode;

        @Override/*from   ww  w  .jav a2 s. c  o  m*/
        public void onFocusChange(final View v, final boolean hasFocus) {
            if (hasFocus)
                actionMode = activity.startActionMode(new ReceivingAddressActionMode());
            else
                actionMode.finish();
        }
    });

    final CurrencyAmountView btcAmountView = (CurrencyAmountView) view.findViewById(R.id.send_coins_amount_btc);
    btcAmountView.setCurrencySymbol(btcShift == 0 ? Constants.CURRENCY_CODE_BTC : Constants.CURRENCY_CODE_MBTC);
    btcAmountView.setInputPrecision(btcShift == 0 ? Constants.BTC_MAX_PRECISION : Constants.MBTC_MAX_PRECISION);
    btcAmountView.setHintPrecision(btcPrecision);
    btcAmountView.setShift(btcShift);

    final CurrencyAmountView localAmountView = (CurrencyAmountView) view
            .findViewById(R.id.send_coins_amount_local);
    localAmountView.setInputPrecision(Constants.LOCAL_PRECISION);
    localAmountView.setHintPrecision(Constants.LOCAL_PRECISION);
    amountCalculatorLink = new CurrencyCalculatorLink(btcAmountView, localAmountView);

    bluetoothEnableView = (CheckBox) view.findViewById(R.id.send_coins_bluetooth_enable);
    bluetoothEnableView.setChecked(bluetoothAdapter != null && bluetoothAdapter.isEnabled());
    bluetoothEnableView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
            if (isChecked && !bluetoothAdapter.isEnabled()) {
                // try to enable bluetooth
                startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),
                        REQUEST_CODE_ENABLE_BLUETOOTH);
            }
        }
    });

    bluetoothMessageView = (TextView) view.findViewById(R.id.send_coins_bluetooth_message);

    sentTransactionView = (ListView) view.findViewById(R.id.send_coins_sent_transaction);
    sentTransactionListAdapter = new TransactionsListAdapter(activity, wallet, application.maxConnectedPeers(),
            false);
    sentTransactionView.setAdapter(sentTransactionListAdapter);

    viewGo = (Button) view.findViewById(R.id.send_coins_go);
    viewGo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            validateReceivingAddress(true);
            validateAmounts(true);

            if (everythingValid())
                handleGo();
        }
    });

    viewCancel = (Button) view.findViewById(R.id.send_coins_cancel);
    viewCancel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            if (state == State.INPUT)
                activity.setResult(Activity.RESULT_CANCELED);

            activity.finish();
        }
    });

    popupMessageView = (TextView) inflater.inflate(R.layout.send_coins_popup_message, container);

    popupAvailableView = inflater.inflate(R.layout.send_coins_popup_available, container);

    if (savedInstanceState != null) {
        restoreInstanceState(savedInstanceState);
    } else {
        final Intent intent = activity.getIntent();
        final String action = intent.getAction();
        final Uri intentUri = intent.getData();
        final String scheme = intentUri != null ? intentUri.getScheme() : null;

        if ((Intent.ACTION_VIEW.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
                && intentUri != null && "devcoin".equals(scheme))
            initStateFromBitcoinUri(intentUri);
        else if (intent.hasExtra(SendCoinsActivity.INTENT_EXTRA_ADDRESS))
            initStateFromIntentExtras(intent.getExtras());
    }

    return view;
}

From source file:com.example.mynsocial.BluetoothChat.java

NdefMessage[] getNdefMessages(Intent intent) {
    Log.e(TAG, "+++ getNdefMessages +++");
    NdefMessage[] msgs = null;/*ww w .  j  av  a  2s .c om*/
    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];
            }
        } else {
            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("Peer to Peer 2", "Unknown intent.");
        finish();
    }

    return msgs;
}