Java 通过USB连接的APDU

Java 通过USB连接的APDU,java,android,usb,apdu,smartcard-reader,Java,Android,Usb,Apdu,Smartcard Reader,我想创建一个android应用程序来查找我的micro usb(智能卡读卡器设备),因此我在智能卡中接收数据时遇到问题(我通过方法usbdeviconnection.bulkTransfer向智能卡发送了一个0x…的APDU。但我不知道如何接收数据或其他回复(它必须在我发送命令后将卡中的数据回复给我)。当它回复时,我如何将十六进制编码或转换为字符串 我猜,(参考代码)我有一个2个端点,所以我对endpointIn和endpointOut有疑问。这些参数是什么?我如何使用它在设备和应用程序之间发送

我想创建一个android应用程序来查找我的micro usb(智能卡读卡器设备),因此我在智能卡中接收数据时遇到问题(我通过方法usbdeviconnection.bulkTransfer向智能卡发送了一个0x…的APDU。但我不知道如何接收数据或其他回复(它必须在我发送命令后将卡中的数据回复给我)。当它回复时,我如何将十六进制编码或转换为字符串

我猜,(参考代码)我有一个2个端点,所以我对endpointIn和endpointOut有疑问。这些参数是什么?我如何使用它在设备和应用程序之间发送和接收数据?我认为正确与否?广播接收器做什么

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
   tv = (TextView) findViewById(R.id.textview);   
   //register the broadcast receiver
   mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
  IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  registerReceiver(mUsbReceiver, filter);

  registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
  registerReceiver(mUsbDeviceReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));

  connectUsb();
}
这里是连接USB的方法

private void connectUsb() {
  Toast.makeText(MainActivity.this,
   "connectUsb()",
  Toast.LENGTH_LONG).show();


  searchEndPoint();

   if (usbInterfaceFound != null) {
  setupUsbComm();

   threadUsbTx = new ThreadUsbTx(usbDeviceConnection, endpointOut);
   threadUsbTx.start();

   //bytes are APDU to send for get data from card.
   threadUsbTx.insertCmd(
   new byte[]{(byte) 0xA0, (byte) 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00});
   threadUsbTx.insertCmd(
   new byte[]{(byte)0x80, (byte)0xb0, 0x00, 0x04, 0x02, 0x00, 0x0d});
   threadUsbTx.insertCmd(
   new byte[]{0x00, (byte)0xc0, 0x00, 0x00, 0x0d});
   }
  }
这是ThreadUsbTx类

class ThreadUsbTx extends Thread {
   boolean running;

  UsbDeviceConnection txConnection;
  UsbEndpoint txEndpoint;
  Queue<byte[]> cmdQueue;
   byte[] cmdToSent;


  ThreadUsbTx(UsbDeviceConnection conn, UsbEndpoint endpoint) {
   txConnection = conn;
   txEndpoint = endpoint;
   cmdQueue = new LinkedList<byte[]>();
   cmdToSent = null;

   running = true;
  }

   public void setRunning(boolean r) {
   running = r;
  }

   public void insertCmd(byte[] cmd) {
   synchronized (cmdQueue) {
   cmdQueue.add(cmd);
  }
  }

   @Override
   public void run() {

   while (running) {

   synchronized (cmdQueue) {
   if (cmdQueue.size() > 0) {
   cmdToSent = cmdQueue.remove();
  }
  }

   if (cmdToSent != null) {

   final int usbResult = usbDeviceConnection.bulkTransfer(
   txEndpoint,
   cmdToSent,
   cmdToSent.length,
   0);

   final String s = new String(cmdToSent);
  String result = null;

   try {
  result = new String(cmdToSent,"TIS620"); // To Thai language
  } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
  }

   final String finalResult = result;
  runOnUiThread(new Runnable() {

   @Override
   public void run() {
  Toast.makeText(MainActivity.this,
   finalResult+" "+String.format("%040x",cmdToSent),
  Toast.LENGTH_LONG).show();
  }
  });
   cmdToSent = null;
   }
  }
  }
  }
类ThreadUsbTx扩展线程{
布尔运算;
UsbDeviceConnection txConnection;
UsbEndpoint txEndpoint;
队列管理器队列;
字节[]cmdToSent;
线程USBTX(UsbDeviceConnection conn,UsbEndpoint端点){
txConnection=conn;
txEndpoint=终点;
cmdQueue=new LinkedList();
cmdToSent=null;
运行=真;
}
公共void setRunning(布尔r){
运行=r;
}
公共void insertCmd(字节[]cmd){
已同步(cmdQueue){
cmdQueue.add(cmd);
}
}
@凌驾
公开募捐{
(跑步时){
已同步(cmdQueue){
如果(cmdQueue.size()>0){
cmdToSent=cmdQueue.remove();
}
}
如果(cmdToSent!=null){
final int usbResult=usbDeviceConnection.bulkTransfer(
txEndpoint,
cmdToSent,
cmdToSent.length,
0);
最终字符串s=新字符串(cmdToSent);
字符串结果=null;
试一试{
结果=新字符串(cmdToSent,“TIS620”);//转换为泰语
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
最终字符串finalResult=结果;
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(MainActivity.this,
finalResult+“”+String.format(“%040x”,cmdToSent),
Toast.LENGTH_LONG).show();
}
});
cmdToSent=null;
}
}
}
}

与USB读卡器通信的最佳方式是将符合CCID的读卡器与支持OTG的手机一起使用。请查看应用程序“CCID读卡器应用程序演示”在Playstore中。也可用。否则,您必须在代码中或多或少实现CCID行为

您的读卡器是否希望通过USB连接直接传输APDU,还是需要一些中间协议(例如,它是否像典型的智能卡读卡器一样遵循CCID设备类)?我只知道只有APDU可以与智能卡通信,所以我有一个示例应用程序,它可以通过USB连接发送APDU。我反编译了它,发现它们也使用USB连接。如果我需要知道什么,请告诉我更多。谢谢。你的英语还可以,只是小心不要在
前面加空格,并且始终保持write“I”(从来不是“I”)vendorID=1423 ProductID=38208 proname=EMV智能卡读卡器再次感谢您: