title: Open browser on scan of NFC tag in Android
body: <p>I am trying to develop a use case such that when any user having a NFC enabled smartphone taps a NFC tag, the browser should open with the URL contained in the tag.</p>
  
  <p>At present I am using a Mifare Classic 1K tag that contains the URL <a href="http://www.google.com" rel="nofollow">http://www.google.com</a> that I have written in my NFC tag.</p>
  
  <p>Now when I tap/scan the tag from my Google Nexus 7 (2012) edition, the tag gets detected but the browser isn't showing up. Also on other devices including Samsung S3, S4 the tag isnt detected at all. Why is that so?</p>
  
  <p>Here is my code that does the writing and reading of tag,</p>
  
  <pre><code>   public class MainActivity extends Activity {
  
          @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
  
                //check for nfc
            adapter = NfcAdapter.getDefaultAdapter(this);
            PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
           tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
           writeTagFilters = new IntentFilter[] { tagDetected }; 
  
                   //write tag
                   write("http://www.google.com",tag);
  
     }
  
  
   private void write(String text, Tag tag) throws IOException, FormatException {
  
      NdefRecord[] records = { createRecord(text) };
      NdefMessage  message = new NdefMessage(records);
      // Get an instance of Ndef for the tag.
      Log.i("App","Tag:" +tag);
      Ndef ndef = Ndef.get(tag);
      // Enable I/O
      ndef.connect();
      // Write the message
      ndef.writeNdefMessage(message);
      // Close the connection
      ndef.close();
  }
  
  
  
     private NdefRecord createRecord(String text) throws UnsupportedEncodingException {
      String lang       = "en";
      byte[] textBytes  = text.getBytes();
      byte[] langBytes  = lang.getBytes("US-ASCII");
      int    langLength = langBytes.length;
      int    textLength = textBytes.length;
      byte[] payload    = new byte[1 + langLength + textLength];
  
      // set status byte (see NDEF spec for actual bits)
      payload[0] = (byte) langLength;
  
      // copy langbytes and textbytes into payload
      System.arraycopy(langBytes, 0, payload, 1,              langLength);
      System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
  
      NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,  NdefRecord.RTD_TEXT,  new byte[0], payload);
  
      return recordNFC;
  }
  
  
  @Override
  protected void onNewIntent(Intent intent){
      if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
          mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
          new NdefReaderTask().execute(mytag);
          Toast.makeText(this, this.getString(R.string.ok_detection) + mytag.toString(), Toast.LENGTH_LONG ).show();
      }
  }
  
  
      private class NdefReaderTask extends AsyncTask&lt;Tag, Void, String&gt; {
  
      @Override
      protected String doInBackground(Tag... params) {
          Tag tag = params[0];
  
          Ndef ndef = Ndef.get(tag);
          if (ndef == null) {
              // NDEF is not supported by this Tag. 
              return null;
          }
  
          NdefMessage ndefMessage = ndef.getCachedNdefMessage();
  
          NdefRecord[] records = ndefMessage.getRecords();
          for (NdefRecord ndefRecord : records) {
              if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN &amp;&amp; Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
                  try {
                      return readText(ndefRecord);
                  } catch (UnsupportedEncodingException e) {
                      Log.e("App", "Unsupported Encoding", e);
                  }
              }
          }
  
          return null;
      }
  
      private String readText(NdefRecord record) throws UnsupportedEncodingException {
          /*
           * See NFC forum specification for "Text Record Type Definition" at 3.2.1 
           * 
           * http://www.nfc-forum.org/specs/
           * 
           * bit_7 defines encoding
           * bit_6 reserved for future use, must be 0
           * bit_5..0 length of IANA language code
           */
  
          byte[] payload = record.getPayload();
  
          // Get the Text Encoding
          String textEncoding = ((payload[0] &amp; 128) == 0) ? "UTF-8" : "UTF-16";
  
          // Get the Language Code
          int languageCodeLength = payload[0] &amp; 0063;
  
          // String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
          // e.g. "en"
  
          // Get the Text
          return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
      }
  
      @Override
      protected void onPostExecute(String result) {
          if (result != null) {
  
              Toast.makeText(getApplicationContext(), "Read content: " +result,Toast.LENGTH_LONG).show(); 
              webView = (WebView) findViewById(R.id.webView1);
              webView.getSettings().setJavaScriptEnabled(true);
              webView.loadUrl(result);
          }
      }
  }
  </code></pre>
  
  <p>Manifest.xml</p>
  
  <pre><code>      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name" &gt;
      &lt;activity
          android:label="@string/app_name"
          android:name="com.framentos.hellonfc.MainActivity" &gt;
          &lt;intent-filter &gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  &lt;/application&gt;
  &lt;uses-permission android:name="android.permission.NFC" /&gt;
  </code></pre>
  
  <p>Also, do I need to have an app always present in the device for the browser to be invoked. As NFC is a service, shouldn't every NFC device open the browser on tap of tag.</p>