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