java nfc 接收数据,Android NFC java.io.IOException:收发失败

I am working on an android application which can read and write on an NFC tag.

I have no problem reading a tag which I already wrote something on, but when I use a blank tag I have difficulties reading the UID of the tag in the HEX code.

I am using mifare classic tags and I read the UID directly in the hex with the readblock method. The strange thing is, it works perfectly on debugger mode where I get the UID. But when I am trying without debbuger I get the following exception:

java.io.IOException: Transceive failed

Here's my method to read into the tag :

static String getUID(Intent intent) {

Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

MifareClassic mc = MifareClassic.get(tagFromIntent);

try {

mc.connect();

Log.i("connect", "ok");

} catch (IOException e) {

// TODO Auto-generated catch block

Log.i("connect", "nok");

e.printStackTrace();

}

try {

boolean secA = mc.authenticateSectorWithKeyA(0, mc.KEY_DEFAULT);

Log.i("secA", "ok");

} catch (IOException e) {

Log.i("secA", "nok");

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

boolean secB = mc.authenticateSectorWithKeyB(0, mc.KEY_DEFAULT);

Log.i("secB", "ok");

} catch (IOException e) {

Log.i("secB", "nok");

// TODO Auto-generated catch block

e.printStackTrace();

}

byte[] uidBytes = null;

try {

uidBytes = mc.readBlock(0);

Log.i("bytes", "ok");

} catch (IOException e) {

Log.i("bytes", "nok");

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

mc.close();

Log.i("close", "ok");

} catch (IOException e) {

Log.i("close", "nok");

// TODO Auto-generated catch block

e.printStackTrace();

}

if (uidBytes != null) {

String uid = HexToString(uidBytes);

return uid;

}

else { return "Repasser le tag";}

}

I have no idea how to fix this, since it works in debug mode.

解决方案

This code works for me. you have to check the authentication before you can read a block.

MifareClassic mif = MifareClassic.get(detectedTag);

int ttype = mif.getType();

Log.d(TAG, "MifareClassic tag type: " + ttype);

int tsize = mif.getSize();

Log.d(TAG, "tag size: " + tsize);

int s_len = mif.getSectorCount();

Log.d(TAG, "tag sector count: " + s_len);

int b_len = mif.getBlockCount();

Log.d(TAG, "tag block count: " + b_len);

try {

mif.connect();

if (mif.isConnected()){

for(int i=0; i< s_len; i++){

boolean isAuthenticated = false;

if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {

isAuthenticated = true;

} else if (mif.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)) {

isAuthenticated = true;

} else if (mif.authenticateSectorWithKeyA(i,MifareClassic.KEY_NFC_FORUM)) {

isAuthenticated = true;

} else {

Log.d("TAG", "Authorization denied ");

}

if(isAuthenticated) {

int block_index = mif.sectorToBlock(i);

byte[] block = mif.readBlock(block_index);

String s_block = NfcUtils.ByteArrayToHexString(block);

Log.d(TAG, s_block);

}

}

}

mif.close();

} catch (IOException e) {

e.printStackTrace();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值