我是 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 [。. . .]
. . .
这个输出正确吗?
如果“不是”,我哪里错了?