簡體   English   中英

android如何通過讀取NFC標簽獲取NDEF消息有效負載的最大大小

[英]android how to get the maximum size of NDEF message payload from reading a NFC tag

我正在開發一個Android應用程序,以寫入nfc標簽的特定內存位置。 而且我需要通過讀取NFC標簽來獲得NDEF消息有效負載的最大大小,以便可以在該范圍內定義內存位置。

我知道它可以通過以下代碼獲取整個ndef內存大小:

Ndef ndef = Ndef.get(標簽); int size = ndef.getMaxSize();

有什么方法可以獲取最大有效載荷大小?

任何幫助將不勝感激!

如果我對您的理解正確,則想知道在NDEF消息中單個NDEF記錄中可以發送的最大數據量。

答案取決於您使用的NDEF記錄格式。 我正在使用外部記錄類型,該類型的結尾處的負載僅為一堆字節。 據我所知,沒有簡單的方法來獲取它,這取決於您的單個域/類型字段的長度,因此我實際上創建了一個“空”記錄(實際上,其中有一些數據是為了獲取較長的字段格式),然后從最大值中減去其長度。

NDEF消息的最大總長度由兼容性容器指定,可在首次聯系時從標簽或設備中讀取,並且各標簽之間有所不同。 Android讓我們使用ndefTag.getMaxSize(); (其中ndefTag是您從意圖中獲得的標簽)

這是我正在使用的方法(我允許使用不同的發送和接收長度):

//here we make some sample transmissions that we convert to work out the max length of data we can send
int test_payload_len = 300; //need to make this >255 to ensure the longer length field format is used
NdefMessage testDeviceNdef = new NdefMessage(NdefRecord.createExternal(CommandConsts.deviceDomain, CommandConsts.deviceType, test_data)); //the domain and type strings are defined elsewhere
NdefMessage testTerminalNdef = new NdefMessage(NdefRecord.createExternal(CommandConsts.terminalDomain, CommandConsts.terminalType, test_data));

maxNdefDeviceSendLength = ndefTag.getMaxSize() - (testDeviceNdef.getByteArrayLength() - test_payload_len); 
maxNdefTerminalSendLength = ndefTag.getMaxSize() - (testTerminalNdef.getByteArrayLength() - test_payload_len)

規范允許的最大有效負載大小為2 ^ 32-1

http://developer.android.com/reference/android/nfc/NdefRecord.html

您可以使用getPayload獲得可變長度的有效負載及其大小

暫無
暫無

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

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