Android Open Source - wherewithal Nfc Utils






From Project

Back to project page wherewithal.

License

The source code is released under:

GNU General Public License

If you think the Android project wherewithal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.curchod.domartin;
/*w  ww  . ja va  2  s . co m*/
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

/**
* A collection of utility methods for NFC data by kyle lampert of Tapped.
*/
public class NfcUtils 
{

  private static final String LOG_TAG = "NfcUtils";

  /**
   * Creates a custom MIME type encapsulated in an NDEF record for a given
   * payload
   *
   * @param mimeType
   */
  public static NdefRecord createRecord(String mimeType, byte[] payload) 
  {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
  }

  /**
   * Creates an Ndef message
   *
   * @param payload
   * @return
   */
  public static NdefMessage createMessage(String mimeType, byte[] payload) 
  {
    // Min API Level of 14 requires an array as the argument
    return new NdefMessage(new NdefRecord[] {createRecord(mimeType, payload)});
  }

  /**
   * Write an NDEF message to a Tag
   *
   * @param message
   * @param tag
   * @return true if successful, false if not written to
   */
  public static boolean writeTag(NdefMessage message, Tag tag) 
  {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
Log.e(LOG_TAG, "Not writing to tag- tag is not writable");
return false;
}
if (ndef.getMaxSize() < size) {
Log.e(LOG_TAG, "Not writing to tag- message exceeds the max tag size of " + ndef.getMaxSize());
return false;
}
ndef.writeNdefMessage(message);
return true;
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
return true;
} catch (IOException e) {
Log.e(LOG_TAG, "Not writing to tag", e);
return false;
}
} else {
Log.e(LOG_TAG, "Not writing to tag- undefined format");
return false;
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "Not writing to tag", e);
return false;
}
}

/**
* Parse an intent for non-empty strings within an NDEF message
*
* @param intent
* @return an empty list if the payload is empty
*/
  public static List<String> getStringsFromNfcIntent(Intent intent) 
  {
List<String> payloadStrings = new ArrayList<String>();

for (NdefMessage message : getMessagesFromIntent(intent)) {
for (NdefRecord record : message.getRecords()) {
byte[] payload = record.getPayload();
String payloadString = new String(payload);

if (!TextUtils.isEmpty(payloadString))
payloadStrings.add(payloadString);
}
}

return payloadStrings;
}

/**
* Parses an intent for NDEF messages, returns all that are found
*
* @param intent
* @return an empty list if there are no NDEF messages found
*/
public static List<NdefMessage> getMessagesFromIntent(Intent intent) {
List<NdefMessage> intentMessages = new ArrayList<NdefMessage>();
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Log.i(LOG_TAG, "Reading from NFC " + action);
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
for (Parcelable msg : rawMsgs) {
if (msg instanceof NdefMessage) {
intentMessages.add((NdefMessage) msg);
}
}
} else {
// Unknown tag type
byte[] empty = new byte[] {};
final NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
final NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
intentMessages = new ArrayList<NdefMessage>() {
{
add(msg);
}
};
}
}
return intentMessages;
}

/**
* A pending intent is required to enable foreground NDEF dispatch
*
* @param context
* @return
*/
public static PendingIntent getPendingIntent(Activity context) {
return PendingIntent.getActivity(context, 0,
new Intent(context, context.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}

}




Java Source Code List

com.curchod.domartin.AsyncLoadGameFile.java
com.curchod.domartin.Constants.java
com.curchod.domartin.Filer.java
com.curchod.domartin.HouseDeck.java
com.curchod.domartin.IWantTo.java
com.curchod.domartin.MockNdefMessages.java
com.curchod.domartin.NfcUtils.java
com.curchod.domartin.RemoteCall.java
com.curchod.domartin.Sarray.java
com.curchod.domartin.Scoring.java
com.curchod.domartin.TagDescription.java
com.curchod.domartin.UtilityTo.java
com.curchod.dto.Card.java
com.curchod.dto.DeckCard.java
com.curchod.dto.GameWord.java
com.curchod.dto.Game.java
com.curchod.dto.PlayerInfo.java
com.curchod.dto.SavedTest.java
com.curchod.dto.SingleWordTestResult.java
com.curchod.dto.SingleWord.java
com.curchod.json.VocabularyDefinition.java
com.curchod.json.VocabularyLearningObject.java
com.curchod.wherewithal.AddPlayerActivity.java
com.curchod.wherewithal.CardDeckActivity.java
com.curchod.wherewithal.CardDecksActivity.java
com.curchod.wherewithal.CardPlayerHouseDeckActivity.java
com.curchod.wherewithal.CardPlayerWordsActivity.java
com.curchod.wherewithal.CardPlayersListActivity.java
com.curchod.wherewithal.CardsActivity.java
com.curchod.wherewithal.GameConcentrationActivity.java
com.curchod.wherewithal.GameReadingStonesActivity.java
com.curchod.wherewithal.GameReadingStonesInstructionsActivity.java
com.curchod.wherewithal.GameSnazzyThumbworkActivity.java
com.curchod.wherewithal.GameWritingStonesActivity.java
com.curchod.wherewithal.GamesActivity.java
com.curchod.wherewithal.InstructionsActivity.java
com.curchod.wherewithal.MainActivity.java
com.curchod.wherewithal.PlayerActivity.java
com.curchod.wherewithal.PlayersActivity.java