簡體   English   中英

Android NFC writeNdefMessage拋出IOException標簽不是ndef

[英]Android NFC writeNdefMessage throws IOException Tag is not ndef

我正在開發一個NFC環境,包括一個標簽(AS3953芯片+微控制器)和一個智能手機(三星Galaxy Fame runnung Android 4.1.2)。

在閱讀NDEF消息時,我仍然堅持將消息寫入標簽。 我從http://tapintonfc.blogspot.de/2012/07/the-above-footage-from-our-nfc-workshop.html復制了大部分代碼,並通過搜索標簽技術列表將其修改為接受ISO14443A標簽類型4對於支持技術()中的IsoDep,NfcA和Ndef。 由於列出了所有這些,app繼續writeTag():

public WriteResponse writeTag(NdefMessage message, Tag tag) {
try {
    Ndef ndef = Ndef.get(tag);
    if (ndef != null) {
        Log.d(TAG, "writeTag: tag type: "+ndef.getType());
        ndef.connect();
        Log.d(TAG, "writeTag: connected!");
        if (!ndef.isWritable()) {
            return new WriteResponse(0, "Tag is read-only");
        }
        if (ndef.getMaxSize() < message.toByteArray().length) {
            return new WriteResponse(0, "size error");
        }
        Log.d(TAG, "writeTag: write ndef...");
        ndef.writeNdefMessage(message);
        Log.d(TAG, "writeTag: wrote ndef!");
        if (writeProtect)
            ndef.makeReadOnly();
        return new WriteResponse(1, "Wrote message to pre-formatted tag.");
    } else {
        Log.d(TAG, "writeTag: ndef==null!");
        return new WriteResponse(0, "writeTag: ndef==null!");
    }
} catch (Exception e) {
    Log.d(TAG, "writeTag: exception: " + e.toString());
    return new WriteResponse(0, "Failed to write tag");
}
}

LogCat顯示:

11:08:46.400: onNewIntent
11:08:46.400: supportedTechs: techlist: android.nfc.tech.IsoDep,android.nfc.tech.NfcA,android.nfc.tech.Ndef,
11:08:46.400: supportedTechs: tech is supported!
11:08:46.400: writeTag: tag type: org.nfcforum.ndef.type4
11:08:46.410: writeTag: connected!
11:08:46.410: writeTag: write ndef...
11:08:46.490: writeTag: exception: java.io.IOException: Tag is not ndef

正如您所看到的,拋出IOException,表示Tag不是ndef,與技術列表相矛盾。 進一步研究android代碼,writeNdefMessage()嘗試從標記中獲取TagService和ServiceHandle以匹配它們。 這會失敗,因此拋出異常(到目前為止沒有寫入消息):

public void writeNdefMessage(NdefMessage msg) throws IOException, FormatException {
...
INfcTag tagService = mTag.getTagService();
...
int serviceHandle = mTag.getServiceHandle();
if (tagService.isNdef(serviceHandle)) {
    ...
}
else {
    throw new IOException("Tag is not ndef");
}
...
}

是否有解決方法或者根本不可能使用我的標簽? 因為我也在編寫標記,錯誤可能在另一邊,但它似乎是一個Java問題。


編輯1:

我以前沒有連接任何技術,因此不應該打開任何連接。 如果我在ndef.connect()之前打開連接,則會出現IllegalStateException:首先關閉其他技術!

我將AS3953配置為ISO14443A Level-4,因此只有Tag Type 4 Block被轉發到微控制器。 只處理I-Block,但即使有其他命令,μC也必須通過SPI端口讀取它,而邏輯分析並非如此。 正如我所說,閱讀ndef文件工作,我測試了4KB文件。 查看邏輯分析,執行以下步驟(所有返回正9000代碼):

(c=command, r=response) (corrected due to renaming mistake)
select by name:
c: 02 00 a4 04 00 07 d2 76 00 00 85 01 01 00
r: 02 90 00
select by id - select cc file
c: 03 00 a4 00 0c 02 e1 03
r: 03 90 00
read 0x0f bytes of cc file
c: 02 00 b0 00 00 0f
r: 02 00 0f 20 00 3b 00 34 04 06 e1 04 0f ff 00 00 90 00
select by id - select ndef file
c: 03 00 a4 00 0c 02 e1 04
r: 03 90 00
read 0x02 bytes (first 2 bytes are apdu-size)
c: 02 00 b0 00 00 02
r: 02 0f d3 90 00
read 0x3b bytes (frame size) of first part of ndef file (external type, jpeg image as payload)
c: 03 00 b0 00 02 3b
r: 03 c4 0c 00 00 0f c1 64 65 2e 74 65 73 74 61 70 70 3a 61 01 ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 60 00 60 00 00 ff db 00 43 00 49 32 36 40 36 2d 49 40 3b 40 52 4d 49 56 6d 90 00
[ndef file]
read 0x26 bytes of last part of ndef file
c: 03 00 b0 0f ae 27
r: 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF D9 00 00 90 00

使用相同的應用程序進行編寫我啟動過濾NfcAdapter.ACTION_TAG_DISCOVERED意圖的活動。 就像在鏈接的例子中一樣,手機接觸調用onResume()的標簽做mNfcAdapter.enableForegroundDispatch(...);

當我記錄SPI通信時,完成與上述相同的讀取過程。 由於LogCat顯示了一個工作意圖調度程序,我猜應用程序停止在IOException,關閉連接並立即讀取,因為android再次找到了標記。


編輯2:

可能有一個提示,因為第一個中斷之一發出一個由AS3953本身處理的取消選擇命令:

(count * interrupt)
3 * power up 
1 * Wake-up IRQ at entry in ACTIVE(*) state 
1 * Deselect command
1 * Wake-up IRQ at entry in ACTIVE(*) state 
1 * IRQ due to start of receive

它現在有效。 我發現檢查和設置配置字的啟動例程是以過高的波特率傳輸的,用於AS3953。 這適用於閱讀,但必須混淆寫入標簽的東西。

我無法確定這是否是它無效的唯一原因。 由於水位中斷的處理太遲,因此32Byte FIFO的讀取速度也很慢 - 我只是期望問題出在android端,因為我在邏輯分析中找不到寫命令而異常沒有足夠描述原因。

非常感謝Michael Roland進行進一步的調試,我仍然沒有解釋為什么首先讀取標簽,如果我想寫它但是很好 - 應該有一個解決方法,讀取空的ndef消息,這樣寫入過程就完成了很快。

目前我必須處理固件,無法預測任何進一步的問題,但實際問題(ndef.writeNdefMessage())成功返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM