Java Code Examples for android.nfc.NdefRecord#getPayload()
The following examples show how to use
android.nfc.NdefRecord#getPayload() .
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: AddFriendQRActivity.java From NaviBee with GNU General Public License v3.0 | 6 votes |
private void handleNfcIntent(Intent NfcIntent) { if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(NfcIntent.getAction())) { Parcelable[] receivedArray = NfcIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if(receivedArray != null) { NdefMessage receivedMessage = (NdefMessage) receivedArray[0]; NdefRecord[] attachedRecords = receivedMessage.getRecords(); for (NdefRecord record: attachedRecords) { String string = new String(record.getPayload()); // Make sure we don't pass along our AAR (Android Application Record) if (string.equals(getPackageName())) continue; addFriend(string); } } } }
Example 2
Source File: BeamActivity.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_beam); // Listing 18-24: Extracting the Android Beam payload Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (messages != null) { NdefMessage message = (NdefMessage) messages[0]; if (message != null) { NdefRecord record = message.getRecords()[0]; String payload = new String(record.getPayload()); Log.d(TAG, "Payload: " + payload); } } }
Example 3
Source File: BlogViewer.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
private void processIntent(Intent intent) { // Listing 18-18: Extracting NFC tag payloads String action = intent.getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (messages != null) { for (Parcelable eachMessage : messages) { NdefMessage message = (NdefMessage) eachMessage; NdefRecord[] records = message.getRecords(); if (records != null) { for (NdefRecord record : records) { String payload = new String(record.getPayload()); Log.d(TAG, payload); } } } } } }
Example 4
Source File: UnknownRecord.java From effective_android_sample with Apache License 2.0 | 6 votes |
public static Record parse(NdefRecord ndefRecord) { /** The value 0x05 (Unknown) SHOULD be used to indicate that the type of the payload is unknown. This is similar to the "application/octet-stream" media type defined by MIME [RFC 2046]. When used, the TYPE_LENGTH field MUST be zero and thus the TYPE field is omitted from the NDEF record. Regarding implementation, it is RECOMMENDED that an NDEF parser receiving an NDEF record of this type, without further context to its use, provides a mechanism for storing but not processing the payload (see section 4.2). */ // check that type is zero length byte[] type = ndefRecord.getType(); if(type != null && type.length > 0) { throw new IllegalArgumentException("Record type not expected"); } return new UnknownRecord(ndefRecord.getPayload()); }
Example 5
Source File: UriRecord.java From effective_android_sample with Apache License 2.0 | 6 votes |
@SuppressLint("NewApi") public static UriRecord parseNdefRecord(NdefRecord ndefRecord) { if (android.os.Build.VERSION.SDK_INT >= 16) { return new UriRecord(ndefRecord.toUri()); } else { byte[] payload = ndefRecord.getPayload(); if (payload.length < 2) { return null; } // payload[0] contains the URI Identifier Code, as per // NFC Forum "URI Record Type Definition" section 3.2.2. int prefixIndex = (payload[0] & (byte)0xFF); if (prefixIndex < 0 || prefixIndex >= URI_PREFIX_MAP.length) { return null; } String prefix = URI_PREFIX_MAP[prefixIndex]; String suffix = new String(Arrays.copyOfRange(payload, 1, payload.length), Charset.forName("UTF-8")); return new UriRecord(Uri.parse(prefix + suffix)); } }
Example 6
Source File: GcActionRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
public static GcActionRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException { byte[] payload = ndefRecord.getPayload(); if ((payload[0] & GcActionRecord.NUMERIC_CODE) != 0) { return new GcActionRecord(Action.getActionByValue(payload[1])); } else { return new GcActionRecord(Record.parse(payload, 1, payload.length - 1)); } }
Example 7
Source File: ExternalTypeRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
public static ExternalTypeRecord parse(NdefRecord ndefRecord) { String domainType = new String(ndefRecord.getType(), Charset.forName("UTF-8")); int colon = domainType.lastIndexOf(':'); String type; String domain; if(colon == -1) { domain = domainType; type = null; } else { domain = domainType.substring(0, colon); if(colon + 1 < domainType.length()) { type = domainType.substring(colon + 1); } else { type = ""; } } if(domain.equals(AndroidApplicationRecord.DOMAIN) && type.equals(AndroidApplicationRecord.TYPE)) { return new AndroidApplicationRecord(ndefRecord.getPayload()); } // see if there is a custom parser if(pluginExternalTypeParser != null) { if(pluginExternalTypeParser.canParse(domain, type)) { ExternalTypeRecord record = pluginExternalTypeParser.parse(domain, type, ndefRecord.getPayload()); if(record == null) { throw new IllegalArgumentException("External Type record " + domainType + " cannot be null"); } return record; } } return new GenericExternalTypeRecord(domain, type, ndefRecord.getPayload()); }
Example 8
Source File: AlternativeCarrierRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
public static AlternativeCarrierRecord parseNdefRecord(NdefRecord ndefRecord) { byte[] payload = ndefRecord.getPayload(); AlternativeCarrierRecord alternativeCarrierRecord = new AlternativeCarrierRecord(); // cps alternativeCarrierRecord.setCarrierPowerState(CarrierPowerState.toCarrierPowerState(payload[0])); // carrier data reference short carrierDataReferenceLength = (short)payload[1]; alternativeCarrierRecord.setCarrierDataReference(new String(payload, 2, carrierDataReferenceLength, Charset.forName("US-ASCII"))); // auxiliary data reference short auxiliaryDataReferenceCount = (short)payload[2 + carrierDataReferenceLength]; int index = 2 + carrierDataReferenceLength + 1; for (int i = 0; i < auxiliaryDataReferenceCount; i++) { short auxiliaryDataReferenceLength = (short)payload[index]; alternativeCarrierRecord.addAuxiliaryDataReference(new String(payload, index + 1, auxiliaryDataReferenceLength, Charset.forName("US-ASCII"))); index += 1 + auxiliaryDataReferenceLength; } // reserved end byte not checked return alternativeCarrierRecord; }
Example 9
Source File: NFCHandler.java From timelapse-sony with GNU General Public License v3.0 | 5 votes |
private static Pair<String, String> decodeSonyPPMMessage(NdefRecord ndefRecord) { if (!SONY_MIME_TYPE.equals(new String(ndefRecord.getType()))) { return null; } try { byte[] payload = ndefRecord.getPayload(); int ssidBytesStart = 8; int ssidLength = payload[ssidBytesStart]; byte[] ssidBytes = new byte[ssidLength]; int ssidPointer = 0; for (int i = ssidBytesStart + 1; i <= ssidBytesStart + ssidLength; i++) { ssidBytes[ssidPointer++] = payload[i]; } String ssid = new String(ssidBytes); int passwordBytesStart = ssidBytesStart + ssidLength + 4; int passwordLength = payload[passwordBytesStart]; byte[] passwordBytes = new byte[passwordLength]; int passwordPointer = 0; for (int i = passwordBytesStart + 1; i <= passwordBytesStart + passwordLength; i++) { passwordBytes[passwordPointer++] = payload[i]; } String password = new String(passwordBytes); return new Pair<>(ssid, password); } catch (Exception e) { return null; } }
Example 10
Source File: NfcReadUtilityImpl.java From android-nfc-lib with MIT License | 5 votes |
private String parseAccordingToType(NdefRecord obj) { if (Arrays.equals(obj.getType(), NfcType.BLUETOOTH_AAR)) { byte[] toConvert = obj.getPayload(); StringBuilder result = new StringBuilder(); for (int i = toConvert.length - 1; i >= 2; i--) { byte temp = toConvert[i]; String tempString = ((temp < 0) ? Integer.toHexString(temp + Byte.MAX_VALUE) : Integer.toHexString(temp)); result.append((tempString.length() < 2) ? "0" + tempString : tempString); result.append(":"); } return !(result.length() == 0) ? result.substring(0, result.length() - 1) : result.toString(); } return Uri.parse(parseAccordingToHeader(obj.getPayload())).toString(); }
Example 11
Source File: NdefRecordUtil.java From commcare-android with Apache License 2.0 | 5 votes |
private static String readValueFromTextRecord(NdefRecord textTypeRecord) { byte[] fullPayload = textTypeRecord.getPayload(); // The payload includes a prefix denoting the language, so we need to parse that off int langBytesLength = fullPayload[0]; // status byte int lengthOfPrefix = langBytesLength + 1; // add 1 for the status byte itself byte[] payloadWithoutLang = Arrays.copyOfRange(fullPayload, lengthOfPrefix, fullPayload.length); return new String(payloadWithoutLang, UTF8_CHARSET); }
Example 12
Source File: ReadTextActivity.java From android-nfc with MIT License | 5 votes |
/** * 解析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 13
Source File: NFCTagReadWriteManager.java From PhoneProfilesPlus with Apache License 2.0 | 4 votes |
private String ndefRecordToString(NdefRecord record) { byte[] payload = record.getPayload(); return new String(payload); }
Example 14
Source File: MimeRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public static MimeRecord parse(NdefRecord ndefRecord) { String contentType = new String(ndefRecord.getType(), Charset.forName("US-ASCII")); // http://www.ietf.org/rfc/rfc2046.txt return new MimeRecord(contentType, ndefRecord.getPayload()); }
Example 15
Source File: UnsupportedRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public UnsupportedRecord(NdefRecord record) { this(record.getTnf(), record.getType(), record.getId(), record.getPayload()); }
Example 16
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 17
Source File: HandoverSelectRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public static HandoverSelectRecord parseNdefRecord(NdefRecord ndefRecord) throws FormatException { byte[] payload = ndefRecord.getPayload(); HandoverSelectRecord handoverSelectRecord = new HandoverSelectRecord(); byte minorVersion = (byte)(payload[0] & 0x0F); byte majorVersion = (byte)((payload[0] >> 4) & 0x0F); handoverSelectRecord.setMinorVersion(minorVersion); handoverSelectRecord.setMajorVersion(majorVersion); // The Handover Selector MAY acknowledge zero, one, or more of the proposed alternative carriers at its own discretion. if(payload.length > 1) { normalizeMessageBeginEnd(payload, 1, payload.length -1); List<Record> records = Message.parseNdefMessage(payload, 1, payload.length -1); // Only Alternative Carrier Records and Error Records have a defined meaning in the payload of a Handover Select Record. // However, an implementation SHALL NOT raise an error if it encounters other record types, but SHOULD silently ignore them. for (int i = 0; i < records.size(); i++) { Record record = records.get(i); if (record instanceof AlternativeCarrierRecord) { handoverSelectRecord.add((AlternativeCarrierRecord)record); } else if (record instanceof ErrorRecord) { if (i == records.size() - 1) { handoverSelectRecord.setError((ErrorRecord)record); } else { // ignore } } else { // ignore } } } return handoverSelectRecord; }
Example 18
Source File: ErrorRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public static ErrorRecord parseNdefRecord(NdefRecord ndefRecord) { byte[] payload = ndefRecord.getPayload(); ErrorReason errorReason = ErrorReason.toErrorReason(payload[0]); ErrorRecord errorRecord = new ErrorRecord(); errorRecord.setErrorReason(errorReason); Number number; 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. */ number = Short.valueOf((short)(payload[1] & 0xFFFF)); 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. */ number = Long.valueOf(((long)(payload[1] & 0xFF) << 24) + ((payload[2] & 0xFF) << 16) + ((payload[3] & 0xFF) << 8) + ((payload[4] & 0xFF) << 0)); 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. */ number = Short.valueOf((short)(payload[1] & 0xFFFF)); break; } default: { throw new RuntimeException(); } } errorRecord.setErrorData(number); return errorRecord; }
Example 19
Source File: UnsupportedRecord.java From effective_android_sample with Apache License 2.0 | 4 votes |
public static UnsupportedRecord parse(NdefRecord ndefRecord) { return new UnsupportedRecord(ndefRecord.getTnf(), ndefRecord.getType(), ndefRecord.getId(), ndefRecord.getPayload()); }
Example 20
Source File: ReadUriActivity.java From android-nfc with MIT License | 3 votes |
/** * 处理绝对的Uri * 没有Uri识别码,也就是没有Uri前缀,存储的全部是字符串 * * @param ndefRecord 描述NDEF信息的一个信息段,一个NdefMessage可能包含一个或者多个NdefRecord * @return */ private static Uri parseAbsolute(NdefRecord ndefRecord) { //获取所有的字节数据 byte[] payload = ndefRecord.getPayload(); Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8"))); return uri; }