title: Reading an NDEF message from an NFC tag from an Android application
body: <p>I am trying to create an application using NFC and I just want to try and read an NFC tag and get the text message from the tag and place it into a TextView. I have code for it already, but nothing happens when I try to pair the phone with an NFC tag.</p>
  
  <p>Here is my code and could someone please look at it and see what I am doing wrong and what needs to be done to fix the issue please:</p>
  
  <pre><code>Button measurementsDataButton;
  NfcAdapter myNfcAdapter;
  PendingIntent myPendingIntent;
  IntentFilter ndef;
  IntentFilter[] filters;
  String[][] techLists;
  int mCount;
  TextView mText;
  String payload;
  byte payloadHeader;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_nfc_scanner);
  
      final ActionBar actionBar = getActionBar();
      actionBar.setDisplayHomeAsUpEnabled(true);
  
      mText = (TextView) findViewById(R.id.flowTextView1);
  
      measurementsDataButton = (Button) findViewById(R.id.measurementsButton1);
      measurementsDataButton.setOnClickListener(this);
  
      myNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  
      myPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  
      ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
      filters = new IntentFilter[] {ndef, };
      techLists = new String[][] {new String[] {Ndef.class.getName()}, new String[] {NdefFormatable.class.getName()}};
  }
  
  @Override
  public void onPause() {
      super.onPause();
      myNfcAdapter.disableForegroundDispatch(this);
  }
  
  @Override
  public void onResume() {
      super.onResume();
  
      if(myNfcAdapter != null) {
  
          myNfcAdapter.enableForegroundDispatch(this, myPendingIntent, filters, techLists);
  
      }
  
  }
  
  @Override
  public void onNewIntent(Intent intent) {
  
      if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()))
      {
          NdefMessage [] messages = getNdefMessages(getIntent());
          for(int i = 0; i&lt;messages.length; i++) 
          {
              for(int j = 0; j&lt;messages[0].getRecords().length; j++)
              {
                  NdefRecord record = messages[i].getRecords()[j];
                  payload = new String(record.getPayload(), 1, record.getPayload().length-1, Charset.forName("UTF-8"));
                  mText.setText(payload);
                  payloadHeader = record.getPayload()[0];
              }
          }
      }
  }
  
  NdefMessage[] getNdefMessages(Intent intent) {
      // TODO Auto-generated method stub
  
      NdefMessage[] message = null;
      if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
  
          Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
  
          if(rawMessages != null) {
  
              message = new NdefMessage[rawMessages.length];
              for(int i = 0; i &lt; rawMessages.length; i++) {
  
                  message[i] = (NdefMessage) rawMessages[i];
  
              }
          }
              else {
                  byte[] empty = new byte[] {};
                  NdefRecord record = new NdefRecord (NdefRecord.TNF_UNKNOWN, empty, empty, empty);
                  NdefMessage msg = new NdefMessage (new NdefRecord[] {record});
                  message = new NdefMessage[] {msg};
              }
  
          }
              else {
                  Log.d("", "Unknown intent.");
                  finish();
                  }
  
  
      return message;
  }
  </code></pre>