2

The following code is inside a thread and reads input data coming over usb. Approximately every 80 readings it misses one of the packets coming from an stm32 board. The board is programmed to send data packets to the android tablet every one second.

// Non Working Code
while(running){
    int resp = bulktransfer(mInEp,mBuf,mBuf.lenght,1000);
    if(resp>0){
        dispatchMessage(mBuf);
    }else if(resp<0)
        showsBufferEmptyMessage();
} 

I was looking the Missile Launcher example in android an other libraries on the internet and they put a delay of 50ms between each poll. Doing this it solves the missing package problem.

  //Working code
  while(running){
     int resp = bulktransfer(mInEp,mBuf,mBuf.lenght,1000);
     if(resp>0){
        dispatchMessage(mBuf);
    }else if(resp<0)
        showsBufferEmptyMessage();
   try{
       Thread.sleep(50);
   }catch(Exception e){}
  }

Does anyone knows the reason why the delay works. Most of the libraries on github has this delay an as I mention before the google example too.

Pablo Valdes
  • 734
  • 8
  • 19

1 Answers1

2

I am putting down my results regarding this problem. After all seems that the UsbConnection.bulkTransfer(...) method has some bug when called continuously. The solution was to use the asynchronous API, UsbRequest class. Using this method I could read from the input endpoint without delay and no data was lost during the whole stress test. So the direction to take is asynchronous UsbRequest instead of synchronously bulktransfer.

Pablo Valdes
  • 734
  • 8
  • 19