Android Nexus S 中的 Mifare Classic 1k 卡写入失败,出现错误 java.ioexception 收发失败
当我尝试在 Android 2.3.3 API 级别 10 的 Nexus S 上的 Mifare classic 1k 卡中写入数据时,出现此错误。收发失败。 我的代码是
private View.OnClickListener write_butClickListener =new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter ndef=new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED );
try{
ndef.addDataType("*/*");
}catch(MalformedMimeTypeException e){
throw new RuntimeException("fail",e);
}
mFilters=new IntentFilter[]{ndef,};
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { MifareClassic.class
.getName() } };
Intent intent=getIntent();
String action=intent.getAction();
if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ){
String msg="Discovered Tag with Intent " + intent;
status_Data.setText(msg);
Tag tagFromintent=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mfc=MifareClassic.get(tagFromintent);
byte[] data;
try{
mfc.connect();
boolean auth = false;
String cardData = null;
status_Data.setText("Authenticating the Tag..");
auth = mfc.authenticateSectorWithKeyA(1,
MifareClassic.KEY_DEFAULT);
if (auth){
status_Data.setText("Authenticated");
String text = "Hello, World!";
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
if (!mfc.isConnected()) mfc.connect();
mfc.writeBlock(1, payload);
mfc.close();
showMessage("written");
}else{
showMessage("Authetication Failed");
status_Data.setText("");
}
}
catch(IOException e){
status_Data.setText(e.toString());
}
}else{
showMessage("Nothing to read");
}
}
};
任何指针
when i am trying to write a data in Mifare classic 1k card, on Nexus S with android 2.3.3 API level 10, I am having this error. transceive failed.
my code is
private View.OnClickListener write_butClickListener =new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter ndef=new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED );
try{
ndef.addDataType("*/*");
}catch(MalformedMimeTypeException e){
throw new RuntimeException("fail",e);
}
mFilters=new IntentFilter[]{ndef,};
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { MifareClassic.class
.getName() } };
Intent intent=getIntent();
String action=intent.getAction();
if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ){
String msg="Discovered Tag with Intent " + intent;
status_Data.setText(msg);
Tag tagFromintent=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mfc=MifareClassic.get(tagFromintent);
byte[] data;
try{
mfc.connect();
boolean auth = false;
String cardData = null;
status_Data.setText("Authenticating the Tag..");
auth = mfc.authenticateSectorWithKeyA(1,
MifareClassic.KEY_DEFAULT);
if (auth){
status_Data.setText("Authenticated");
String text = "Hello, World!";
String lang = "en";
byte[] textBytes = text.getBytes();
byte[] langBytes = lang.getBytes("US-ASCII");
int langLength = langBytes.length;
int textLength = textBytes.length;
byte[] payload = new byte[1 + langLength + textLength];
// set status byte (see NDEF spec for actual bits)
payload[0] = (byte) langLength;
// copy langbytes and textbytes into payload
System.arraycopy(langBytes, 0, payload, 1, langLength);
System.arraycopy(textBytes, 0, payload, 1 + langLength, textLength);
if (!mfc.isConnected()) mfc.connect();
mfc.writeBlock(1, payload);
mfc.close();
showMessage("written");
}else{
showMessage("Authetication Failed");
status_Data.setText("");
}
}
catch(IOException e){
status_Data.setText(e.toString());
}
}else{
showMessage("Nothing to read");
}
}
};
any pointers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试将 NDEF 数据直接写入 mifare 卡的低级块。
这不起作用的原因有两个:
在写入 mifare 块之前,您必须使用authenticateSectorWithKeyA 或authenticateSectorWithKeyB 函数对自己进行身份验证才能进行写入访问。 NDEF 格式的 mifare 卡的密钥在 MifareClassic 类中可用。
您不能直接将 NDEF 数据写入卡并期望其他应用程序能够解释该数据。 NDEF 是一种用户格式。写入卡的数据包含额外的数据,例如应用程序目录、大小信息等。
我建议您使用通用标签接口的 NDEF 写入功能。这些将为您完成所有额外的格式化。
如果您想尝试对卡进行低级写入,我建议您首先下载 Mifare 数据表并阅读扇区和块是什么。了解芯片的身份验证和安全概念也很重要。否则很容易意外覆盖身份验证块并破坏标签。
It seems like you're trying to directly write NDEF data onto the low-level blocks of the mifare card.
This won't work for two reasons:
Before you write to mifare blocks you have to authenticate yourself for write access using the authenticateSectorWithKeyA or authenticateSectorWithKeyB function. The keys for NDEF formatted mifare cards are available in the MifareClassic class.
You cannot directly write NDEF data onto the card and expect that other applications are able to interpret the data. NDEF is a user format. The data that gets written to the card contains extra data such as application directories, size information etc.
I suggest that you use the NDEF write functions of the generic Tag interface. These will do all the extra formatting for you.
If you want to experiment with low-level writing to the card I suggest that you first download the mifare datasheet and read what sectors and blocks are. Also it is important that you understand the authentication and security concept of the chip. Otherwise it is all to easy to accidentally overwrite the authentication blocks and destroy the tag.