title: connection to a NFC-tag in an android app
body: <p>I'm trying to write an android app for may Nexus S that is able to write a simple Text Record on a NFC-Tag. My problem is the connection-establishment to the NFC-Tag.
  I've implemented two activities called "TagWriter" &amp; "TagWriterStartPage".</p>
  
  <p>Here ist the "TagWriter"-activity:</p>
  
  <pre><code>package nfc.example.writer;
  
  import java.util.Locale;
  
  import com.google.common.base.Charsets;
  import com.google.common.primitives.Bytes;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.nfc.NdefMessage;
  import android.nfc.NdefRecord;
  import android.nfc.NfcAdapter;
  import android.nfc.Tag;
  import android.nfc.tech.Ndef;
  import android.nfc.tech.NdefFormatable;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class TagWriter extends Activity 
  {
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tagwriter);
  
          Intent intent = this.getIntent();
  
          Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
          writeTag(tag);
      }
  
      private void writeTag(Tag t) 
      {
          Ndef tag = Ndef.get(t);  
          Locale locale = Locale.US;
          final byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
          String text = "Tag, you're it!";
          final byte[] textBytes = text.getBytes(Charsets.UTF_8);
          final int utfBit = 0;
          final char status = (char) (utfBit + langBytes.length);
          final byte[] data = Bytes.concat(new byte[] {(byte) status}, langBytes, textBytes);
          NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
          try 
          {
              NdefRecord[] records = {record};
              NdefMessage message = new NdefMessage(records);
              tag.connect();
              boolean connected = tag.isConnected();
              boolean writeable = tag.isWritable();
              if( connected &amp;&amp; writeable)
              {   
                  tag.writeNdefMessage(message);
              }
              tag.close();
          }
          catch (Exception e)
          {
              //do error handling
          }
      }
  }
  </code></pre>
  
  <p>The activity "TagWriter" is called when I put my device on the NFC-tag. Everytime the method "tag.connect()" is called, there occures an exception.</p>
  
  <p>Can anyone help me to solve the problem???</p>
  
  <p>Thanks,
  Dennis</p>