Java Code Examples for android.nfc.NdefRecord#TNF_EXTERNAL_TYPE

The following examples show how to use android.nfc.NdefRecord#TNF_EXTERNAL_TYPE . 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.NdefMessage to mojo NfcMessage
 */
public static NfcMessage toNfcMessage(NdefMessage ndefMessage)
        throws UnsupportedEncodingException {
    NdefRecord[] ndefRecords = ndefMessage.getRecords();
    NfcMessage nfcMessage = new NfcMessage();
    List<NfcRecord> nfcRecords = new ArrayList<NfcRecord>();

    for (int i = 0; i < ndefRecords.length; i++) {
        if ((ndefRecords[i].getTnf() == NdefRecord.TNF_EXTERNAL_TYPE)
                && (Arrays.equals(ndefRecords[i].getType(), WEBNFC_URN.getBytes("UTF-8")))) {
            nfcMessage.url = new String(ndefRecords[i].getPayload(), "UTF-8");
            continue;
        }

        NfcRecord nfcRecord = toNfcRecord(ndefRecords[i]);
        if (nfcRecord != null) nfcRecords.add(nfcRecord);
    }

    nfcMessage.data = new NfcRecord[nfcRecords.size()];
    nfcRecords.toArray(nfcMessage.data);
    return nfcMessage;
}
 
Example 2
Source File: ExternalTypeRecord.java    From effective_android_sample with Apache License 2.0 6 votes vote down vote up
@Deprecated
private NdefRecord createExternal(String domain, String type, byte[] data) {
    if (domain == null) throw new NullPointerException("domain is null");
    if (type == null) throw new NullPointerException("type is null");

    domain = domain.trim().toLowerCase(Locale.US);
    type = type.trim().toLowerCase(Locale.US);

    if (domain.length() == 0) throw new IllegalArgumentException("domain is empty");
    if (type.length() == 0) throw new IllegalArgumentException("type is empty");

    byte[] byteDomain = domain.getBytes(Charset.forName("UTF-8"));
    byte[] byteType = type.getBytes(Charset.forName("UTF-8"));
    byte[] b = new byte[byteDomain.length + 1 + byteType.length];
    System.arraycopy(byteDomain, 0, b, 0, byteDomain.length);
    b[byteDomain.length] = ':';
    System.arraycopy(byteType, 0, b, byteDomain.length + 1, byteType.length);

    // external type id must be empty
    return new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, b, EMPTY, data != null ? data : EMPTY);
}
 
Example 3
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 4
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;
}