NFC超时应在哪里处理?

发布于 2025-01-30 05:54:13 字数 1586 浏览 4 评论 0 原文

在Android应用程序中的NFC超时情况下,我需要返回服务器特定的返回代码。

我找到了设置超时的文档,但是我找不到有关该超时应如何处理的文档。

超时本身应该在哪里处理,是例外还是应该覆盖的方法?

private boolean ConnectToTag(@NonNull IsoDep isoDep) {

    if (!isoDep.isConnected()) {

        try {

            isoDep.connect();
            isoDep.setTimeout(5000);

        }  catch (Exception e) {

            HyperLog.e(TAG, "Could not connect to tag", e);
            onNFCException(SIM.TRANSMISSION_ERROR);
            return false;
        }

    }
    return true;

}

@Nullable
public byte[] Transceive(byte[] apdu) {


    try {
        if (!isoDep.isConnected()) {
            boolean connected = ConnectToTag(isoDep);
        }
        if (isoDep.isConnected()) {

            byte[] response;
            HyperLog.i(TAG, "NFC => " + ByteUtils.BytesToHexString(apdu));
           
            response = isoDep.transceive(apdu);
           

            if (response == null) {
                HyperLog.i(TAG, "NFC <= null");
            } else {
                HyperLog.i(TAG, "NFC <= " + ByteUtils.BytesToHexString(response));
                return response;
            }

        } else {
            HyperLog.e(TAG, "Transceive() - Tag disconnected.");
        }

    } catch (TagLostException e) {
        HyperLog.e(TAG, "Transceive() - Error transceiving data", e);
        onNFCException(SIM.TRANSMISSION_ERROR);
    }
}

I need to return to a server a specific return code in the case of a NFC timeout in an android application.

I have found the documentation for the setting a timeout, however I cant find documentation about how this timeout should/can be handled if it occurs.

https://developer.android.com/reference/android/nfc/tech/IsoDep#getTimeout()

Where should the timeout itself be handled , is it an exception or is there a method that should be overridden?

private boolean ConnectToTag(@NonNull IsoDep isoDep) {

    if (!isoDep.isConnected()) {

        try {

            isoDep.connect();
            isoDep.setTimeout(5000);

        }  catch (Exception e) {

            HyperLog.e(TAG, "Could not connect to tag", e);
            onNFCException(SIM.TRANSMISSION_ERROR);
            return false;
        }

    }
    return true;

}

@Nullable
public byte[] Transceive(byte[] apdu) {


    try {
        if (!isoDep.isConnected()) {
            boolean connected = ConnectToTag(isoDep);
        }
        if (isoDep.isConnected()) {

            byte[] response;
            HyperLog.i(TAG, "NFC => " + ByteUtils.BytesToHexString(apdu));
           
            response = isoDep.transceive(apdu);
           

            if (response == null) {
                HyperLog.i(TAG, "NFC <= null");
            } else {
                HyperLog.i(TAG, "NFC <= " + ByteUtils.BytesToHexString(response));
                return response;
            }

        } else {
            HyperLog.e(TAG, "Transceive() - Tag disconnected.");
        }

    } catch (TagLostException e) {
        HyperLog.e(TAG, "Transceive() - Error transceiving data", e);
        onNFCException(SIM.TRANSMISSION_ERROR);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2025-02-06 05:54:13

响应= isodep.transceive(apdu);

也可以投掷 ioexception

因此您的尝试/捕获也应处理此例外,因为这是可能返回超时的方式。

response = isoDep.transceive(apdu);

can also throw an IOException

so your try/catch should also handle this Exception as this is how a timeout is likely to be returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文