use*_*450 7 android exception nfc
我正在开发一个可以在NFC标签上读写的Android应用程序.我在读取已经写过的标签时没有问题,但是当我使用空白标签时,我很难在HEX代码中读取标签的UID.
我正在使用mifare经典标签,我使用readblock方法直接在十六进制中读取UID.奇怪的是,它在调试器模式下完美运行,我获得了UID.但是当我在没有debbuger的情况下尝试时,我得到以下异常:
java.io.IOException: Transceive failed
Run Code Online (Sandbox Code Playgroud)
这是我读取标记的方法:
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";}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题,因为它在调试模式下工作.
可能存在身份验证问题。您可以通过这种方式验证这一点......
if (mfc.authenticateSectorWithKeyA(sectorNumber,
MifareClassic.KEY_MIFARE_APPLICATION_DIRECTORY)) {
Log.d("TAG", "Authorized sector with MAD key");
} else if (mfc.authenticateSectorWithKeyA(
sectorNumber, MifareClassic.KEY_DEFAULT)) {
Log.d("TAG",
"Authorization granted to sector with DEFAULT key");
} else if (mfc
.authenticateSectorWithKeyA(sectorNumber,
MifareClassic.KEY_NFC_FORUM)) {
Log.d("TAG",
"Authorization granted to sector with NFC_FORUM key");
} else {
Log.d("TAG", "Authorization denied ");
return false;
}
Run Code Online (Sandbox Code Playgroud)
这里的 SectorNumber 是:您要验证的扇区。例如:0,1,2....15 for mifare Classic 1K 当身份验证完成后,您可以读取或写入。
| 归档时间: |
|
| 查看次数: |
4673 次 |
| 最近记录: |