Example usage for android.nfc NdefRecord getTnf

List of usage examples for android.nfc NdefRecord getTnf

Introduction

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

Prototype

public short getTnf() 

Source Link

Document

Returns the 3-bit TNF.

Usage

From source file:Main.java

static JSONObject recordToJSON(NdefRecord record) {
    JSONObject json = new JSONObject();
    try {/*  ww w .  jav  a 2 s  . co 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  w  w.j a v a2s .  c  o  m*/

    // 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.piusvelte.taplock.client.core.TapLockToggle.java

@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
    mServiceInterface = ITapLockService.Stub.asInterface(binder);
    if (mUIInterface != null) {
        try {//from   w w w  .  jav  a  2 s.  com
            mServiceInterface.setCallback(mUIInterface);
        } catch (RemoteException e) {
            Log.e(TAG, e.toString());
        }
    }
    Intent intent = getIntent();
    if (intent != null) {
        String action = intent.getAction();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
                && intent.hasExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) {
            Log.d(TAG, "service connected, NDEF_DISCOVERED");
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            NdefMessage message;
            if (rawMsgs != null) {
                // process the first message
                message = (NdefMessage) rawMsgs[0];
                // process the first record
                NdefRecord record = message.getRecords()[0];
                if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
                    try {
                        byte[] payload = record.getPayload();
                        /*
                         * payload[0] contains the "Status Byte Encodings" field, per the
                         * NFC Forum "Text Record Type Definition" section 3.2.1.
                         *
                         * bit7 is the Text Encoding Field.
                         *
                         * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1):
                         * The text is encoded in UTF16
                         *
                         * Bit_6 is reserved for future use and must be set to zero.
                         *
                         * Bits 5 to 0 are the length of the IANA language code.
                         */
                        String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
                        int languageCodeLength = payload[0] & 0077;
                        String taggedDeviceName = new String(payload, languageCodeLength + 1,
                                payload.length - languageCodeLength - 1, textEncoding);
                        manageDevice(taggedDeviceName, ACTION_TOGGLE);
                    } catch (UnsupportedEncodingException e) {
                        // should never happen unless we get a malformed tag.
                        Log.e(TAG, e.toString());
                        finish();
                    }
                } else
                    finish();
            } else
                finish();
        } else if (intent.getData() != null) {
            String taggedDeviceName = intent.getData().getHost();
            if (taggedDeviceName != null)
                manageDevice(taggedDeviceName, ACTION_TOGGLE);
            else
                finish();
        } else if (ACTION_UNLOCK.equals(action) || ACTION_LOCK.equals(action) || ACTION_TOGGLE.equals(action))
            manageDevice(intent.getStringExtra(EXTRA_DEVICE_NAME), action);
        else
            finish();
    } else
        finish();
}

From source file:de.berlin.magun.nfcmime.core.RfidDAO.java

/**
 * Checks all NDEF records against the CRC value contained in the last record.
 * @return true, if the checksum is correct.
 *//*w  w w . ja v  a2s  .  c om*/
public boolean verifyMimeRecords() {
    CrcGenerator generator = new CrcGenerator();
    NdefRecord checksumRecord = this.records[this.records.length - 1];
    NdefRecord[] payloadRecords = (NdefRecord[]) ArrayUtils.remove(this.records, this.records.length - 1);
    if (checksumRecord.getTnf() == NdefRecord.TNF_MIME_MEDIA
            && EncodingUtils.getString(checksumRecord.getPayload(), "UTF-8").startsWith("crc32:")) {
        String checksumStr = EncodingUtils.getString(checksumRecord.getPayload(), "UTF--8");
        long checksum = Long.parseLong(checksumStr.substring(checksumStr.indexOf("crc32:") + 6));
        return generator.checkHash(payloadRecords, checksum);
    } else {
        return false;
    }
}

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

/**
 * Handle incoming intents. Do not load a product if we already have one.
 *//*from w  ww .  ja v  a2 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   ww  w . j ava  2s  .c o  m*/
    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 2 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;

}