Android Open Source - NFCAndroid Text Ndef Record






From Project

Back to project page NFCAndroid.

License

The source code is released under:

Copyright (c) 2012, Wireless Sensor Technologies, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project NFCAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gototags.nfc.ndef;
// w  w  w .  ja va  2 s . c om
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Bytes;

import android.nfc.NdefRecord;

public class TextNdefRecord extends ParsedNdefRecord {
  private Locale locale;
  private String text;
  
  public TextNdefRecord() {
    this.setLocale(Locale.ENGLISH);
  }
  
  public TextNdefRecord(String languageCode, String text) {
    this.setLocale(new Locale(languageCode));
    this.setText(text);
  }
  
  public TextNdefRecord(Locale locale, String text) {
    this.setLocale(locale);
    this.setText(text);
  }
  
  public Locale getLocale() {
    return this.locale;
  }

  public void setLocale(Locale locale) {
    this.locale = locale;
  }
  
  public String getText() {
    return this.text;
  }

  public void setText(String text) {
    this.text = text;
  }
  
  @Override
  public NdefRecord toNdefRecord() {
    Preconditions.checkNotNull(this.getLocale());
    Preconditions.checkNotNull(this.getText());
    
    try {
      byte[] languageBytes = this.getLocale().getLanguage().getBytes(Charset.forName("US_ASCII"));
      
      byte[] textBytes = text.getBytes(Charset.forName("UTF-8"));
      
      int utfBit = (1 << 7);
          char status = (char)(utfBit + languageBytes.length);
          
          byte[] data = Bytes.concat(new byte[]{ (byte)status }, languageBytes, textBytes);
      
          return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);      
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }
  
  public static TextNdefRecord fromNdefRecord(NdefRecord ndefRecord) {
    Preconditions.checkNotNull(ndefRecord);
    Preconditions.checkArgument(ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN);
    Preconditions.checkArgument(Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT));
    
    try {
      byte[] payload = ndefRecord.getPayload();
      
      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 TextNdefRecord(languageCode, text);      
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }
  
  public static boolean isTextNdefRecord(NdefRecord ndefRecord) {
    try {
      TextNdefRecord.fromNdefRecord(ndefRecord);
      return true;
    } catch (IllegalArgumentException illegalArgumentException) {
      return false;
    }
  }
}




Java Source Code List

com.gototags.nfc.Helper.java
com.gototags.nfc.NfcTag.java
com.gototags.nfc.app.BaseActivity.java
com.gototags.nfc.app.MainActivity.java
com.gototags.nfc.ndef.ParsedNdefRecord.java
com.gototags.nfc.ndef.TextNdefRecord.java
com.gototags.nfc.ndef.UriNdefRecord.java
com.gototags.nfc.records.NdefRecordParser.java
com.gototags.nfc.records.PhoneRecord.java
com.gototags.nfc.records.RecordType.java
com.gototags.nfc.records.Record.java
com.gototags.nfc.records.TextRecord.java
com.gototags.nfc.records.WebsiteRecord.java