I have a usb device which have a button.
And I want to an android app to catch a signal of the button.
I found inferface and endpoint number of the button.
It had seemed to perform ordinarily at galaxy S3 and galaxy note.
But later, I found that it has delay at other phones.
I was able to receive instant responses about 10% of the time; usually
there was a 2-second delay, with some cases where the whole response was
lost.
Although I couldn't figure out the exact reason, I realized that the phones
that had response delays were those with kernel version 3.4 or later.
Here is the code that I used initially.
if(mConnection != null){
mConnection.claimInterface(mInterfaces.get(0), true);
final UsbEndpoint endpoint = mInterfaces.get(0).getEndpoint(0);
Thread getSignalThread = new Thread(new Runnable() {
@Override
public synchronized void run() {
byte[] buffer = new byte[8];
final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
while(mConnection!=null){
int len = mConnection.bulkTransfer(endpoint, buffer, buffer.length, 0);
if( len>=0 ){
// do my own code
}
}
}
});
getSignalThread.setPriority(Thread.MAX_PRIORITY);
getSignalThread.start();
}
***edit timeout** *
when the timeout was set to 50ms, I wasn't able to receive responses most
of the time. When the timeout was 500ms, I was able to initially get some
delayed-responses; however, I lost all responses after several tries with
this setting.
***Using UsbRequest***
In addition to using the bulktransfer method, I also tried using UsbRequest
and below is the code that I used.
@Override
public synchronized void run() {
byte[] buffer = new byte[8];
final ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
UsbRequest inRequest = new UsbRequest();
inRequest.initialize(mConnection, endpoint);
while(mConnection!=null){
inRequest.queue( byteBuffer , buffer.length);
if( mConnection.requestWait() == inRequest ){
// do my own code
}
}
}
However, the same kind of delay happened even after using UsbRequest.
***Using libusb***
I also tried using
[libusb_interrupt_transfer](http://libusb.sourceforge.net/api-1.0/group__syncio.html#gac412bda21b7ecf57e4c76877d78e6486)
from an open source library called libusb.
However this also produced the same type of delay that I had when using
UsbDeviceConnection.
unsigned char data_bt[8] = { 0, };
uint32_t out[2];
int transfered = 0;
while (devh_usb != NULL) {
libusb_interrupt_transfer(devh_usb, 0x83, data_bt, 8, &transfered, 0);
memcpy(out, data_bt, 8);
if (out[0] == PUSH) {
LOGI("button pushed!!!");
memset(data_bt, 0, 8);
//(env)->CallVoidMethod( thiz, mid);
}
}
After looking into the part where libusb_interrupt_transfer is processed
libusb, I was able to figure out the general steps of interrupt_transfer:
1. make a transfer object of type interrupt
2. make a urb object that points to the transfer object
3. submit the urb object to the device's fd
4. detect any changes in the fd object via urb object
5. read urb through ioctl
steps 3, 4, 5 are the steps regarding file i/o.
I was able to find out that at step 4 the program waits for the button
press before moving onto the next step.
Therefore I tried changing poll to epoll in order to check if the poll
function was causing the delay; unfortunately nothing changed.
I also tried setting the timeout of the poll function to 500ms and making
it always get values of the fd through ioctl but only found out that the
value changed 2~3 seconds after pressing the button.
So in conclusion I feel that there is a delay in the process of updating
the value of the fd after pressing the button. If there is anyone who could
help me with this issue, please let me know. Thank you.
Thanks for reading
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/aafaf47e-b8c6-4d40-b94d-f73ce5ae22c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.