Example usage for android.nfc NdefRecord getPayload

List of usage examples for android.nfc NdefRecord getPayload

Introduction

In this page you can find the example usage for android.nfc NdefRecord getPayload.

Prototype

public byte[] getPayload() 

Source Link

Document

Returns the variable length payload.

Usage

From source file:it.imwatch.nfclottery.MainActivity.java

/**
 * Parses a discovered NDEF vCard message and stores it
 * in the app's ContentProvider. If the card contains more than one
 * for each detail type (name, organization, title, email) then they
 * are merged when inserting them in the database in a list string
 * for each type. Values in these lists are separated by  Only the first non-empty value for each type of
 * field will be shown on the UI, anyway.
 *
 * @param ndefMessage The NDEF message to parse and store data from
 *///  w  ww  .j  a  va 2 s  . c  om
private void parseAndInsertVCard(NdefMessage ndefMessage) {
    NdefRecord[] records = ndefMessage.getRecords();
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> organizations = new ArrayList<String>();
    ArrayList<String> titles = new ArrayList<String>();

    // Tags might contain more than one record, each of which might contain
    // one or more of the elements (or no record, if malformed...)!
    for (NdefRecord record : records) {
        String vCardString = new String(record.getPayload());

        try {
            readDetailsFromvCard(mVCardEngine, vCardString, names, emails, organizations, titles);
        } catch (IOException e) {
            Log.e(TAG, "Error while parsing a vCard!", e);
            if (DEBUG)
                Log.v(TAG, "Malformed vCard contents:\n" + vCardString);
        }
    }

    if (emails.isEmpty()) {
        // It's mandatory to have at least one email address
        showCroutonNao(getString(R.string.error_email_not_present), Style.ALERT);
        Log.e(TAG, "The tag doesn't contain any email address");
        return;
    }

    // Now check and consolidate all emails in a CSV list
    // (to avoid looping though this list all over again later on)
    StringBuilder emailCsv = new StringBuilder();
    for (String email : emails) {
        if (DataHelper.isEmailAlreadyPresent(this, email)) {
            // we add only once every email
            showCroutonNao(getString(R.string.error_email_already_present, email), Style.ALERT);
            return;
        }

        emailCsv.append(email).append(DataHelper.VALUES_SEPARATOR);
    }

    if (DataHelper.insertContact(this, names, emailCsv.toString(), organizations, titles)) {
        showCroutonNao(getString(R.string.new_contact_added, emails.get(0)), Style.CONFIRM);
        updateParticipantsCount();
    }
}

From source file:us.rader.wyfy.MainActivity.java

/**
 * Initialize from a legacy <code>NdefMessage</code>
 * //from www  .  j  a  v a2  s  .c o  m
 * Provide backward compatibility for tags written with older versions of
 * this app
 * 
 * @param ndefMessage
 *            legacy <code>NdefMessage</code>
 * 
 * @return <code>true</code> if and only if an asynchronouse attempt to
 *         connect was launched
 */
private boolean parseLegacyMessage(NdefMessage ndefMessage) {

    try {

        NdefRecord[] records = ndefMessage.getRecords();

        if (records.length > 0) {

            NdefRecord record = records[0];

            if (record.getTnf() != NdefRecord.TNF_MIME_MEDIA) {

                return false;

            }

            String type = new String(record.getType(), "US-ASCII"); //$NON-NLS-1$

            if ("application/x-wyfy".equals(type)) { //$NON-NLS-1$

                String payload = new String(record.getPayload(), "US-ASCII"); //$NON-NLS-1$
                return parseUri(payload);

            }
        }

    } catch (Exception e) {

        Log.e(getClass().getName(), "initializeNdefMessage", e); //$NON-NLS-1$

    }

    return false;

}

From source file:com.mario22gmail.license.nfc_project.NavigationDrawerActivity.java

public String getTextFromNdefRecord(android.nfc.NdefRecord ndefRecord) {
    String tagContent = null;/*w w  w. j av  a2  s .c o m*/
    try {
        byte[] payload = ndefRecord.getPayload();
        String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
        int languageSize = payload[0] & 0063;
        tagContent = new String(payload, languageSize + 1, payload.length - languageSize - 1, textEncoding);
    } catch (UnsupportedEncodingException e) {
        Log.e("getTextFromNdefRecord", e.getMessage(), e);
    }
    Log.i(nfcDebugTag, "nfc content " + tagContent);
    return tagContent;
}