private void resolveIntent(Intent intent) {
   String action = intent.getAction();
   if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
       || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
       || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
     Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
     NdefMessage[] msgs;
     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[0];
       byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
       Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
       byte[] payload = dumpTagData(tag).getBytes();
       NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
       NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
       msgs = new NdefMessage[] {msg};
     }
     // Setup the views
     populateCardID(msgs);
   }
 }
  private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

      String type = intent.getType();
      if (MIME_TEXT_PLAIN.equals(type)) {

        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        new NdefReaderTask().execute(tag);

      } else {
        Log.d(TAG, "Wrong mime type: " + type);
      }
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

      // In case we would still use the Tech Discovered Intent
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      String[] techList = tag.getTechList();
      String searchedTech = Ndef.class.getName();

      for (String tech : techList) {
        if (searchedTech.equals(tech)) {
          new NdefReaderTask().execute(tag);
          break;
        }
      }
    }
  }
示例#3
1
 @Override
 protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
   if (intent != null) {
     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
       Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
       Toast.makeText(this, makeToastText(tag.getTechList()), Toast.LENGTH_SHORT).show();
     }
   }
 }
示例#4
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();
    }
    */
  }
 private void handleIntent(Intent intent) {
   if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
       || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())
       || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
     Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
     if (rawMsgs != null) {
       ndefMessages = new NdefMessage[rawMsgs.length];
       for (int i = 0; i < rawMsgs.length; i++) {
         ndefMessages[i] = (NdefMessage) rawMsgs[i];
       }
     }
     if (ndefMessages != null && ndefMessages.length > 0) {
       String productId = new String(ndefMessages[0].getRecords()[0].getPayload());
       productId = productId != null ? productId.replace("en", "") : "Invalid Product tag";
       // Toast.makeText(getBaseContext(), body != null ? body.replace("en", "") : "Invalid Product
       // tag", Toast.LENGTH_LONG).show();
       productId = productId.replace(String.valueOf(productId.charAt(0)), "");
       if (!CartentiaApplication.MY_CART_PRODUCTS.contains(productId)) {
         addProductToCart(productId);
         loadAddProductFragment(productId);
       } else {
         Toast.makeText(
                 getBaseContext(), "Product is already added into your cart", Toast.LENGTH_LONG)
             .show();
       }
     } else {
       Toast.makeText(getBaseContext(), " Empty Product tag", Toast.LENGTH_LONG).show();
     }
   }
 }
 public void resolveIntent(Intent intent) {
   if (nfcAdapter != null) {
     String action = intent.getAction();
     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
       executeNfcAction(intent);
     }
   }
 }
示例#7
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");
        }
      }
    }
  }
示例#8
0
  @Override
  public ScanableTag scan() {
    Intent intent = parentActivity.getIntent();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
      Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
      return rawToScanable(rawMsgs);
    }

    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
      return new EmptyTag();
    }

    return null;
  }
 @Override
 public void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
   if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
     if (userAccount != null) {
       String tmp = BaseNfc.ScanNfc(this, intent);
       if (tmp != null) {
         userAccount.setText(tmp);
         Logining();
       }
     }
     intents = intent;
   }
 }
示例#10
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;
  }
示例#11
0
文件: Scan.java 项目: kornfeldc/OG125
  @Override
  public void onNewIntent(Intent intent) {
    // fetch the tag from the intent
    String tagId = "";
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
      tagId =
          Functions.ByteArrayToHexString((byte[]) intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
      Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      tagId = Functions.ByteArrayToHexString(tag.getId());
    }

    if (!tagId.equals("")) tagDiscovered(tagId);
  }
示例#12
0
  @Override
  public void onCreate(Bundle savedState) {
    super.onCreate(savedState);

    setContentView(R.layout.foreground_dispatch);
    mText = (TextView) findViewById(R.id.text);

    Intent intent = getIntent();
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
      mText.setText("Discovered tag " + ++mCount + " with intent: " + intent);
    } else {
      mText.setText("Scan a tag");
    }
  }
 @Override
 public void onResume() {
   super.onResume();
   nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, techList);
   if (isnews) {
     if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
       if (userAccount != null) {
         String tmp = BaseNfc.ScanNfc(this, getIntent());
         if (tmp != null) {
           userAccount.setText(tmp);
           Logining();
         }
       }
       intents = getIntent();
       isnews = false;
     }
   }
 }
示例#14
0
  @Override
  public void onNewIntent(Intent intent) {
    nfcid = "";
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
      nfcid = this.ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
      Log.i("Huy", "NDEF DISCOVERED = " + nfcid);

    } else if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
      nfcid = this.ByteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
      Log.i("Huy", "TAG DISCOVERED = " + nfcid);

    } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
      nfcid = this.ByteArrayToHexString(getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID));
      Log.i("Huy", "TECH DISCOVERED = " + nfcid);
    }
    setIntent(intent);
    processNfcID();
    // resolveIntent(intent);
  }
 public void newIntent(Intent intent) {
   String action = intent.getAction();
   if ((this.isReading || this.isWriting) && NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
     handleNfcVIntent(intent);
   }
 }
示例#16
0
  private void resolveIntent(Intent intent) {
    // 1) Parse the intent and get the action that triggered this intent
    String action = intent.getAction();
    // 2) Check if it was triggered by a tag discovered interruption.
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {

      mydialog = ProgressDialog.show(ReadCardActivity.this, "loading", "loading");

      // 3) Get an instance of the TAG from the NfcAdapter
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
      // 4) Get an instance of the Mifare classic card from this TAG
      // intent
      MifareClassic mfc = MifareClassic.get(tagFromIntent);
      MifareClassCard mifareClassCard = null;

      try {
        // 5.1) Connect to card
        mfc.connect();

        // 5.2) and get the number of sectors this card has..and loop
        // thru these sectors
        int secCount = mfc.getSectorCount();
        mifareClassCard = new MifareClassCard(secCount);

        // init information
        String techtemp = "";
        for (String tech : mfc.getTag().getTechList()) {
          techtemp = techtemp + tech + "\n";
        }

        mifareClassCard.addValue("UID", Converter.getHexString(mfc.getTag().getId()));
        mifareClassCard.addValue("TechList", techtemp);
        // mifareClassCard.addValue("MemorySize", mfc.get);

        int bCount = 0;
        int bIndex = 0;
        for (int j = 0; j < secCount; j++) {
          boolean authKeyA = false;
          boolean authKeyB = false;

          MifareSector mifareSector = new MifareSector();
          mifareSector.sectorIndex = j;
          // 6.1) authenticate the sector

          for (String key : defaultKeys) {
            if (!authKeyA) {
              authKeyA = mfc.authenticateSectorWithKeyA(j, Converter.hexStringToByteArray(key));
              mifareSector.keyA = new MifareKey(Converter.hexStringToByteArray(key));
            }

            if (!authKeyB) {
              authKeyB = mfc.authenticateSectorWithKeyB(j, Converter.hexStringToByteArray(key));
              mifareSector.keyB = new MifareKey(Converter.hexStringToByteArray(key));
            }

            // 都满足则break
            if (authKeyA && authKeyB) {
              break;
            }
          }

          if ((authKeyA && authKeyB) || authKeyA || authKeyB) {
            mifareSector.authorized = true;

            // 6.2) In each sector - get the block count
            bCount = mfc.getBlockCountInSector(j);
            bCount = Math.min(bCount, MifareSector.BLOCKCOUNT);
            bIndex = mfc.sectorToBlock(j);

            // set access condition
            byte[] acdata = mfc.readBlock(bIndex + 3);
            String hexString = Converter.getHexString(acdata, 16);
            mifareSector.accessCondition = hexString.substring(12, 20);

            for (int i = 0; i < bCount; i++) {

              // 6.3) Read the block
              byte[] data = mfc.readBlock(bIndex);
              // System.out.println(Converter.getHexString(data, 16));
              MifareBlock mifareBlock = new MifareBlock(mifareSector);
              mifareBlock.setData(data);
              mifareBlock.setIndex(bIndex);
              // 7) Convert the data into a string from Hex
              // format.

              bIndex++;
              mifareSector.blocks[i] = mifareBlock;
            }
            mifareClassCard.setSector(mifareSector.sectorIndex, mifareSector);
          } else { // Authentication failed - Handle it
            mifareSector.authorized = false;
          }
        }
        this.mydialog.dismiss();
        mfc.close();
        Intent dataIntent = new Intent(this, ViewCardActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("mifare", mifareClassCard);
        dataIntent.putExtras(bundle);
        this.startActivityForResult(dataIntent, 0);
        this.finish();
      } catch (IOException e) {
        Log.e(TAG, e.getLocalizedMessage());
        showAlert(3);
        this.mydialog.dismiss();
      } finally {

        if (mifareClassCard != null) {
          // mifareClassCard.debugPrint();
        }
      }
    } // End of IF
  } // End of method