java nfc_Android NFC java.io.IOException: Transceive failed

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值