我试着写一个简单的Hello World字符串扇区0上的Mifare 1K标签的第一个块。这个标签是新品牌的默认配置。该部门的拖车(3座),读的时候,就是: 00 00 00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF FF FF
。因此,准入条件是 FF 07 80 69
,这意味着我可以读取和写入与关键A中的每个块。
不过,我不能设法写在标签上任何东西。这是我的code:
尝试{
mfc.connect();
布尔AUTH = FALSE;
AUTH = mfc.authenticateSectorWithKeyA(0,MifareClassic.KEY_DEFAULT);
如果(AUTH){
文本字符串=你好,世界!;
byte []的值= text.getBytes();
byte []的toWrite =新的字节[MifareClassic.BLOCK_SIZE]
的for(int i = 0; I< MifareClassic.BLOCK_SIZE;我++){
如果(I< value.length)toWrite [i] =值[I]
别的toWrite [I] = 0;
}
mfc.writeBlock(0,toWrite);
}
我收到以下异常:收发失败
我是什么做错了吗?
下面的堆栈跟踪:
07-09 00:19:44.836:W / System.err的(13167):在android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:52)
07-09 00:19:44.843:W / System.err的(13167):在android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151)
07-09 00:19:44.843:W / System.err的(13167):在android.nfc.tech.MifareClassic.writeBlock(MifareClassic.java:453)
07-09 00:19:44.843:W / System.err的(13167):在com.example.andorid.apis.mifare.MainActivity.resolveIntent(MainActivity.java:128)
07-09 00:19:44.843:W / System.err的(13167):在com.example.andorid.apis.mifare.MainActivity.onNewIntent(MainActivity.java:275)
07-09 00:19:44.843:W / System.err的(13167):在android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1123)
07-09 00:19:44.843:W / System.err的(13167):在android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2041)
07-09 00:19:44.843:W / System.err的(13167):在android.app.ActivityThread.performNewIntents(ActivityThread.java:2054)
07-09 00:19:44.843:W / System.err的(13167):在android.app.ActivityThread.handleNewIntent(ActivityThread.java:2063)
07-09 00:19:44.843:W / System.err的(13167):在android.app.ActivityThread.access $ 1400(ActivityThread.java:122)
07-09 00:19:44.843:W / System.err的(13167):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1193)
07-09 00:19:44.851:W / System.err的(13167):在android.os.Handler.dispatchMessage(Handler.java:99)
07-09 00:19:44.851:W / System.err的(13167):在android.os.Looper.loop(Looper.java:137)
07-09 00:19:44.851:W / System.err的(13167):在android.app.ActivityThread.main(ActivityThread.java:4340)
07-09 00:19:44.851:W / System.err的(13167):在java.lang.reflect.Method.invokeNative(本机方法)
07-09 00:19:44.851:W / System.err的(13167):在java.lang.reflect.Method.invoke(Method.java:511)
07-09 00:19:44.851:W / System.err的(13167):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 00:19:44.851:W / System.err的(13167):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 00:19:44.851:W / System.err的(13167):在dalvik.system.NativeStart.main(本机方法)
解决方案
正在试图写入块0,这是不可能的。块0始终只读的,甚至的准入条件,不能不说是可写的。块0 contais的UID和其他一些制造商的数据。试着写的块1或2,而不是看你是否仍然有同样的问题。
I'm trying to write a simple "Hello World" string to the first block of sector 0 on a Mifare 1K tag. The tag is brand new with default configuration. The sector trailer (block 3), when read, is: 00 00 00 00 00 00 00 00 ff 07 80 69 ff ff ff ff ff ff ff ff
. Hence, the access conditions is ff 07 80 69
, meaning I can read and write on each block with key A.
Still, I can't manage to write anything on the tag. Here's my code:
try {
mfc.connect();
boolean auth = false;
auth = mfc.authenticateSectorWithKeyA(0,MifareClassic.KEY_DEFAULT);
if (auth) {
String text = "Hello, World!";
byte[] value = text.getBytes();
byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];
for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) {
if (i < value.length) toWrite[i] = value[i];
else toWrite[i] = 0;
}
mfc.writeBlock(0, toWrite);
}
I'm getting the following exception: Transceived failed
.
What am I doing wrong?
Here's the stack trace:
07-09 00:19:44.836: W/System.err(13167): at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:52)
07-09 00:19:44.843: W/System.err(13167): at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151)
07-09 00:19:44.843: W/System.err(13167): at android.nfc.tech.MifareClassic.writeBlock(MifareClassic.java:453)
07-09 00:19:44.843: W/System.err(13167): at com.example.andorid.apis.mifare.MainActivity.resolveIntent(MainActivity.java:128)
07-09 00:19:44.843: W/System.err(13167): at com.example.andorid.apis.mifare.MainActivity.onNewIntent(MainActivity.java:275)
07-09 00:19:44.843: W/System.err(13167): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1123)
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2041)
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2054)
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2063)
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread.access$1400(ActivityThread.java:122)
07-09 00:19:44.843: W/System.err(13167): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1193)
07-09 00:19:44.851: W/System.err(13167): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 00:19:44.851: W/System.err(13167): at android.os.Looper.loop(Looper.java:137)
07-09 00:19:44.851: W/System.err(13167): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-09 00:19:44.851: W/System.err(13167): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 00:19:44.851: W/System.err(13167): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 00:19:44.851: W/System.err(13167): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 00:19:44.851: W/System.err(13167): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 00:19:44.851: W/System.err(13167): at dalvik.system.NativeStart.main(Native Method)
解决方案
You are trying to write to block 0, which is not possible. Block 0 is always read-only, even of the access conditions say that it is writeable. Block 0 contais the UID and some other manufacturer data. Try writing to block 1 or 2 instead and see whether you still have the same problem.