首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APDU命令异步调用

APDU命令异步调用
EN

Stack Overflow用户
提问于 2017-06-13 15:04:19
回答 1查看 336关注 0票数 1

我一直在使用有线智能卡读卡器SDK,其中的调用是同步的。最近一直在尝试蓝牙接口,但是APDU命令是异步的,因此我们无法解释为形成下一个APDU命令而发送的调用的响应。

任何帮助都将不胜感激。

请求

代码语言:javascript
复制
byte[] var1 = {(byte)0, (byte)-92, (byte)4, (byte)0, (byte)12, (byte)-96, (byte)0, (byte)0, (byte)2, (byte)67, (byte)0, (byte)19, (byte)0, (byte)0, (byte)0, (byte)1, (byte)1};

        String apduString = QPOSUtil.byteArray2Hex(var1);
        pos.sendApdu(apduString);

结果:

@Override public void onReturnApduResult(boolean arg0, String arg1, int arg2) { }

EN

回答 1

Stack Overflow用户

发布于 2017-06-26 13:51:27

您可以将异步调用封装为同步调用。主要思想是在等待结果时阻塞当前线程。我喜欢使用锁,所以我使用了java.util.concurrent.locks.ReentrantLock。但是有很多方法来实现这种行为,比如java.util.concurrent.Future<T>忙着等待

代码语言:javascript
复制
// Lock that is used for waiting
private final ReentrantLock waitLock = new ReentrantLock();

private APDUResult result = null;

// synchronous wrapper 
// synchronized keyword is used to serialize requests
public synchronized APDUResult sendAPDUSynchronous(byte[] apdu) {
    // calles asynchronous function
    sendAPDUAsynchronous(apdu);
    // waits until .unlock() is called
    waitLock.lock();
    // returns the result
    return result;
}

// this function sould not be called outside - so its private
private void sendAPDUAsynchronous(byte[] apdu) {
    String apduString = QPOSUtil.byteArray2Hex(var1);
    pos.sendApdu(apduString);
}

@Override
public void onReturnApduResult(boolean arg0, String arg1, int arg2) {
    // stores the result
    result = new APDUResult(arg0, arg1, arg2);
    // notifys the waiting thread in synchronous call
    waitLock.unlock();
}

APDUResult只是将这三个参数封装到一个可以通过同步函数进行处理的对象中。不要忘记在错误回调时调用waitLock.unlock()。否则就会陷入僵局。

如果您需要更多的灵感,也有关于这个主题的其他帖子

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44525208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档