2016-08-04 322 views
0

我寫了一個Android應用程序,它使用transceive()函數與NFC-V卡進行通信。我的問題是那行Android nfcv.transceive()拋出異常

byte[] response = nfcv.transceive(command) 

總是拋出一個標記丟失異常。

有人可以幫助我嗎?

String action = intent.getAction(); 

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
NfcV nfcv = NfcV.get(tag); 
if(nfcv != null) { 
    Toast.makeText(this, "nfcv detected", Toast.LENGTH_LONG).show(); 
} 

try { 
    nfcv.connect(); 
    Toast.makeText(this, "connected", Toast.LENGTH_LONG).show(); 
    byte[] command = new byte[]{      
      (byte) 0x00, // Flags 
      (byte) 0x20, // Command: Read single block 
      (byte) 0x00, // First block (offset) 
      (byte) 0x04 // Number of blocks}; 
    byte[] response = nfcv.transceive(command); 
    nfcv.close(); 
} catch(Exception e) { 
    Toast.makeText(this, "Error exception!", Toast.LENGTH_LONG).show(); 
} 

我得到以下異常:

android.nfc.TagLostException: Tag was lost. 
    at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:48) 
    at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151) 
    at android.nfc.tech.NfcV.transceive(NfcV.java:115) 
    at com.example.nxf07589.nfc.MainActivity.onCreate(MainActivity.java:148) 
    at android.app.Activity.performCreate(Activity.java:6374) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879) 
    at android.app.ActivityThread.access$900(ActivityThread.java:182) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6141) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
+2

您將丟棄異常消息和堆棧跟蹤,這對於這類問題可能非常有用。在catch塊的下方,請添加'android.util.Log.e(「NFC」,「Exception」,e);'讀取LogCat中的異常信息。然後將堆棧跟蹤添加到您的帖子。 –

回答

1

,您會收到TagLostException因爲你的命令是在一個錯誤的格式,因此,標籤不回答。

顧名思義,READ SINGLE BLOCK命令(命令碼0x20)讀取的是一個單塊。因此,此命令中沒有長度(「塊數」)字段。正確的命令是這樣的:

int blockAddress = 0; 
byte[] cmd = new byte[] { 
     (byte) 0x00, // FLAGS 
     (byte) 0x20, // READ_SINGLE_BLOCK 
     (byte)(blockAddress & 0x0ff) 
}; 
byte[] response = nfcv.transceive(cmd); 

注意,如果標籤不理解的命令(讀單個塊在ISO/IEC 15693的可選命令),你仍然可以得到TagLostException然後。

最後,一些Android平臺不能很好地(或根本不支持)NFC-V的未編譯命令。您可能因此想要使用該命令的地址形式:

byte[] tagUid = tag.getId(); // store tag UID for use in addressed commands 

int blockAddress = 0; 
byte[] cmd = new byte[] { 
     (byte)0x20, // FLAGS 
     (byte)0x20, // READ_SINGLE_BLOCK 
     0, 0, 0, 0, 0, 0, 0, 0, 
     (byte)(blockAddress & 0x0ff) 
}; 
System.arraycopy(tagUid, 0, cmd, 2, 8); // paste tag UID into command 
byte[] response = nfcv.transceive(cmd); 
+1

thx尋求幫助。它工作得很好! –