コード例 #1
0
  @Override
  protected void onNewIntent(Intent intent) {
    toast("INTENT");
    // NDEF Exchange
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
      // vibrate(500); //Vibrate for half a secondb.
      receiveTab.append("\nWe Discovered an NDEsF tag");
      NdefMessage[] msgs = getNdefData(intent);
      if (msgs == null) {
        receiveTab.append("\nThere are NO messages");
        return;
      } else {
        receiveTab.append("\nThere ARE MESSAGES");
        Toast.makeText(this, "There are messages", Toast.LENGTH_LONG).show();
      }
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
      receiveTab.append("\nNew  Card found");
      resolveIntent(intent); // From example on mifareclassic..blogspot
    } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
      receiveTab.append("\nTag found from card");
      resolveIntent(intent);
    }

    // Mifare Classic Mode
    /*
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()))
    {
    	vibrate(1000); //Vibrate for a second
    	Toast.makeText(this, "Mifare Type received",
    			Toast.LENGTH_LONG).show();
    }
    */
  }
コード例 #2
0
  private void resolveIntent(Intent intent) {
    String action = intent.getAction();
    toast("resolving intent");
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
      toast("RESOLVE THIS INTENT");
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      receiveTab.append("\nResolve Intent for NDEF discovery");
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      MifareClassic mfc = MifareClassic.get(tagFromIntent);
      byte[] data;
      receiveTab.append("\nNew tech tag received");
      try {
        mfc.connect();
        boolean auth = false;
        String cardData = null;
        int secCount = mfc.getSectorCount();
        int bCount = 0;
        int bIndex = 0;
        for (int j = 0; j < secCount; j++) {
          auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT);
          if (auth) {
            bCount = mfc.getBlockCountInSector(j);
            bIndex = 0;
            for (int i = 0; i < bCount; i++) {
              bIndex = mfc.sectorToBlock(j);
              data = mfc.readBlock(bIndex);
              cardData = new String(data);
              receiveTab.append("\n" + cardData);
              bIndex++;
            }
          } else {
            Toast.makeText(this, "Auth failed", Toast.LENGTH_LONG).show();
          }
        }
      } catch (IOException ex) {
        Toast.makeText(this, "IO Error while attempting to read mifare", Toast.LENGTH_LONG).show();
      }
    } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
      receiveTab.append("\nSome Tag found from card");
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

      receiveTab.append("\n" + tagFromIntent.toString());
      for (int k = 0; k < tagFromIntent.getTechList().length; k++) {
        receiveTab.append("\nTech List: " + tagFromIntent.getTechList()[k]);
        if (tagFromIntent.getTechList()[k].equals("android.nfc.tech.NfcA")) {
          // Run connection method
          parseNfcATag(tagFromIntent);
          // connectToNDEF(tagFromIntent);
          // parseMifareTag(tagFromIntent);
        } else if (tagFromIntent.getTechList()[k].equals("android.nfc.tech.Ndef")) {
          connectToNDEF(tagFromIntent);
          // receiveTab.append("\nRun connect to NDEF");
        }
      }
    }
  }
コード例 #3
0
  protected NdefMessage[] getNdefData(Intent intent) {
    NdefMessage[] messages = null;
    String action = intent.getAction();

    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
        || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
        || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
      Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
      messages = getMessages(rawMsgs);
    } else {
      Toast.makeText(this, "Not sure which nfc type this is", Toast.LENGTH_LONG).show();
    }

    return messages;
  }