簡體   English   中英

APDU命令異步調用

[英]APDU Command asynchronous calls

我一直在使用有線智能卡讀卡器SDK,其中調用同步。 最近一直嘗試藍牙接口,但APDU命令是異步的,因此我們無法解釋為形成下一個APDU命令而發送的呼叫的響應。

任何幫助都感激不盡。

請求

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) { }

您可以將異步調用封裝為同步調用。 主要思想是在等待結果時阻止當前線程。 我喜歡使用鎖,所以我使用了java.util.concurrent.locks.ReentrantLock 但是有很多方法可以實現這種行為,比如java.util.concurrent.Future<T>Busy Waiting

// 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() 否則就會出現僵局。

如果您需要更多靈感,還有關於此主題的其他帖子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM