2013-01-11 57 views
1

我有一個應用程序掃描NFC標籤以從有效負載中獲取信息,然後通過Web服務將該信息發送到服務器。它工作正常。標籤是Mifare。在我加入公司之前,系統的移動部分是用J2ME爲諾基亞c700編寫的。我已經在Android中重寫了這個系統。 Android應用可以獲取標籤ID並讀取有效載荷。如果沒有有效載荷,那麼它只讀取標籤ID,這很好。如何掃描未格式化的NFC標籤

問題是,當我掃描未格式化的標籤,然後我無法獲得tagID。 nokias上的舊系統可以從未格式化的標籤讀取tagID。我如何在Android中執行此操作?爲什麼標籤必須格式化?

在此先感謝。

[編輯]

if(intentAction.equalsIgnoreCase(NFC_ACTION)){ 

      nfcScanTimeFromDB = null; 

      cursor = nfcscannerapplication.loginValidate.getLastTagFromTransations(); 

      if (cursor.getCount() > 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 < 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 < 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 < 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 < 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 




     } 

[EDIT2]

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); 
       } 

[EDIT3]

<activity 
      android:name=".NfcscannerActivity" 
      android:screenOrientation="portrait" > 
      <intent-filter> 
       <action android:name="android.nfc.action.NDEF_DISCOVERED" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="text/plain" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="com.carefreegroup.QRCODE_ACTION" /> 

       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="text/plain" /> 
      </intent-filter> 

      <intent-filter> 
      <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
      </intent-filter> 

      <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
         android:resource="@xml/nfc_tech_filter" /> 

     </activity> 

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <tech-list> 
     <tech>android.nfc.tech.IsoDep</tech> 
     <tech>android.nfc.tech.NfcA</tech> 
     <tech>android.nfc.tech.NfcB</tech> 
     <tech>android.nfc.tech.NfcF</tech> 
     <tech>android.nfc.tech.NfcV</tech> 
     <tech>android.nfc.tech.Ndef</tech> 
     <tech>android.nfc.tech.NdefFormatable</tech> 
    <tech>android.nfc.tech.MifareClassic</tech> 
    <tech>android.nfc.tech.MifareUltralight</tech> 
    </tech-list> 
</resources> 

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); 
       } 

[編輯4]

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 

[edit5]

01-21 10:07:25.420: E/NfcService(512): No tag fallback activity found for Intent { act=android.nfc.action.TAG_DISCOVERED (has extras) } 

[edit6]

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <tech-list> 

    <tech>android.nfc.tech.MifareUltralight</tech> 
    </tech-list> 
</resources> 
+0

你是什麼意思的「格式化」 ? –

+0

它應該可以正常工作,並可以與其他應用程序一起正常工作。你可以包含代碼嗎? – kichik

+0

@NFCguy嗨,如果我掃描直接離開卷標的標籤,那麼應用程序不會捕獲標籤ID。對於我的應用程序獲取標籤ID,我必須首先使用單獨的標籤寫入器/閱讀器寫入標籤的有效負載。如果需要,我可以將有效負載留空,但標記寫入程序必須將標記的格式更改爲NDEF。我的應用捕獲了NDEF中的標籤。所以我只能假定標籤不是以NDEF格式來的,但很有趣的是,j2me中的舊諾基亞c700如何做到這一點 – turtleboy

回答

1

如果MIFARE經典標籤不格式化以包含NDEF,你可以收到他們的意圖(並獲得ID )如下(從documentation):

添加到您的清單爲需要接收意圖的活動:

<activity> 
    ... 
    <intent-filter> 
    <action android:name="android.nfc.action.TECH_DISCOVERED"/> 
    <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 

    <meta-data android:name="android.nfc.action.TECH_DISCOVERED" 
    android:resource="@xml/nfc_tech_filter" /> 
    ... 
</activity> 

創建包含一個新的文件<project-root>/res/xml/nfc_tech_filter.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 
    <tech-list> 
    <tech>android.nfc.tech.MifareClassic</tech> 
    </tech-list> 
    <tech-list> 
    <tech>android.nfc.tech.MifareUltralight</tech> 
    </tech-list> 
</resources> 

在你活動,在一些適當的地方,加上:

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) { 
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
    byte[] tagId = tag.getId(); 
} 
+0

嘿非常感謝。我沒有閱讀文檔,但過濾android.nfc.action。NDEF_DISCOVERED和tag_discovered,因爲當我掃描一個未格式化的標籤eclipse登出沒有後備標籤等,再次感謝我會試試這個。 – turtleboy

+0

我剛剛編輯過這個帖子[edit2]。我已經將該代碼添加到了我的活動中,並完成了對manifest和res/xml文件夾的其他修改,但它沒有記錄任何內容/捕獲未格式化的標記。有任何想法嗎? – turtleboy

+0

我的答案格式有誤:TECH_DISCOVERED的意圖過濾器由於某種原因沒有顯示。你已經自己添加了嗎? –