我一直在使用有线智能卡读卡器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) { }
发布于 2017-06-26 13:51:27
您可以将异步调用封装为同步调用。主要思想是在等待结果时阻塞当前线程。我喜欢使用锁,所以我使用了java.util.concurrent.locks.ReentrantLock。但是有很多方法来实现这种行为,比如java.util.concurrent.Future<T>或忙着等待。
// 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()。否则就会陷入僵局。
如果您需要更多的灵感,也有关于这个主题的其他帖子。
https://stackoverflow.com/questions/44525208
复制相似问题