Android 在nfc中读写数据的应用
Android application to read and write data in an nfc
我是 NFC 新手,我正在开发一个 android 应用程序来在 nfc 中读取和写入数据,但我遇到了一些问题。
这是我正在使用的代码(写入):
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Toast.makeText(this, R.string.message_tag_detected, Toast.LENGTH_SHORT).show();
}
Tag currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = currentTag.getId();
String myData = "ABCDEFGHIJKL";
for (String tech : currentTag.getTechList()) {
if (tech.equals(NfcV.class.getName())) {
NfcV tag5 = NfcV.get(currentTag);
try {
tag5.connect();
int offset = 0;
int blocks = 8;
byte[] data = myData.getBytes();
byte[] cmd = new byte[] {
(byte)0x20,
(byte)0x21,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
};
System.arraycopy(id, 0, cmd, 2, 8);
for (int i = 0; i < blocks; ++i) {
cmd[10] = (byte)((offset + i) & 0x0ff);
System.arraycopy(data, i, cmd, 11, 4);
response = tag5.transceive(cmd);
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
}
}
当我在应用程序 TagInfo 中读取标签时,输出是:
[00]。 41 42 43 44 [ABCD]
[01]。 42 43 44 45 [BCDE]
[02]。 43 44 45 46 [CDEF]
[03]。 44 45 46 47 [DEFG]
[04]。 45 46 47 48 [EFGH]
[05]。 46 47 48 49 [FGHI]
[06]。 47 48 49 4A [GHIJ]
[07]。 48 49 4A 4B [HIJK]
[08]。 00 00 00 00 [。 . . .]
。 . .
这个输出正确吗?
如果'NOT',我哪里错了?
对我来说这看起来不对,但不是 NfcV 专家只使用 NDEF nfc 卡。
[00] . 41 42 43 44 [ABCD]
[01] . 45 46 47 48 [EFGH]
[02] . 49 4A 4B 4C [IJKL]
作为你真正想做的事情
我觉得问题出在System.arraycopy(data, i, cmd, 11, 4);
您正在从源数据数组中复制 4 个字节的数据,但仅将起始位置增加 1 个字节的数据,因此下一个块从后面的字母开始。
我认为System.arraycopy(data, i*4, cmd, 11, 4);
会产生你想要的结果。
因为这会将源数据中 arraycopy 的开始增加您已经存储的字节数。
因为你是12个字节的数据,每个块存储4个字节你只需要使用3个块,所以通过设置int blocks = 3;
只循环3次否则你将运行没有数据复制输入 cmd 发送到从 arraycopy
生成 IndexOutOfBoundsException
的卡
如果您没有 4 字节的倍数数据,则必须用零填充数据以使其成为 4 字节的倍数,或者将 IndexOutOfBoundsException
从 arraycopy
正确处理复制剩余的字节。
我是 NFC 新手,我正在开发一个 android 应用程序来在 nfc 中读取和写入数据,但我遇到了一些问题。
这是我正在使用的代码(写入):
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Toast.makeText(this, R.string.message_tag_detected, Toast.LENGTH_SHORT).show();
}
Tag currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] id = currentTag.getId();
String myData = "ABCDEFGHIJKL";
for (String tech : currentTag.getTechList()) {
if (tech.equals(NfcV.class.getName())) {
NfcV tag5 = NfcV.get(currentTag);
try {
tag5.connect();
int offset = 0;
int blocks = 8;
byte[] data = myData.getBytes();
byte[] cmd = new byte[] {
(byte)0x20,
(byte)0x21,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00
};
System.arraycopy(id, 0, cmd, 2, 8);
for (int i = 0; i < blocks; ++i) {
cmd[10] = (byte)((offset + i) & 0x0ff);
System.arraycopy(data, i, cmd, 11, 4);
response = tag5.transceive(cmd);
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
return;
}
}
}
}
当我在应用程序 TagInfo 中读取标签时,输出是:
[00]。 41 42 43 44 [ABCD]
[01]。 42 43 44 45 [BCDE]
[02]。 43 44 45 46 [CDEF]
[03]。 44 45 46 47 [DEFG]
[04]。 45 46 47 48 [EFGH]
[05]。 46 47 48 49 [FGHI]
[06]。 47 48 49 4A [GHIJ]
[07]。 48 49 4A 4B [HIJK]
[08]。 00 00 00 00 [。 . . .]
。 . .
这个输出正确吗?
如果'NOT',我哪里错了?
对我来说这看起来不对,但不是 NfcV 专家只使用 NDEF nfc 卡。
[00] . 41 42 43 44 [ABCD]
[01] . 45 46 47 48 [EFGH]
[02] . 49 4A 4B 4C [IJKL]
作为你真正想做的事情
我觉得问题出在System.arraycopy(data, i, cmd, 11, 4);
您正在从源数据数组中复制 4 个字节的数据,但仅将起始位置增加 1 个字节的数据,因此下一个块从后面的字母开始。
我认为System.arraycopy(data, i*4, cmd, 11, 4);
会产生你想要的结果。
因为这会将源数据中 arraycopy 的开始增加您已经存储的字节数。
因为你是12个字节的数据,每个块存储4个字节你只需要使用3个块,所以通过设置int blocks = 3;
只循环3次否则你将运行没有数据复制输入 cmd 发送到从 arraycopy
IndexOutOfBoundsException
的卡
如果您没有 4 字节的倍数数据,则必须用零填充数据以使其成为 4 字节的倍数,或者将 IndexOutOfBoundsException
从 arraycopy
正确处理复制剩余的字节。