Java Code Examples for android.nfc.NdefRecord#getTnf()

The following examples show how to use android.nfc.NdefRecord#getTnf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NfcTypeConverter.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Converts android.nfc.NdefRecord to mojo NfcRecord
 */
private static NfcRecord toNfcRecord(NdefRecord ndefRecord)
        throws UnsupportedEncodingException {
    switch (ndefRecord.getTnf()) {
        case NdefRecord.TNF_EMPTY:
            return createEmptyRecord();
        case NdefRecord.TNF_MIME_MEDIA:
            return createMIMERecord(
                    new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
        case NdefRecord.TNF_ABSOLUTE_URI:
            return createURLRecord(ndefRecord.toUri());
        case NdefRecord.TNF_WELL_KNOWN:
            return createWellKnownRecord(ndefRecord);
    }
    return null;
}
 
Example 2
Source File: ReadUriActivity.java    From android-nfc with MIT License 5 votes vote down vote up
/**
 * 解析NdefRecord中Uri数据
 *
 * @param record
 * @return
 */
public static Uri parse(NdefRecord record) {
    short tnf = record.getTnf();
    if (tnf == NdefRecord.TNF_WELL_KNOWN) {
        return parseWellKnown(record);
    } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
        return parseAbsolute(record);
    }
    throw new IllegalArgumentException("Unknown TNF " + tnf);
}
 
Example 3
Source File: ReadTextActivity.java    From android-nfc with MIT License 5 votes vote down vote up
/**
 * 解析NDEF文本数据,从第三个字节开始,后面的文本数据
 *
 * @param ndefRecord
 * @return
 */
public static String parseTextRecord(NdefRecord ndefRecord) {
    /**
     * 判断数据是否为NDEF格式
     */
    //判断TNF
    if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
        return null;
    }
    //判断可变的长度的类型
    if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
        return null;
    }
    try {
        //获得字节数组,然后进行分析
        byte[] payload = ndefRecord.getPayload();
        //下面开始NDEF文本数据第一个字节,状态字节
        //判断文本是基于UTF-8还是UTF-16的,取第一个字节"位与"上16进制的80,16进制的80也就是最高位是1,
        //其他位都是0,所以进行"位与"运算后就会保留最高位
        String textEncoding = ((payload[0] & 0x80) == 0) ? "UTF-8" : "UTF-16";
        //3f最高两位是0,第六位是1,所以进行"位与"运算后获得第六位
        int languageCodeLength = payload[0] & 0x3f;
        //下面开始NDEF文本数据第二个字节,语言编码
        //获得语言编码
        String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
        //下面开始NDEF文本数据后面的字节,解析出文本
        String textRecord = new String(payload, languageCodeLength + 1,
                payload.length - languageCodeLength - 1, textEncoding);
        return textRecord;
    } catch (Exception e) {
        throw new IllegalArgumentException();
    }
}
 
Example 4
Source File: NdefRecordUtil.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param record - the Ndef record to read from
 * @param userSpecifiedTypes - the list of types that the user specified as expected
 * @param domainForExternalTypes - the domain to qualify external types with
 * @return A Pair in which the 2nd value represents whether the read operation was successful.
 * So if the 2nd value is true, the 1st value is the successfully read string value; if the 2nd
 * value is false, the 1st value is the locale key for the error message that should be shown.
 */
protected static Pair<String,Boolean> readValueFromRecord(NdefRecord record,
                                                          String[] userSpecifiedTypes,
                                                          String domainForExternalTypes) {
    switch (record.getTnf()) {
        case NdefRecord.TNF_WELL_KNOWN:
            return handleWellKnownTypeRecord(record, userSpecifiedTypes);
        case NdefRecord.TNF_EXTERNAL_TYPE:
            return handleExternalTypeRecord(record, userSpecifiedTypes, domainForExternalTypes);
        default:
            return new Pair<>(READ_ERROR_UNSUPPORTED_KEY, false);
    }
}
 
Example 5
Source File: TabbedMainActivity.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private void onBitcoinUri() {

        Uri uri = null;
        if (!NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()))
            uri = getIntent().getData();
        else {
            final Parcelable[] rawMessages;
            rawMessages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            for (final Parcelable parcel : rawMessages) {
                final NdefMessage ndefMsg = (NdefMessage) parcel;
                for (final NdefRecord record : ndefMsg.getRecords())
                    if (record.getTnf() == NdefRecord.TNF_WELL_KNOWN &&
                        Arrays.equals(record.getType(), NdefRecord.RTD_URI))
                        uri = record.toUri();
            }
        }
        if (uri == null)
            return;

        final Intent intent = new Intent(this, SendAmountActivity.class);
        final String text = uri.toString();
        try {
            final int subaccount = getActiveAccount();
            final GDKTwoFactorCall call = getSession().createTransactionFromUri(null, text, subaccount);
            final ObjectNode transactionFromUri = call.resolve(null, new HardwareCodeResolver(this));
            final String error = transactionFromUri.get("error").asText();
            if ("id_invalid_address".equals(error)) {
                UI.toast(this, R.string.id_invalid_address, Toast.LENGTH_SHORT);
                return;
            }
            removeUtxosIfTooBig(transactionFromUri);
            intent.putExtra(PrefKeys.INTENT_STRING_TX, transactionFromUri.toString());
        } catch (final Exception e) {
            e.printStackTrace();
            if (e.getMessage() != null)
                UI.toast(this, e.getMessage(), Toast.LENGTH_SHORT);
            return;
        }
        intent.putExtra("internal_qr", getIntent().getBooleanExtra("internal_qr", false));
        startActivityForResult(intent, REQUEST_BITCOIN_URL_SEND);
    }
 
Example 6
Source File: UnsupportedRecord.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
public static UnsupportedRecord parse(NdefRecord ndefRecord) {
	return new UnsupportedRecord(ndefRecord.getTnf(), ndefRecord.getType(), ndefRecord.getId(), ndefRecord.getPayload());
}
 
Example 7
Source File: UnsupportedRecord.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
public UnsupportedRecord(NdefRecord record) {
	this(record.getTnf(), record.getType(), record.getId(), record.getPayload());
}
 
Example 8
Source File: Record.java    From effective_android_sample with Apache License 2.0 4 votes vote down vote up
/**
    * Parse a byte-based {@link NdefRecord} into a high-level {@link Record}.
    * 
    * @param ndefRecord record to parse
    * @return corresponding {@link Record} subclass - {@link UnsupportedRecord} is not known.
    * @throws FormatException if known record type cannot be parsed
    */

public static Record parse(NdefRecord ndefRecord) throws FormatException {
	short tnf = ndefRecord.getTnf();
	
	Record record = null;
	switch (tnf) {
       case NdefRecord.TNF_EMPTY: {
       	record = EmptyRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_WELL_KNOWN: {
       	record = parseWellKnown(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_MIME_MEDIA: {
       	record = MimeRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_ABSOLUTE_URI: {
       	record = AbsoluteUriRecord.parse(ndefRecord);
       	
       	break;
       }
       case NdefRecord.TNF_EXTERNAL_TYPE: {
       	record = ExternalTypeRecord.parse(ndefRecord);

       	break;
       }
       case NdefRecord.TNF_UNKNOWN: {
       	record = UnknownRecord.parse(ndefRecord);
       	
       	break;
       }
       /*
       case NdefRecord.TNF_UNCHANGED: {
       	throw new IllegalArgumentException("Chunked records no supported"); // chunks are abstracted away by android so should never happen
       }
       */
       	
	}

	if(record == null) { // pass through
		record = UnsupportedRecord.parse(ndefRecord);
	}
	
	if(ndefRecord.getId().length > 0) {
		record.setId(ndefRecord.getId());
	}
	
	return record;
}