0

NFC タグからの文字列と xml ファイルからの文字列の 2 つの文字列を比較する際に問題が発生しています。タグを記述するために使用するコードは次のようになります。

   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 = new String(tag_id.getBytes("UTF-8"), "ascii") 無駄に
隠し文字がなく基本的な文字列比較が機能するようにタグを記述する方法、または同じ番号の文字列と同等になるようにタグから文字列を変更する方法を誰かが説明してください。ありがとう。

psこれを書いた後に使い始めた Android Developer NFC Basics の write メソッドは次のとおりです。これにより、上記と同じ問題が発生します。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;
}
4

2 に答える 2

0

このTextRecordを試して、問題が解決するかどうかを確認してください。おそらく、エンコード/デコード テキスト レコードが正しくありません。

于 2013-03-25T17:39:39.793 に答える
0

各タグに long のみを格納するため、タグ メッセージから他のすべての文字を除外するメソッドを作成しました。これで、タグ 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;
}
于 2013-03-26T06:16:14.220 に答える