Example usage for android.nfc NdefRecord getType

List of usage examples for android.nfc NdefRecord getType

Introduction

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

Prototype

public byte[] getType() 

Source Link

Document

Returns the variable length Type field.

Usage

From source file:Main.java

static JSONObject recordToJSON(NdefRecord record) {
    JSONObject json = new JSONObject();
    try {//from ww  w.  j a  v a  2s.  c  o m
        json.put("tnf", record.getTnf());
        json.put("type", byteArrayToJSON(record.getType()));
        json.put("id", byteArrayToJSON(record.getId()));
        json.put("payload", byteArrayToJSON(record.getPayload()));
    } catch (JSONException e) {
        //Not sure why this would happen, documentation is unclear.
        Log.e(TAG, "Failed to convert ndef record into json: " + record.toString(), e);
    }
    return json;
}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);//from  w ww . j  av a 2s  .  c om

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}

From source file:com.hybris.mobile.activity.AbstractProductDetailActivity.java

/**
 * Handle incoming intents. Do not load a product if we already have one.
 *//*  w w w .j  a v  a  2 s  . co m*/
@Override
protected void onResume() {
    super.onResume();

    String[] options = { InternalConstants.PRODUCT_OPTION_BASIC, InternalConstants.PRODUCT_OPTION_CATEGORIES,
            InternalConstants.PRODUCT_OPTION_CLASSIFICATION, InternalConstants.PRODUCT_OPTION_DESCRIPTION,
            InternalConstants.PRODUCT_OPTION_GALLERY, InternalConstants.PRODUCT_OPTION_PRICE,
            InternalConstants.PRODUCT_OPTION_PROMOTIONS, InternalConstants.PRODUCT_OPTION_REVIEW,
            InternalConstants.PRODUCT_OPTION_STOCK, InternalConstants.PRODUCT_OPTION_VARIANT };

    String productCode = null;

    Intent intent = getIntent();

    // Direct Call
    if (intent.hasExtra(DataConstants.PRODUCT_CODE)) //direct call from search list for example
    {
        productCode = intent.getStringExtra(DataConstants.PRODUCT_CODE);
    }
    // NFC Call
    else if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) //NFC 
    {
        Tag tag = getIntent().getExtras().getParcelable(NfcAdapter.EXTRA_TAG);

        Ndef ndef = Ndef.get(tag);
        NdefMessage message = ndef.getCachedNdefMessage();

        NdefRecord record = message.getRecords()[0];
        if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN
                && Arrays.equals(record.getType(), NdefRecord.RTD_URI)) {
            productCode = RegexUtil
                    .getProductCode(new String(record.getPayload(), 1, record.getPayload().length - 1));
        }
    }
    // Call from another application (QR Code) 
    else if (StringUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {
        productCode = RegexUtil.getProductCode(intent.getDataString());
    }

    if (StringUtils.isNotEmpty(productCode)) {
        this.enableAndroidBeam(productCode);
    }

    // Only load if we don't have a product already
    if (mProduct == null) {
        populateProduct(productCode, options);
    }

    invalidateOptionsMenu();
}

From source file:org.sufficientlysecure.keychain.ui.PassphraseWizardActivity.java

private String read(Tag tag) throws IOException, FormatException {
    //read string from tag
    String password = null;/*from w w w  .j  a  v  a  2 s. c om*/
    Ndef ndef = Ndef.get(tag);
    ndef.connect();
    NdefMessage ndefMessage = ndef.getCachedNdefMessage();

    NdefRecord[] records = ndefMessage.getRecords();
    for (NdefRecord ndefRecord : records) {
        if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN
                && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
            try {
                password = readText(ndefRecord);
            } catch (UnsupportedEncodingException e) {
                Log.e(Constants.TAG, "Failed to read password from tag", e);
            }
        }
    }
    ndef.close();
    return password;
}

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

/**
 * Initialize from a legacy <code>NdefMessage</code>
 * /* ww w .  j  a v a 2s  . 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;

}