gpt4 book ai didi

android - NTAG213 : Android throws IOException when trying to authenticate with wrong password

转载 作者:行者123 更新时间:2023-11-30 01:19:27 26 4
gpt4 key购买 nike

我正在编写一个 Android 应用程序,它应该使用 NXP 的写保护 NTAG213 NFC 标签进行身份验证。使用正确的密码一切正常,我得到了 PACK 作为返回。

尝试使用错误的密码进行身份验证,我的代码没有按预期运行。根据标签的描述,在这种情况下我应该会收到 NAK,但我却收到了 TagLostException。还是我弄错了,一切都按预期进行?那么我如何区分错误的密码和实际的 TagLostException

如果有人能告诉我我做错了什么,我将不胜感激。

try {
nfcA.connect();

byte[] response = nfcA.transceive(new byte[]{
NTAG213_PWD_AUTH, mTagPassword[0], mTagPassword[1],
mTagPassword[2], mTagPassword[3]
});

// checking if response was a NAK
if (((byte) 0x00 == response[0]) ||
((byte) 0x01 == response[0]) ||
((byte) 0x04 == response[0]) ||
((byte) 0x05 == response[0])) {

nfcA.close();
result.setErrorCode(WRONG_PASSWORD);
return result;
}
}catch (TagLostException e){
result.setErrorCode(TAG_LOST_EXCEPTION);
e.printStackTrace();
return result;
}catch (IOException e){
result.setErrorCode(IO_EXCEPTION);
e.printStackTrace();
return result;
}

最佳答案

你是对的,如果身份验证命令失败,标签将使用 NACK 响应进行应答。这与标签不支持命令的情况相同。

不幸的是,各种 Android NFC 堆栈实现以不同的方式处理 NACK 响应。它们要么在响应字节数组中返回 NACK 值,返回空响应 (?),要么生成 IOException(通常是 TagLostException)。因此,您无法可靠地区分身份验证失败、命令不受支持或与标签的通信实际中断的情况。

为了检测标签是否仍然存在(因此,无论出于何种原因,它只是命令失败),您可以发出 READ 命令(或您希望对特定标签成功的其他命令)之后输入)。如果同样失败,您可以确定与标签的通信丢失了。

请注意,您需要断开连接 (NfcA.close()) 并重新连接 (NfcA.connect()) 命令失败后以及继续操作之前发送其他命令。无论您是从 transceive() 方法收到 IOException 还是实际的 NACK 响应值,都应该执行此操作。这通常是必要的,以便在任何命令失败后重置标签的状态。

关于android - NTAG213 : Android throws IOException when trying to authenticate with wrong password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37350458/

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