2017-11-14 6 views
0

私はどのタイプのコピーNFCタグもエミュレートできるはずのアプリを開発中です。ゼロブロックとページマークからデータを読んだ後、「NFCタイプA」と入力し、メソッドを使用して「MIFAREウルトラライト」(まだ私の所持しているラベルタイプ):NFCタグタイプAからデータを読み取る際に、「Transceive failed」例外を回避するにはどうすればよいですか?

nfcA.transceive() 

mifareUltralight.readPages() 

残りのデータは読み取れませんが、 "Transceive failed"例外がスローされます。

ここに、適切なメソッドのコードを示します。 NFCAため:MifareUltralighについて

private static ArrayList<IdNameValue> readeNfcADataset(final Tag tag, final Context cxt) { 

    String excMsg = ""; 
    ArrayList<IdNameValue> dataSet = new ArrayList<>(); 
    NfcA nfcA = NfcA.get(tag); 

    if (nfcA != null) { 

     dataSet.add(new IdNameValue(cxt.getString(R.string.atqa) + " ", 
       "0x" + byteArrayToHexString(nfcA.getAtqa()))); 
     dataSet.add(new IdNameValue(cxt.getString(R.string.sak) + " ", 
       "0x" + shortToHexString(nfcA.getSak()))); 

     byte blockNumber = 0x00; 
     int bytesNumber = 1, pagesNumber = 16, counter = 0; 
     byte[][] bytes = new byte[pagesNumber][bytesNumber]; 

     excMsg = "_________________________________________________\n" + 
       cxt.getString(R.string.picc_compliant_with_iso_iec_14443_3A_fnca) + ":"; 
     Log.i(TAG, excMsg); 

     for (int i = 0; i < pagesNumber; i++) { 

      excMsg = ""; 
      while (!nfcA.isConnected()) { 
       try { 
        nfcA.connect(); 
        excMsg = cxt.getString(R.string.connecting_to_tag_has_been_created); 
        Log.i(TAG, 109 + ": " + excMsg); 

        bytes[i] = nfcA.transceive(new byte[]{ 
          (byte) 0x30, // READ 
          (byte) (blockNumber & 0x0FF) 
        }); 
        excMsg = cxt.getString(R.string.made_to_read_data_from_a_block) 
          + Integer.toHexString(blockNumber); 
        Log.i(TAG, 117 + ": " + excMsg); 
        if (nfcA.isConnected()) nfcA.close(); 
        excMsg += cxt.getString(R.string.tag_has_been_closed); 
        break; 
       } catch (IOException e) { 
        counter++; 
        excMsg = e.getMessage() + ":> " + (excMsg.equals("") ? 
          cxt.getString(R.string.the_connection_tag_is_not_created) : 
          (excMsg.contains(cxt.getString(R.string.connecting_to_tag_has_been_created)) || 
            excMsg.contains(cxt.getString(R.string.made_to_read_data_from_a_page))) ? 
            "Failed to read data from a tag block: " 
              + Integer.toHexString(blockNumber) : 
            cxt.getString(R.string.error_closing_tag)); 
        Log.e(TAG, 130 + ": " + excMsg); 
       } 

       if (nfcA.isConnected()) try { 
        nfcA.close(); 
       } catch (IOException e1) { 
        excMsg += (":> " + e1.getMessage()); 
        Log.e(TAG, 137 + ": " + excMsg); 
       } 

       if (counter > 9) { 
        excMsg = cxt.getString(R.string.the_number_of_connection_attempts_exceeded_the_number_of_ten); 

        Log.e(TAG, 143 + ": " + excMsg); 
        break; 
       } 
      } 
      counter = 0; 
      blockNumber += (byte) bytes[i].length; 
     } 

     if (excMsg.equals(cxt.getString(R.string.the_number_of_connection_attempts_has_exceeded_one_hundred))) { 
      dataSet.add(new IdNameValue("Error", " reading the data tag")); 
      return dataSet; 
     } 

     dataSet.add(new IdNameValue("Page", " 0 1 2 3")); 

     String name = ""; 
     String value = ""; 
     for (int i = 0; i < pagesNumber; i++) { 
      if (!value.equals("")) dataSet.add(new IdNameValue(name, value)); 
      name = (i > 9 ? " " : " ") + String.valueOf(i) + " "; 
      value = ""; 
      for (int j = 0; j < bytes[i].length; j++) { 
       value += (" " + byteToHexString(bytes[i][j])); 
      } 
     } 
     dataSet.add(new IdNameValue(name, value)); 
     return dataSet; 
    } 
    return dataSet; 
} 

private static ArrayList<IdNameValue> readeMifareUltralightDataset(final Tag tag, final Context cxt) { 

    MifareUltralight mifareUltralight = MifareUltralight.get(tag); 
    String excMsg = ""; 
    int bytesNumber = 1, pagesNumber = 4, counter = 0, startBlock; 
    byte[][] bytes = new byte[pagesNumber][bytesNumber]; 
    startBlock = 0x00; 

    excMsg = "_________________________________________________\n" + 
      cxt.getString(R.string.mifare_ultralight_or_ultralight_c) + ":"; 
    Log.i(TAG, excMsg); 

    for (int i = 0; i < pagesNumber; i++) { 

     while (!mifareUltralight.isConnected()) { 
      excMsg = ""; 
      try { 
       mifareUltralight.connect(); 
       excMsg = cxt.getString(R.string.connecting_to_tag_has_been_created); 
       Log.i(TAG, 201 + ": " + excMsg); 

       bytes[i] = mifareUltralight.readPages(startBlock); 
       excMsg = cxt.getString(R.string.done_reading_pages_twice_blocks) 
         + Integer.toHexString(startBlock) + " - " 
         + Integer.toHexString((startBlock + bytes[i].length) - 1); 
       Log.i(TAG, 207 + ": " + excMsg); 
       if (mifareUltralight.isConnected()) mifareUltralight.close(); 
       excMsg = cxt.getString(R.string.tag_has_been_closed); 
       Log.i(TAG, 210 + ": " + excMsg); 
       break; 
      } catch (IOException e) { 
       counter++; 
       excMsg = e.getMessage() + ":> " + (excMsg.equals("") ? 
         cxt.getString(R.string.the_connection_tag_is_not_created) : 
         (excMsg.contains(cxt.getString(R.string.connecting_to_tag_has_been_created)) || 
           excMsg.contains(cxt.getString(R.string.done_reading_pages_twice_blocks)) ? 
           "Failed to read page from tag blocks: " 
             + Integer.toHexString(startBlock) + " - " 
             + Integer.toHexString(startBlock + bytes[i].length - 1) : 
           cxt.getString(R.string.error_closing_tag))); 
       Log.e(TAG, 222 + ": " + excMsg); 
      } 

      if (mifareUltralight.isConnected()) try { 
       mifareUltralight.close(); 
       break; 
      } catch (IOException e) { 
       excMsg = (":> " + e.getMessage()); 
       Log.e(TAG, 230 + ": " + excMsg); 
      } 

      if (counter > 9) { 
       excMsg = cxt.getString(R.string.the_number_of_connection_attempts_exceeded_the_number_of_ten); 
       Log.e(TAG, 236 + ": " + excMsg); 
       break; 
      } 
     } 
     counter = 0; 
     startBlock += bytes[i].length; 
    } 

    ArrayList<IdNameValue> dataSet = new ArrayList<>(); 
    if (excMsg.equals(cxt.getString(R.string.the_number_of_connection_attempts_has_exceeded_one_hundred))) { 
     dataSet.add(new IdNameValue("Error", " reading the data tag")); 
     return dataSet; 
    } 

    dataSet.add(new IdNameValue("Page", " 0 1 2 3")); 

    int n = -1; 
    String name = ""; 
    String value = ""; 
    for (int i = 0; i < pagesNumber; i++) { 
     for (int j = 0; j < bytes[i].length; j++) { 
      if (j % 4 == 0) { 
       n++; 
       if (!value.equals("")) dataSet.add(new IdNameValue(name, value)); 
       name = (n > 9 ? " " : " ") + String.valueOf(n) + " "; 
       value = ""; 
      } 
      value += (" " + byteToHexString(bytes[i][j])); 
     } 
    } 
    dataSet.add(new IdNameValue(name, value)); 
    return dataSet; 
} 

は、それが得られたログフラグメント:

...cardreader I/TagController: _________________________________________________ 
           PICC compliant with ISO/IEC 14443-3A (NFCA): 
...cardreader I/TagController: 109: The connecting to tag has been created 
...cardreader I/TagController: 117: Made to read data from a block: 0 
...cardreader I/TagController: 109: The connecting to tag has been created 
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 10 
...cardreader I/TagController: 109: The connecting to tag has been created 
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 10 
........................................................... 
...cardreader I/TagController: 109: The connecting to tag has been created 
...cardreader E/TagController: 130: Transceive failed:> Failed to read data from a tag block: 1e 
...cardreader E/TagController: 143: The number of connection attempts exceeded the number of ten. 
...cardreader I/TagController: _________________________________________________ 
           MIFARE Ultralight or Ultralight C: 
...cardreader I/TagController: 201: The connecting to tag has been created 
...cardreader I/TagController: 207: Done reading pages twice blocks:0 - f 
...cardreader I/TagController: 210: Tag has been closed 
...cardreader I/TagController: 201: The connecting to tag has been created 
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 10 - 10 
...cardreader I/TagController: 201: The connecting to tag has been created 
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 11 - 11 
...cardreader I/TagController: 201: The connecting to tag has been created 
...cardreader E/TagController: 222: Transceive failed:> Failed to read page from tag blocks: 12 - 12 

残りのブロック内のデータが利用可能であるという事実は、出力ポート監視プログラムを示します"Arduino 1.8.5":

Card UID: 04 3A 31 02 54 2E 82 
Card SAK: 00 
PICC type: MIFARE Ultralight or Ultralight C 
Page 0 1 2 3 
    0 04 3A 31 87 
    1 02 54 2E 82 
    2 FA 48 F0 00 
    3 FF FF FF FC 
    4 45 D9 B0 49 
    5 9E 6A 2D 00 
    6 1E 82 00 00 
    7 1E 82 00 00 
    8 1E 7E 05 00 
    9 40 00 08 09 
10 B7 DD DB FF 
11 1E 7E 98 48 
12 1E 7E 05 00 
13 40 00 08 09 
14 B7 DD DB FF 
15 1E 7E 98 48 
以下のスクリーンショットで

、アプリケーションは、ラベルタイプ、技術およびUIDのデータを読み取ることができることを実証している: 以下The output data read from the label on its type, technology, and UID

タグから読み取った出力、データの最初の16 bytsを示しています。 MIFARE超軽量のため The output read from the data labels (for NFC type A)

:NFCタイプAについて The output read from the data labels (for MIFARE Ultralight)分かるように、システムは、最初の16 bytsを読み出します。何が間違っていますか?何か考えてください。

答えて

0

ドキュメントを長く見て、すなわち"MF0ICU1 MIFARE Ultralight contactless single-ticket IC"/Rev. 3.9 — 23 July 2014 #028639"MF0ICU2 MIFARE Ultralight C"/Rev. 3.2 — 19 May 2009 #171432をリードし、最終的に問題を理解しました。コマンド入力READEに供給されるブロックアドレス内のすべての塩。

blockNumber = (byte) (i * 4); 

とラベルタイプのMifareウルトラライト用:タイプタグNFCAの番号ブロックの計算は次のようになります

startBlock = i * 4; 

その後、すべてがオフになります。

関連する問題