【发布时间】:2019-10-29 17:55:43
【问题描述】:
我是 NFC 新手,我正在开发一个 android 应用程序来在 nfc 中读取和写入数据,但我遇到了一些问题。
这是我正在使用的代码(WRITE):
@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格式吗?因为这在 Android 中的支持非常差,甚至某些 Android 手机中的某些硬件对这种格式的支持有限。如果您可以使用支持更好的 NDEF 格式。
-
是的,我需要使用 NfcV !!
-
我唯一能建议的就是你看看stackoverflow.com/a/37098162/2373819 并使用“命令的地址版本”
标签: android android-studio tags nfc iso-15693