title: how to scan an unformatted NFC tag
body: <p>I've an app that scans NFC tags to get information from the payload and then sends that info to a server via web service. It works fine. The tags are Mifare. The mobile part of the system before i joined the company was written in J2me for nokia c700. I've re-written the system in Android. The Android app can get the tag ID and read the payload. If there is no payload then it just reads the tag ID, which is fine. </p>
  
  <p>The problem is when i scan a tag that has not been formatted then i can't get the tagID. The old system on the nokias could read a tagID from an <strong>unformatted</strong> tag. How can i do this in Android? Why does the tag have to be formatted?</p>
  
  <p>Thanks in advance.</p>
  
  <p>[edit]</p>
  
  <pre><code>if( intentAction.equalsIgnoreCase(NFC_ACTION)){
  
               nfcScanTimeFromDB = null;
  
              cursor = nfcscannerapplication.loginValidate.getLastTagFromTransations();
  
              if (cursor.getCount() &gt; 0) {
                  if (cursor.moveToLast()) {
  
                      _tagScanTime = cursor.getLong(cursor
                              .getColumnIndex(LoginValidate.C_TAG_SCAN_TIME));
  
                      nfcScanTimeFromDB = new DateTime(_tagScanTime);
  
                  }
              }
  
              if(cursor.getCount() == 0){
                  //DB must be empty
                  Log.e(TAG, "just scanned an nfc tag and DB must be empty");
                  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  
                  tagId = bytesToHexString(tag.getId());
                  Log.e(TAG, "tagId immediately after scanning nfc tag = " + tagId);
  
                  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  
                      Log.e(TAG, "NFC Tag scanned");
  
                      // get the messages from the intent
                      Parcelable[] rawMsgs = intent
                              .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                      if (rawMsgs != null) {
                          msgs = new NdefMessage[rawMsgs.length];
                          for (int i = 0; i &lt; rawMsgs.length; i++) {
                              msgs[i] = (NdefMessage) rawMsgs[i];
                          }
                      }
                  } else {
                      Log.e(TAG, "ndef not discovered!!!!!!");
                  }
  
                   Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
                    if(c.getCount() == 0){
  
                        Toast.makeText(this, "Please login before signing in",
                                  Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(NfcscannerActivity.this, EntryActivity.class);
                        startActivity(intent);
                    }else{
                        c.moveToLast();
                        tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
                    }
  
  
  
  
                  // process the msgs array
                  for (int i = 0; i &lt; msgs.length; i++) {
  
                      NdefRecord[] records = msgs[i].getRecords();
                      Log.e(TAG, "ndefrecord has a length of " + records.length);
  
                      tr = parse(records[i]);
                      payload = tr.getText();
  
                      Log.e(TAG, "TextRecord.text = " + tr.getText());
  
  
                  }
  
                  if(payload.length() == 0){
  
                      tagType = "1";
                      tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
                      tagPerson = tagId;//c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
                      tagUserName = tagId;
                      tagLatitude = "0.0000000";
                      tagLongitude = "0.0000000"; 
  
                      if(rotasOnly.trim().equalsIgnoreCase("false")){
                           processinfo();
                      }
                  }else{
  
                  // /////////////////////////////////////////////////// split the payload
                  // using delimiter. assign value at position[0] to tagType
                  String[] splitPayload = payload.split(",");
  
  
  
                  tagType = splitPayload[0];
                  //tagCompany = splitPayload[1];
  
  
                  c.moveToLast();
                  tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
  
  
                  if(splitPayload[2].length() == 0 || splitPayload[2] == null || splitPayload[2].trim().equalsIgnoreCase("0")){
  
                      tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
                  }else{
  
                  tagPerson = splitPayload[2];
  
                  }
  
                  tagUserName = splitPayload[3];
                  tagLatitude = "0.0000000";
                  tagLongitude = "0.0000000"; 
  
                  if(rotasOnly.trim().equalsIgnoreCase("false")){
                       processinfo();
  
                  }else{
                      Log.e(TAG, "cant process tag because of company option 160 rotas only");
                  }
  
                  }   
  
              }else{
  
  
  
              //DB not empty///////////////////
  
              DateTime nfcScanTimeMinustagTouchInterval = new DateTime();
              nfcScanTimeMinustagTouchInterval.minusMinutes(Integer.parseInt(tagTouchInterval));
  
  
  
  
  
  //          AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
  //          // For example to set the volume of played media to maximum.
  //          audioManager.setStreamVolume (AudioManager.STREAM_MUSIC, 
  //                        audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
  //          MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.meep2); 
  //          mediaPlayer.start();
              //mediaPlayer.release();
              //mediaPlayer = null; 
  
  
          Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
  
          tagId = bytesToHexString(tag.getId());
          Log.e(TAG, "tagId immediately after scanning nfc tag = " + tagId);
  
          if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  
              Log.e(TAG, "NFC Tag scanned");
  
              // get the messages from the intent
              Parcelable[] rawMsgs = intent
                      .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
              if (rawMsgs != null) {
                  msgs = new NdefMessage[rawMsgs.length];
                  for (int i = 0; i &lt; rawMsgs.length; i++) {
                      msgs[i] = (NdefMessage) rawMsgs[i];
                  }
              }
          } else {
              Log.e(TAG, "ndef not discovered!!!!!!");
          }
  
           Cursor c = nfcscannerapplication.loginValidate.queryAllFromCarer();
  
            if(c.getCount() == 0){
  
                Toast.makeText(this, "Please login before signing in",
                          Toast.LENGTH_LONG).show();
                Intent intent = new Intent(NfcscannerActivity.this, EntryActivity.class);
                startActivity(intent);
            }else{
                c.moveToLast();
                tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
            }
  
  
  
  
          // process the msgs array
          for (int i = 0; i &lt; msgs.length; i++) {
  
              NdefRecord[] records = msgs[i].getRecords();
              Log.e(TAG, "ndefrecord has a length of " + records.length);
  
              tr = parse(records[i]);
              payload = tr.getText();
  
              Log.e(TAG, "TextRecord.text = " + tr.getText());
              Log.e(TAG, "payload len = " + payload.length() + " so must be a blank formatted tag!");
  
  
          }
  
          if(payload.length() == 0){
  
              tagType = "1";
              tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
              tagPerson = tagId;//c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
              tagUserName = tagId;
              tagLatitude = "0.0000000";
              tagLongitude = "0.0000000"; 
  
              if(rotasOnly.trim().equalsIgnoreCase("false")){
                   processinfo();
              }
          }else{
  
  
          // /////////////////////////////////////////////////// split the payload
          // using delimiter. assign value at position[0] to tagType
          String[] splitPayload = payload.split(",");
  
  
  
          tagType = splitPayload[0];
          //tagCompany = splitPayload[1];
          tagCompany = c.getString(c.getColumnIndex(LoginValidate.C_COMP_ID));
  
          if(splitPayload[2].length() == 0 || splitPayload[2] == null || splitPayload[2].trim().equalsIgnoreCase("0")){
  
              tagPerson = c.getString(c.getColumnIndex(LoginValidate.C_CARER_ID));
  
          }else{
  
          tagPerson = splitPayload[2];
  
          }
  
          tagUserName = splitPayload[3];
          tagLatitude = "0.0000000";
          tagLongitude = "0.0000000"; 
  
          if(rotasOnly.trim().equalsIgnoreCase("false")){
               processinfo();
          }else{
              Log.e(TAG, "cant process tag because of company option 160 rotas only");
          }
  
  
          }   
  
              }//finished processing tag with a NONE empty DB
  
  
  
  
          }
  </code></pre>
  
  <p>[edit2]</p>
  
  <pre><code>if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    byte[] tagIdbyteArray = tag.getId();
                    Log.e(TAG, "unformatted tag found with a tagID of " + tagIdbyteArray);
                  }
  </code></pre>
  
  <p>[edit3]</p>
  
  <pre><code>&lt;activity
              android:name=".NfcscannerActivity"
              android:screenOrientation="portrait" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
                  &lt;data android:mimeType="text/plain" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.carefreegroup.QRCODE_ACTION" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
                  &lt;data android:mimeType="text/plain" /&gt;
              &lt;/intent-filter&gt;
  
              &lt;intent-filter&gt;
              &lt;action android:name="android.nfc.action.TECH_DISCOVERED"/&gt;
              &lt;/intent-filter&gt; 
  
              &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                         android:resource="@xml/nfc_tech_filter" /&gt;
  
          &lt;/activity&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
    &lt;tech-list&gt;
        &lt;tech&gt;android.nfc.tech.IsoDep&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcB&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NfcV&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
          &lt;tech&gt;android.nfc.tech.NdefFormatable&lt;/tech&gt;
      &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
      &lt;tech&gt;android.nfc.tech.MifareUltralight&lt;/tech&gt;
    &lt;/tech-list&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>.</p>
  
  <pre><code>if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
                    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                    byte[] tagIdbyteArray = tag.getId();
                    Log.e(TAG, "unformatted tag found with a tagID of " + tagIdbyteArray);
                  }
  </code></pre>
  
  <p>[edit 4]</p>
  
  <pre><code>01-21 09:27:35.180: D/NativeNfcTag(512): Connect Failed - status = 146
  01-21 09:27:35.180: E/NFC JNI(512): doDisconnect() - Target already disconnected
  </code></pre>
  
  <p>[edit5]</p>
  
  <pre><code>01-21 10:07:25.420: E/NfcService(512): No tag fallback activity found for Intent { act=android.nfc.action.TAG_DISCOVERED (has extras) }
  </code></pre>
  
  <p>[edit6]</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
    &lt;tech-list&gt;
  
      &lt;tech&gt;android.nfc.tech.MifareUltralight&lt;/tech&gt;
    &lt;/tech-list&gt;
  &lt;/resources&gt;
  </code></pre>