Java Code Examples for android.nfc.NdefRecord#TNF_WELL_KNOWN
The following examples show how to use
android.nfc.NdefRecord#TNF_WELL_KNOWN .
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: WriteTextActivity.java From android-nfc with MIT License | 6 votes |
/** * 创建NDEF文本数据 * * @param text * @return */ public static NdefRecord createTextRecord(String text) { byte[] langBytes = Locale.CHINA.getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = Charset.forName("UTF-8"); //将文本转换为UTF-8格式 byte[] textBytes = text.getBytes(utfEncoding); //设置状态字节编码最高位数为0 int utfBit = 0; //定义状态字节 char status = (char) (utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; //设置第一个状态字节,先将状态码转换成字节 data[0] = (byte) status; //设置语言编码,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1到langBytes.length的位置 System.arraycopy(langBytes, 0, data, 1, langBytes.length); //设置文本字节,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1 + langBytes.length //到textBytes.length的位置 System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); //通过字节传入NdefRecord对象 //NdefRecord.RTD_TEXT:传入类型 读写 NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return ndefRecord; }
Example 2
Source File: ForegroundNdefPush.java From codeexamples-android with Eclipse Public License 1.0 | 6 votes |
public static NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) { byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16"); byte[] textBytes = text.getBytes(utfEncoding); int utfBit = encodeInUtf8 ? 0 : (1 << 7); char status = (char) (utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte) status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); }
Example 3
Source File: WriteUriActivity.java From android-nfc with MIT License | 6 votes |
/** * 将Uri转成NdefRecord * * @param uriStr * @return */ public static NdefRecord createUriRecord(String uriStr) { byte prefix = 0; for (Byte b : UriPrefix.URI_PREFIX_MAP.keySet()) { String prefixStr = UriPrefix.URI_PREFIX_MAP.get(b).toLowerCase(); if ("".equals(prefixStr)) continue; if (uriStr.toLowerCase().startsWith(prefixStr)) { prefix = b; uriStr = uriStr.substring(prefixStr.length()); break; } } byte[] data = new byte[1 + uriStr.length()]; data[0] = prefix; System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length()); NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], data); return record; }
Example 4
Source File: PNfc.java From PHONK with GNU General Public License v3.0 | 6 votes |
/** * Write text to a tag * * @param textToWrite the text to write */ public void write(String textToWrite) { Locale locale = Locale.US; final byte[] langBytes = locale.getLanguage().getBytes(StandardCharsets.UTF_8); final byte[] textBytes = textToWrite.getBytes(StandardCharsets.UTF_8); final int utfBit = 0; final char status = (char) (utfBit + langBytes.length); final byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte) status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); NdefRecord[] records = {record}; messageToWrite = new NdefMessage(records); }
Example 5
Source File: SmartPosterRecord.java From effective_android_sample with Apache License 2.0 | 6 votes |
@Override public NdefRecord getNdefRecord() { Message message = new Message(); if (hasTitle()) { message.add(title); } if (hasUri()) { message.add(uri); } if (hasAction()) { message.add(action); } return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_SMART_POSTER, id != null ? id : EMPTY, message.getNdefMessage().toByteArray()); }
Example 6
Source File: NfcTypeConverter.java From 365browser with Apache License 2.0 | 6 votes |
/** * 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 7
Source File: GenericControlRecord.java From effective_android_sample with Apache License 2.0 | 6 votes |
@Override public NdefRecord getNdefRecord() { if(!hasTarget()) { throw new IllegalArgumentException("Expected target"); } List<NdefRecord> records = new ArrayList<NdefRecord>(); records.add(target.getNdefRecord()); if (hasAction()) { records.add(action.getNdefRecord()); } if (hasData()) { records.add(data.getNdefRecord()); } byte[] array = new NdefMessage(records.toArray(new NdefRecord[records.size()])).toByteArray(); byte[] payload = new byte[array.length + 1]; payload[0] = configurationByte; System.arraycopy(array, 0, payload, 1, array.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, payload); }
Example 8
Source File: GcTargetRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
@Override public NdefRecord getNdefRecord() { if (!hasTargetIdentifier()) { throw new IllegalArgumentException("Expected target identifier"); } return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, targetIdentifier.toByteArray()); }
Example 9
Source File: ActionRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
@Override public NdefRecord getNdefRecord() { if (!hasAction()) { throw new IllegalArgumentException("Expected action"); } return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, new byte[] {action.getValue()}); }
Example 10
Source File: NdefRecordUtil.java From commcare-android with Apache License 2.0 | 5 votes |
public static NdefRecord createTextRecordManually(String payload, Locale locale) { byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII")); byte[] textBytes = payload.getBytes(UTF8_CHARSET); int utfBit = 0; char status = (char)(utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte)status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); }
Example 11
Source File: NdefRecordUtil.java From commcare-android with Apache License 2.0 | 5 votes |
/** * * @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 12
Source File: HandoverSelectRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
@Override public NdefRecord getNdefRecord() { // implementation note: write alternative carriers and error record together List<NdefRecord> records = new ArrayList<NdefRecord>(); if (hasAlternativeCarriers()) { // n alternative carrier records for(Record record : alternativeCarriers) { records.add(record.getNdefRecord()); } } if (hasError()) { // an error message records.add(error.getNdefRecord()); } byte[] subPayload = new NdefMessage(records.toArray(new NdefRecord[records.size()])).toByteArray(); byte[] payload = new byte[subPayload.length + 1]; // major version, minor version payload[0] = (byte)((majorVersion << 4) | minorVersion); System.arraycopy(subPayload, 0, payload, 1, subPayload.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_SELECT, id != null ? id : EMPTY, payload); }
Example 13
Source File: ReadUriActivity.java From android-nfc with MIT License | 5 votes |
/** * 解析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 14
Source File: TextRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
@Override public NdefRecord getNdefRecord() { if(!hasLocale()) { throw new IllegalArgumentException("Expected locale"); } if(!hasEncoding()) { throw new IllegalArgumentException("Expected encoding"); } if(!hasText()) { throw new IllegalArgumentException("Expected text"); } byte[] languageData = (locale.getLanguage() + (locale.getCountry() == null || locale.getCountry().length() == 0 ? "" : ("-" + locale.getCountry()))).getBytes(); if (languageData.length > TextRecord.LANGUAGE_CODE_MASK) { throw new IllegalArgumentException("Expected language code length <= 32 bytes, not " + languageData.length + " bytes"); } byte[] textData = text.getBytes(encoding); byte[] payload = new byte[1 + languageData.length + textData.length]; byte status = (byte)(languageData.length | (TextRecord.UTF16.equals(encoding) ? 0x80 : 0x00)); payload[0] = status; System.arraycopy(languageData, 0, payload, 1, languageData.length); System.arraycopy(textData, 0, payload, 1 + languageData.length, textData.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, id != null ? id : EMPTY, payload); }
Example 15
Source File: HandoverCarrierRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public static HandoverCarrierRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException { byte[] payload = ndefRecord.getPayload(); CarrierTypeFormat carrierTypeFormat = CarrierTypeFormat.toCarrierTypeFormat((byte)(payload[0] & 0x7)); HandoverCarrierRecord handoverCarrierRecord = new HandoverCarrierRecord(); handoverCarrierRecord.setCarrierTypeFormat(carrierTypeFormat); int carrierTypeLength = (int)(payload[1] & 0xFF); switch (carrierTypeFormat) { case WellKnown: { // NFC Forum well-known type [NFC RTD] // parse records 'manually' here, so that we can check the tnf type instead of the class type byte[] recordsPayload = new byte[carrierTypeLength]; System.arraycopy(payload, 2, recordsPayload, 0, carrierTypeLength); NdefMessage message = new NdefMessage(recordsPayload); NdefRecord[] records = message.getRecords(); if(records.length != 1) { throw new IllegalArgumentException(); } if(records[0].getTnf() != NdefRecord.TNF_WELL_KNOWN) { throw new IllegalArgumentException("Expected well-known type carrier type"); } handoverCarrierRecord.setCarrierType(Record.parse(records[0])); break; } case Media: { // Media-type as defined in RFC 2046 [RFC 2046] handoverCarrierRecord.setCarrierType(new String(payload, 2, carrierTypeLength, Charset.forName("US-ASCII"))); break; } case AbsoluteURI: { // Absolute URI as defined in RFC 3986 [RFC 3986] handoverCarrierRecord.setCarrierType(new String(payload, 2, carrierTypeLength, Charset.forName("US-ASCII"))); break; } case External: { // NFC Forum external type [NFC RTD] Record record = Record.parse(payload, 2, carrierTypeLength); if (record instanceof ExternalTypeRecord) { handoverCarrierRecord.setCarrierType(record); } else { throw new IllegalArgumentException("Expected external type carrier type, not " + record.getClass().getSimpleName()); } } default: { throw new RuntimeException(); } } // The number of CARRIER_DATA octets is equal to the NDEF record PAYLOAD_LENGTH minus the CARRIER_TYPE_LENGTH minus 2. int carrierDataLength = payload.length - 2 - carrierTypeLength; byte[] carrierData; if (carrierDataLength > 0) { carrierData = new byte[carrierDataLength]; System.arraycopy(payload, 2 + carrierTypeLength, carrierData, 0, carrierDataLength); } else { carrierData = null; } handoverCarrierRecord.setCarrierData(carrierData); return handoverCarrierRecord; }
Example 16
Source File: AlternativeCarrierRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
@Override public NdefRecord getNdefRecord() { ByteArrayOutputStream bout = new ByteArrayOutputStream(); // cps if (!hasCarrierPowerState()) { throw new IllegalArgumentException("Expected carrier power state"); } bout.write(carrierPowerState.getValue() & 0x7); // 3 lsb // carrier data reference: 1 if (!hasCarrierDataReference()) { throw new IllegalArgumentException("Expected carrier data reference"); } byte[] carrierDataReferenceChar = carrierDataReference.getBytes(Charset.forName("US-ASCII")); if (carrierDataReferenceChar.length > 255) { throw new IllegalArgumentException("Expected carrier data reference '" + carrierDataReference + "' <= 255 bytes"); } // carrier data reference length (1) bout.write(carrierDataReferenceChar.length); // carrier data reference char bout.write(carrierDataReferenceChar, 0, carrierDataReferenceChar.length); // auxiliary data reference count bout.write(auxiliaryDataReferences.size()); for (String auxiliaryDataReference : auxiliaryDataReferences) { byte[] auxiliaryDataReferenceChar = auxiliaryDataReference.getBytes(Charset.forName("US-ASCII")); // carrier data reference length (1) if (auxiliaryDataReferenceChar.length > 255) { throw new IllegalArgumentException("Expected auxiliary data reference '" + auxiliaryDataReference + "' <= 255 bytes"); } bout.write(auxiliaryDataReferenceChar.length); // carrier data reference char bout.write(auxiliaryDataReferenceChar, 0, auxiliaryDataReferenceChar.length); } // reserved future use bout.write(0); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_ALTERNATIVE_CARRIER, id != null ? id : EMPTY, bout.toByteArray()); }
Example 17
Source File: SignatureRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
@Override public NdefRecord getNdefRecord() { if(isStartMarker()) { return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, new byte[]{0x01, 0x00});// version 1 and type 0 } else { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(version); if(!hasSignatureType()) { throw new IllegalArgumentException("Expected signature type"); } if(hasSignature() && hasSignatureUri()) { throw new IllegalArgumentException("Expected signature or signature uri, not both"); } else if(!hasSignature() && !hasSignatureUri()) { throw new IllegalArgumentException("Expected signature or signature uri"); } baos.write(((hasSignatureUri() ? 1 : 0) << 7) | (signatureType.getValue() & 0x7F)); byte[] signatureOrUri; if(hasSignature()) { signatureOrUri = signature; if(signatureOrUri.length > 65535) { throw new IllegalArgumentException("Expected signature size " + signatureOrUri.length + " <= 65535"); } } else { signatureOrUri = signatureUri.getBytes(Charset.forName("UTF-8")); if(signatureOrUri.length > 65535) { throw new IllegalArgumentException("Expected signature uri byte size " + signatureOrUri.length + " <= 65535"); } } baos.write((signatureOrUri.length >> 8) & 0xFF); baos.write(signatureOrUri.length & 0xFF); baos.write(signatureOrUri); if(!hasCertificateFormat()) { throw new IllegalArgumentException("Expected certificate format"); } if(certificates.size() > 16) { throw new IllegalArgumentException("Expected number of certificates " + certificates.size() + " <= 15"); } baos.write(((hasCertificateUri() ? 1 : 0) << 7) | (certificateFormat.getValue() << 4) | (certificates.size() & 0xF)); for(int i = 0; i < certificates.size(); i++) { byte[] certificate = certificates.get(i); if(certificate.length > 65535) { throw new IllegalArgumentException("Expected certificate " + i + " size " + certificate.length + " <= 65535"); } baos.write((certificate.length >> 8) & 0xFF); baos.write(certificate.length & 0xFF); baos.write(certificate); } if(hasCertificateUri()) { byte[] certificateUriBytes = certificateUri.getBytes(Charset.forName("UTF-8")); if(certificateUriBytes.length > 65535) { throw new IllegalArgumentException("Expected certificate uri byte size " + certificateUriBytes.length + " <= 65535"); } baos.write((certificateUriBytes.length >> 8) & 0xFF); baos.write(certificateUriBytes.length & 0xFF); baos.write(certificateUriBytes); } return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, baos.toByteArray()); } catch (IOException e) { throw new RuntimeException(e); } } }
Example 18
Source File: ErrorRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
@Override public NdefRecord getNdefRecord() { if (!hasErrorReason()) { throw new IllegalArgumentException("Expected error reason"); } if (!hasErrorData()) { throw new IllegalArgumentException("Expected error data"); } byte[] payload; switch (errorReason) { case TemporaryMemoryConstraints: { /** * An 8-bit unsigned integer that expresses the minimum number of milliseconds after which a Handover * Request Message with the same number of octets might be processed successfully. The number of * milliseconds SHALL be determined by the time interval between the sending of the error indication and * the subsequent receipt of a Handover Request Message by the Handover Selector. */ payload = new byte[] { errorReason.getValue(), (byte)(errorData.shortValue() & 0xFF) }; break; } case PermanenteMemoryConstraints: { /** * A 32-bit unsigned integer, encoded with the most significant byte first, that indicates the maximum * number of octets of an acceptable Handover Select Message. The number of octets SHALL be determined * by the total length of the NDEF message, including all header information. */ long unsignedInt = errorData.longValue(); payload = new byte[] { errorReason.getValue(), (byte)((unsignedInt >> 24) & 0xFF), (byte)((unsignedInt >> 16) & 0xFF), (byte)((unsignedInt >> 8) & 0xFF), (byte)(unsignedInt & 0xFF) }; break; } case CarrierSpecificConstraints: { /** * An 8-bit unsigned integer that expresses the minimum number of milliseconds after which a Handover * Request Message might be processed successfully. The number of milliseconds SHALL be determined by * the time interval between the sending of the error indication and the subsequent receipt of a * Handover Request Message by the Handover Selector. */ payload = new byte[] { errorReason.getValue(), (byte)(errorData.shortValue() & 0xFF) }; break; } default : { throw new IllegalArgumentException("Unknown error reason " + errorReason); } } return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, type, id != null ? id : EMPTY, payload); }
Example 19
Source File: Record.java From effective_android_sample with Apache License 2.0 | 4 votes |
/** * 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; }
Example 20
Source File: NfcManager.java From NFCard with GNU General Public License v3.0 | 3 votes |
NdefMessage createNdefMessage() { String uri = "3play.google.com/store/apps/details?id=com.sinpo.xnfc"; byte[] data = uri.getBytes(); // about this '3'.. see NdefRecord.createUri which need api level 14 data[0] = 3; NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, null, data); return new NdefMessage(new NdefRecord[] { record }); }