public TextRecord Parse(NdefRecord record) { Preconditions.CheckArgument(record.Tnf == NdefRecord.TnfWellKnown); var typeInfo = record.GetTypeInfo(); if (record.GetTypeInfo()[0] != NdefRecord.RtdText[0]) { throw new Exception(); } //Preconditions.CheckArgument(Arrays.Equals(record.GetTypeInfo()[0], NdefRecord.RtdText[0])); // Preconditions.CheckArgument(Array.Equals(record.GetType(), NdefRecord.RtdText)); var payload = record.GetPayload(); string textEncoding = (payload[0] & 0200) == 0 ? "UTF-8" : "UTF-16"; int languageCodeLength = payload[0] & 0077; Encoding enc = Encoding.GetEncoding("US-ASCII"); Encoding encText = Encoding.GetEncoding(textEncoding); string languageCode = enc.GetString(payload, 0, languageCodeLength); string text = encText.GetString(payload, languageCodeLength + 1, payload.Length - languageCodeLength - 1); return(new TextRecord(text)); /* * String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; * int languageCodeLength = payload[0] & 0077; * String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); * String text = * new String(payload, languageCodeLength + 1, * payload.length - languageCodeLength - 1, textEncoding); * return new TextRecord(languageCode, text); */ }
private UriRecord ParseWellKnown(NdefRecord record) { //Preconditions.CheckArgument(Arrays.Equals(record.GetTypeInfo(), NdefRecord.RtdUri)); if (record.GetTypeInfo()[0] != NdefRecord.RtdUri[0]) { throw new Exception(); } // Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI)); // byte[] payload = record.getPayload(); /// * // * payload[0] contains the URI Identifier Code, per the // * NFC Forum "URI Record Type Definition" section 3.2.2. // * // * payload[1]...payload[payload.length - 1] contains the rest of // * the URI. // */ // String prefix = URI_PREFIX_MAP.get(payload[0]); // byte[] fullUri = // Bytes.concat(prefix.getBytes(Charset.forName("UTF-8")), Arrays.copyOfRange(payload, 1, // payload.length)); // Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8"))); // return new UriRecord(uri); var payload = record.GetPayload(); string prefix = Utility.UriMap(payload[0]); Encoding enc = Encoding.GetEncoding("UTF-8"); string text = enc.GetString(payload, 1, payload.Length - 1); return(new UriRecord(prefix + text)); }
internal SmartPoster Parse(NdefRecord record) { /* * * Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN); * Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_SMART_POSTER)); * try { * NdefMessage subRecords = new NdefMessage(record.getPayload()); * return parse(subRecords.getRecords()); * } catch (FormatException e) { * throw new IllegalArgumentException(e); * } */ Preconditions.CheckArgument(record.Tnf == NdefRecord.TnfWellKnown); //Preconditions.CheckArgument(Arrays.Equals(record.GetTypeInfo(), NdefRecord.RtdSmartPoster)); if (record.GetTypeInfo()[0] != NdefRecord.RtdSmartPoster[0]) { throw new Exception(); } NdefMessage subRecords = new NdefMessage(record.GetPayload()); return(Parse(subRecords.GetRecords())); }