Forum Stats

  • 3,770,460 Users
  • 2,253,117 Discussions
  • 7,875,465 Comments

Discussions

Problem with APDU via USB Connection

P.S. My english so poor but .. Please help me!

I want to create an android app to find my micro-usb (smartcard reader device), so I have a problem with receive data in smartcard (I sent an APDU that is 0x... to smartcard by method usbDeviceConnection.bulkTransfer. But I dont know how to receive data or something that answer back (It have to answer a data in card back to me after i sent command). And when it answer back how can I encode or translate hex to string ?

I guess, (ref from code) I have a 2 endpoint so i have question about endpointIn and endpointOut. What are these params? How can I use it to send and receive data between device and app ? I think right, or not? And what is BroadcastReceiver do ?

Please give me some advise to solve this problem.

Thank you so much.

protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

   tv = (TextView) findViewById(R.id.textview);

   btnLED = (ToggleButton) findViewById(R.id.ledbutton);

   btnLED.setOnClickListener(btnLEDOnClickListener);

   //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();

}

private void connectUsb() {

  Toast.makeText(MainActivity.this,

   "connectUsb()",

  Toast.LENGTH_LONG).show();

  searchEndPoint();

   if (usbInterfaceFound != null) {

  setupUsbComm();

   threadUsbTx = new ThreadUsbTx(usbDeviceConnection, endpointOut);

   threadUsbTx.start();

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

  }

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;

   }

  }

  }

  }