android如何通过读取NFC标签获得NDEF消息有效载荷的最大大小

余芳君*_*余芳君 2 android nfc ndef

我正在开发一个 android 应用程序来写入 nfc 标签的特定内存位置。我需要通过读取 NFC 标签获得最大 NDEF 消息有效负载大小,以便我可以在该范围内定义内存位置。

我知道它能够通过下面的代码获得整个 ndef 内存大小:

Ndef ndef = Ndef.get(tag); int size = ndef.getMaxSize();

有没有办法获得最大有效载荷大小?

任何帮助将不胜感激!

Sam*_*Sam 5

如果我理解正确,您想知道在 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)
Run Code Online (Sandbox Code Playgroud)