2013-03-25 94 views
0

我在比較兩個字符串(一個來自NFC標籤,另一個來自xml文件)時遇到問題。 我用寫標籤的代碼如下所示:Android NFC標籤字符串比較

private NdefMessage getTagAsNdef(Card new_card) 
    { 
     boolean addAAR = false; 
     String unique_id = new_card.getCardId();  
     byte[] bytes = unique_id.getBytes(Charset.forName("UTF-8")); 
     byte[] payload = new byte[bytes.length + 1]; 
     NdefRecord rtdUriRecord = new NdefRecord(
     NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload); 
     if(addAAR) 
     { 
      NdefRecord new_record = null; 
      try 
      { 
       new_record = createRecord(new_card); 
      } catch (UnsupportedEncodingException e) 
      { 
       e.printStackTrace(); 
      } 
      return new NdefMessage(new NdefRecord[] {rtdUriRecord, new_record}); 
     } else 
     { 
      return new NdefMessage(new NdefRecord[] 
     { 
       rtdUriRecord}); 
     } 
    } 

我試圖刪除通常在以下建議的有效載荷設定發現這裏的一切語言和編碼信息: Comparing Data Read from NFC Tag 我以後在這裏做的比較讀取標籤但總是失敗,但在日誌中打印的字符串值是相同的:

looking at this_card_id -6465415649291849135 
     compare to tag_id -6465415649291849135 

我得到的字符串的長度,藉以說明問題:

looking at this_card_id 20 
     compare to tag_id 21 

所以標籤ID有一個隱藏的字符,我一直無法擺脫。 下面是對比代碼:

private void associateWithCardsFile(String tag_id) 
{ 
    String method = "associateWithCardsFile"; 
    Log.i(DEBUG_TAG, method+" tag_id "+tag_id); 
    tag_id.trim(); 
    boolean found = false; 
    Enumeration e = cards.keys(); 
    Log.i(DEBUG_TAG, method+" cards "+cards.size()); 
    while (e.hasMoreElements()) 
    { 
     String this_card_id = (String)e.nextElement(); 
     this_card_id.trim(); 
     Log.i(DEBUG_TAG, method+" looking at this_card_id "+this_card_id.length()); 
     Log.i(DEBUG_TAG, method+"  compare to tag_id "+tag_id.length()); 
     String card_id_str = UtilityTo.encodeThisString(this_card_id, "UTF-8"); 
     String tag_id_str = UtilityTo.encodeThisString(tag_id, "UTF-8"); 
     if (card_id_str.equals(tag_id_str)) 
     { 
      Card matching_card = cards.get(this_card_id); 
      String word = UtilityTo.getWord(matching_card); 
      Toast.makeText(this, word, Toast.LENGTH_LONG).show(); 
      Log.i(DEBUG_TAG, method+" 1 match"+matching_card.getText()+" "+matching_card.getDefinition()+" "+matching_card.getWordType()); 
      turn_cards.add(matching_card); 
      found = true; 
      showCards(); 
      break; 
     } 
    } 
    if (!found) 
    { 
     Toast.makeText(this, "Not a game card!", Toast.LENGTH_LONG).show(); 
     Log.i(DEBUG_TAG, method+" card not found"); 
    } 
} 

在UtilityTo類,我有這樣的方法,對具有外語的工作效果很好。

public static String encodeThisString(String original_value, String encoding) 
{ 
    try 
    { 
     byte[] utf8Bytes = original_value.getBytes(encoding); 
     String new_value = new String(utf8Bytes, encoding); 
     return new_value; 
    } catch (UnsupportedEncodingException e) 
    { 
     e.printStackTrace(); 
     return null; 
    } catch (java.lang.NullPointerException n) 
    { 
     n.printStackTrace(); 
     return null; 
    } 
} 

我也嘗試從StackOverflow的這樣一些建議:無濟於事tag_id_str =新的String(tag_id.getBytes( 「UTF-8」), 「ASCII」)。
有人可以請解釋如何寫標籤,以便沒有隱藏的字符和基本的字符串比較將工作,或如何修改字符串從標籤,因此它將等同於具有相同數字的字符串。 謝謝。

p.s. 下面是我寫這篇文章後開始使用的Android開發者NFC基礎知識的寫入方法。它會導致與上述相同的問題:tag_id比xml文件中的card_id長1個字符。

public NdefRecord createRecord(String payload) 
{ 
    String language = "en"; 
    boolean encodeInUtf8 = true; 
    byte[] langBytes = language.getBytes(Charset.forName("US-ASCII")); 
    Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16"); 
    byte[] textBytes = payload.getBytes(utfEncoding); 
    int utfBit = encodeInUtf8 ? 0 : (1 << 7); 
    char status = (char) (utfBit + langBytes.length); 
    byte[] data = new byte[1 + langBytes.length + textBytes.length]; 
    data[0] = (byte) status; 
    System.arraycopy(langBytes, 0, data, 1, langBytes.length); 
    System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); 
    NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
    NdefRecord.RTD_TEXT, new byte[0], data); 
    return record; 
} 

回答

0

試試這個TextRecord看看是否能解決您的問題。可能您的編碼/解碼文本記錄不正確。

+0

感謝您的幫助托馬斯。由於NFC的需求非常簡單,而且我幾乎完成了讀取和寫入方法,因此我最終能夠解決問題。 NDF工具看起來是一個不錯的選擇。如果我再次從頭開始,我可能會使用它來節省一些時間。 – curchod 2013-03-26 06:19:52

0

由於我只在每個標籤上存儲了很長時間,所以我提出了一種從標籤消息中排除所有其他字符的方法。 現在我可以將標記ID字符串與xml文件中的id字符串進行比較。

private String removeHiddenCharacters(String message) 
{ 
    String dash = "-"; 
    char d = dash.charAt(0); 
    StringBuffer sb = new StringBuffer(); 
    for (int i = 0; i < message.length(); i++) 
    { 
    char c = message.charAt(i); 
    if (Character.isDigit(c)) 
    { 
     sb.append(c); 
    } else if (c == d) 
    { 
     sb.append(d); 
    } 
    } 
    String result = new String(sb); 
    Log.i(DEBUG_TAG, " result "+result); 
    return result; 
} 
+0

您是否考慮過外部類型記錄?它的有效載荷是一個(任何)字節數組,所以問題的來源會更少。 – ThomasRS 2013-03-26 12:35:57