我正在尝试创建一个使用 OMAPI 库将 APDU 传输到 SIMCARD 的应用程序,但 SEService 的 OnConnected 侦听器从未被调用。
我等待了 30 秒,看看是否调用了
OnConnectedListener
方法,但这从未发生。我总是收到代码中留下的错误:throw new TimeoutException( "Service could not be connected after " + SERVICE_CONNECTION_TIME_OUT + "ms");
我使用的SIMCARD包含ARA-M小程序,以及必要的SHA-1密钥,即应用程序获得运营商权限。 在另一个使用 TelephonyManager 库的应用程序中,我成功传输了 APDU。
如果我做错了什么,请您帮助我理解吗? 我需要在我的应用程序清单中声明一些权限吗?
提前谢谢您。
我给你留下了我实现的部分代码:
public class MainActivity extends AppCompatActivity [...]{
/*FOR OMAPI*/
private final Object serviceMutex = new Object();
private SEService seService;
private boolean connected = false;
private Timer connectionTimer;
private ServiceConnectionTimerTask mTimerTask = new ServiceConnectionTimerTask();
private final long SERVICE_CONNECTION_TIME_OUT = 30000;
//////////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
[...]
setUp(getApplicationContext());
try {
waitForConnection();
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
Reader[] readers = seService.getReaders();
byte [] apdu = {(byte)0x80, (byte)0x02, (byte)0xfC, (byte)0x00};
for (Reader reader : readers){
byte [] response = new byte[0];
try {
response = Util.internalTransmitAPDU(reader, apdu);
} catch (IOException e) {
throw new RuntimeException(e);
}
Log.i("response OMAPI", String.valueOf(response));
}
}
public void setUp(Context context){
seService = new SEService(context, new SynchronousExecutor(), mListener);
connectionTimer = new Timer();
connectionTimer.schedule(mTimerTask, SERVICE_CONNECTION_TIME_OUT);
}
static class SynchronousExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
}
private final SEService.OnConnectedListener mListener = () -> {
synchronized (serviceMutex){
connected = true;
serviceMutex.notify();
Log.i("OMAPI", "EntraOnconnect");
}
private void waitForConnection() throws TimeoutException {
synchronized (serviceMutex) {
if (!connected) {
try {
serviceMutex.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!connected && !seService.isConnected()) {
throw new TimeoutException(
"Service could not be connected after " + SERVICE_CONNECTION_TIME_OUT
+ " ms");
}
if (connectionTimer != null) {
connectionTimer.cancel();
}
}
}
}
尝试使用
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
权限。也许缺少这个。
您也可以尝试使用不同的
Executor
来代替SynchronousExecutor
吗?例如:
Executors.newSingleThreadExecutor()
如果不在不同的线程中运行,您的代码可能会被阻塞。