2017-05-26 110 views
3

我正在開發一個android應用程序來讀取和寫入不同的NFC標籤。我遇到了特定標籤,iCODE SLI X和iCODE SLI S的問題。在我在標籤上寫入信息後,我無法做任何其他操作,看起來NFC停止正常工作,因爲如果我重新啓動它,它會實際讀取標籤。如果我使用其他標籤類型(如MIFARE Classic 1K),則不會發生這種情況。 Android版本是6.0。另一方面,如果我在Android 6.1或7.0(完全相同的代碼)的其他設備上嘗試應用程序,iCODE SLI X和iCODE SLIS將可以正常工作,但不是MIFARE Classic 1K。NFC在iCODE標籤上寫入後停止工作,Android 6.0

除了嘗試不同的代碼示例,我也嘗試過在這些設備上的2個應用程序。在「NFC工具」上,您可以看到與我的應用程序完全相同的問題。恩智浦的「TagWriter」是唯一一款適用於所有類型標籤的應用程序。

下面是我用寫在標籤上的信息代碼:

@Override 
protected void onNewIntent(Intent intent) { 

    if (mNfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) { 
     Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
     if (tag != null) { 
      try { 
       Ndef ndef = Ndef.get(tag); 

       NdefRecord text1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
         youstring1.getBytes(Charset.forName("US-ASCII")), 
         null, 
         youstring1.getBytes()); 

       NdefRecord text2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
         youstring2.getBytes(Charset.forName("US-ASCII")), 
         null, 
         youstring2.getBytes()); 

       NdefRecord[] records = {text1, text2}; 

       NdefMessage message = new NdefMessage(records); 


       if (ndef != null) { 
        NdefMessage ndefMesg = ndef.getCachedNdefMessage(); 
        if (ndefMesg != null) { 
         ndef.connect(); 
         ndef.writeNdefMessage(message); 
         ndef.close(); 
        } 
       } else { 
        NdefFormatable ndefFormatable = NdefFormatable.get(tag); 
        if (ndefFormatable != null) { 
         // initialize tag with new NDEF message 
         try { 
          ndefFormatable.connect(); 
          ndefFormatable.format(message); 
          ndefFormatable.close(); 
         } finally { 
          try { 
           //ndefFormatable.close(); 
          } catch (Exception e) { 
          } 
         } 
        } 
       } 
      }catch (FormatException |IOException ue){} 
     } 
    } 
} 

我不明白我可能做錯了......

回答

1

讓我進一步瞭解我的應用程序出了什麼問題,所以我自己發佈了答案。這是事情:

當我嘗試寫入標籤上的信息時,我首先檢查標籤是否被格式化爲使用「Ndef」技術,如果沒有,我使用「NdefFormatable」格式化標籤。

奇怪的是,某些設備中的某個標籤支持「NdefFormatable」,而在某些設備中則不支持。 (不確定它是否與NFC本身或操作系統版本相關)。在我嘗試使用「NdefFormatable」後,這導致NFC行爲不當或者根本不工作。

我現在正在做的是,我已經建立了這個功能,使我可以在標籤上使用的技術。根據它,我使用「NdefFormatable」或「NfcV」(用於iCODE標籤)在標籤上讀取或寫入。

相關問題