コード例 #1
0
 @Override
 protected void onResume() {
   super.onResume();
   nfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mMifareFilters, mTechLists);
   receiveTab.append("\nAdded new mifare intent filter");
   Intent intent = getIntent();
   resolveIntent(intent);
 }
コード例 #2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar.newTab().setText(R.string.receive_receipt).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this));

    // Initialize NFC and add Intent
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (nfcAdapter == null) {
      Toast.makeText(this, "No NFC Adapter, Maybe try Bluetooth", Toast.LENGTH_LONG).show();
    }
    // receiveTab.append("\nIn Create");
    mNfcPendingIntent =
        PendingIntent.getActivity(
            this,
            0,
            new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            0);

    // Intent filters for reading a note from a tag or exchanging over p2p
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
      ndefDetected.addDataType("text/plain");
    } catch (MalformedMimeTypeException ex) {
    }
    mNdefExchangeFilters = new IntentFilter[] {ndefDetected};

    // Intent filter for reading a mifare classic card/data
    IntentFilter mifareDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);

    try {
      mifareDetected.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {

      throw new RuntimeException("fail", e);
    }
    IntentFilter someTag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    mMifareFilters = new IntentFilter[] {mifareDetected, someTag};
    if (mMifareFilters != null) {
      toast("Filter created");
    }
    mTechLists =
        new String[][] {
          new String[] {
            MifareClassic.class.getName(),
            NfcA.class.getName(),
            NfcB.class.getName(),
            NfcF.class.getName(),
            NfcV.class.getName(),
            IsoDep.class.getName(),
            Ndef.class.getName(),
            NdefFormatable.class.getName()
          }
        };
    Intent intent = getIntent();
    resolveIntent(intent);
  }
コード例 #3
0
 @Override
 protected void onPause() {
   super.onPause();
   nfcAdapter.disableForegroundDispatch(this);
   toast("App Paused");
 }