Example usage for android.nfc NfcAdapter getDefaultAdapter

List of usage examples for android.nfc NfcAdapter getDefaultAdapter

Introduction

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

Prototype

public static NfcAdapter getDefaultAdapter(Context context) 

Source Link

Document

Helper to get the default NFC Adapter.

Usage

From source file:net.networksaremadeofstring.rhybudd.ViewZenossDeviceListActivity.java

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

    BugSenseHandler.initAndStartSession(ViewZenossDeviceListActivity.this, "44a76a8c");

    setContentView(R.layout.view_zenoss_device_list);

    try {//from   w w  w . j  a v a 2s  .c  om
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (null != mNfcAdapter) {
            // Register callback
            mNfcAdapter.setNdefPushMessageCallback(this, this);
        }
    } catch (Exception e) {
        BugSenseHandler.sendExceptionMessage("ViewZenossDevice", "NFC", e);
    }

    ab = getActionBar();
    ab.setTitle("Rhybudd");
    ab.setSubtitle("Zenoss Infrastructure Details");
    ab.setDisplayHomeAsUpEnabled(true);

    if (findViewById(R.id.device_detail_container) != null) {
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((ViewZenossDeviceListFragment) getSupportFragmentManager().findFragmentById(R.id.device_list))
                .setActivateOnItemClick(true);

        if (savedInstanceState == null) {
            DeviceListWelcomeFragment fragment = new DeviceListWelcomeFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.device_detail_container, fragment)
                    .commit();
        }
    }
}

From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java

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

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        Toast.makeText(getApplicationContext(), "not found NFC feature", Toast.LENGTH_SHORT).show();
        finish();/* w  ww .  j av a  2  s  . co  m*/
        return;
    }

    if (!mNfcAdapter.isEnabled()) {
        Toast.makeText(getApplicationContext(), "NFC feature is not available", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
    IntentFilter[] intentFilter = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED), };
    String[][] techList = new String[][] { { android.nfc.tech.NdefFormatable.class.getName() },
            { android.nfc.tech.Ndef.class.getName() } };
    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, techList);

}

From source file:ca.psiphon.ploggy.ActivitySendIdentityByNfc.java

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

    mNfcEnabled = false;/*from www  .j ava2s  . c  om*/
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter != null) {
        if (!mNfcAdapter.isEnabled() || !mNfcAdapter.isNdefPushEnabled()) {
            Toast.makeText(this, R.string.prompt_nfc_not_enabled, Toast.LENGTH_LONG).show();
        } else {
            mNfcEnabled = true;
            mNfcAdapter.setNdefPushMessageCallback(this, this);
            mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
        }
    }
}

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

/**
 * NFC: Initialize NFC sharing if OS and device supports it
 *//*  ww w.  ja v  a  2s .c o m*/
private void initNfc(Uri dataUri) {
    // check if NFC Beam is supported (>= Android 4.1)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter != null) {
            // init nfc

            // get public keyring as byte array
            long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
            mSharedKeyringBytes = ProviderHelper.getKeyRingsAsByteArray(this, dataUri,
                    new long[] { masterKeyId });

            // Register callback to set NDEF message
            mNfcAdapter.setNdefPushMessageCallback(this, this);
            // Register callback to listen for message-sent success
            mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
        }
    }
}

From source file:org.sufficientlysecure.keychain.ui.dialog.ShareNfcDialogFragment.java

/**
 * Creates dialog/* w ww. j a v a 2s .  c  o  m*/
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final FragmentActivity activity = getActivity();

    CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(activity);

    alert.setTitle(R.string.share_nfc_dialog);
    alert.setCancelable(true);

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });

    HtmlTextView textView = new HtmlTextView(getActivity());
    textView.setPadding(8, 8, 8, 8);
    alert.setView(textView);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        textView.setText(getString(R.string.error) + ": " + getString(R.string.error_jelly_bean_needed));
    } else {
        // check if NFC Adapter is available
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
        if (nfcAdapter == null) {
            textView.setText(getString(R.string.error) + ": " + getString(R.string.error_nfc_needed));
        } else {
            // nfc works...
            textView.setHtmlFromRawResource(getActivity(), R.raw.nfc_beam_share, true);

            alert.setNegativeButton(R.string.menu_beam_preferences, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
                    startActivity(intentSettings);
                }
            });
        }
    }

    return alert.show();
}

From source file:org.thialfihar.android.apg.ui.dialog.ShareNfcDialogFragment.java

/**
 * Creates dialog//from w w  w .  j a  va  2 s . com
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final FragmentActivity activity = getActivity();

    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle(R.string.share_nfc_dialog);
    alert.setCancelable(true);

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });

    HtmlTextView textView = new HtmlTextView(getActivity());
    textView.setPadding(8, 8, 8, 8);
    alert.setView(textView);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        textView.setText(getString(R.string.error) + ": " + getString(R.string.error_jelly_bean_needed));
    } else {
        // check if NFC Adapter is available
        NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
        if (nfcAdapter == null) {
            textView.setText(getString(R.string.error) + ": " + getString(R.string.error_nfc_needed));
        } else {
            // nfc works...
            textView.setHtmlFromRawResource(getActivity(), R.raw.nfc_beam_share);

            alert.setNegativeButton(R.string.menu_beam_preferences, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
                    startActivity(intentSettings);
                }
            });
        }
    }

    // no flickering when clicking textview for Android < 4
    // aboutTextView.setTextColor(getResources().getColor(android.R.color.black));

    return alert.create();
}

From source file:de.lazyheroproductions.campuscardreader.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // get an instance of the local broadcast manager to receive messages which are send inside the application
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter(CardReaderIntentService.CAMPUS_CARD_INTENT));
    creditTextView = (TextView) findViewById(R.id.credit);
    transactionTextView = (TextView) findViewById(R.id.last_transaction);

    if (BuildConfig.DEBUG) {
        Log.i(this.getClass().getName(), "received intent on create");
    }/*from  www .  ja v a 2  s. c  o m*/
    startNfcIntentService(getIntent());

    // intercept all NFC related Intents and redirect them to this activity while this activity is activ and on the front
    // this is called the "foreground dispatch"
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter nfcTech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    mFilters = new IntentFilter[] { nfcTech };
    mTechLists = new String[][] { new String[] { IsoDep.class.getName() }, { NfcA.class.getName() } };

    //adview related stuff
    //        adView = (AdView) this.findViewById(R.id.adView);
    setUpAdview();
}

From source file:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_door);

    mTextView = (TextView) findViewById(R.id.txtOpenDoor);
    mProgressBar = (ProgressBar) findViewById(R.id.pbOpenDoor);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    mProgressBar.setVisibility(View.GONE);

    handleIntent(getIntent());//  w  ww.jav a  2 s.  c  o m
}

From source file:com.example.android.cardreader.CardReaderFragment.java

private void enableReaderMode() {
    Log.i(TAG, "Enabling reader mode");
    Activity activity = getActivity();//from  w  w w . j  a v  a  2s.  c o m
    NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
    if (nfc != null) {
        nfc.enableReaderMode(activity, mLoyaltyCardReader, READER_FLAGS, null);
    }
}

From source file:se.anyro.nfc_reader.TagViewer.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tag_viewer);
    mTagContent = (LinearLayout) findViewById(R.id.list);
    resolveIntent(getIntent());/*from w w w.  ja va  2s  .  c o  m*/

    mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null).create();

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mAdapter == null) {
        showMessage(R.string.error, R.string.no_nfc);
        finish();
        return;
    }

    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    mNdefPushMessage = new NdefMessage(
            new NdefRecord[] { newTextRecord("Message from NFC Reader :-)", Locale.ENGLISH, true) });
}