gpt4 book ai didi

Android NFC java.io.IOException : Transceive failed

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:53 25 4
gpt4 key购买 nike

我正在开发一个可以在 NFC 标签上读写的 android 应用程序。我可以毫无问题地读取我已经写过东西的标签,但是当我使用空白标签时,我很难读取十六进制代码中标签的 UID。

我使用的是 mifare 经典标签,我使用 readblock 方法直接读取十六进制的 UID。奇怪的是,它在我获得 UID 的调试器模式下完美运行。但是当我在没有调试器的情况下尝试时,我得到以下异常:

   java.io.IOException: Transceive failed

这是我读入标签的方法:

    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";}
}

我不知道如何解决这个问题,因为它在 Debug模式下工作。

最佳答案

这段代码对我有用。在读取 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();
}

关于Android NFC java.io.IOException : Transceive failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com