Creating a Serial to USB driver using the Android USB-Host API

Lately we have received many concerns about using the serial interface on Android (Honeycomb with USB-Host). The way to usually communicate with a USB-Serial device in Linux, is to create a virtual serial interface and use it like a normal serial interface. For instance if you connect an FTDI controller via USB, a device /dev/ttyUSBX will be created.

USB-Serial Connection connects streams to Bulk Endpoints

However, behind the scenes you are actually communicating with the device using plain USB Bulk transfers. This job is done by the USB-Serial driver for you (ftdi_sio for instance). If you like to avoid using the kernel driver for some reason you can do this by using a library based on libusb called “libftdi”.

If you look at the source code you will notice that this library manages the connection (depending on the chip type) to the serial controller. Part of this task for instance is resetting TX/RX or setting the baudrate, which is not entirely trivial as it turns out.

Reading data is pretty simple (if your IN packet returns more than two bytes there is data ready). While when sending data you have to be sure not to write more than the max packet size defined by the controller. For details see the source code of libftdi.

Looking at Android there is a core problem with serial devices. As shown in a previous article it is pretty complicated to get started with building the driver. However, the even more painful part is, that there is no API-featured way to receive permissions for the /dev/USBX device node! So root is required.

But Hey! With libftdi, or it’s implementation it is now possible to write your own driver using the Android USB Host API. In the attached example application a connection to a VNC2 Controller is opened and each seconds one byte is transferred. For the example LEDs on the board are toggled. Using the Bulk OUT EP a rs232 “write” is emulated.

Download the Android source
Note that you should change the VID_PID String according to your device “VVVV:PPPP”.
Also note that if you would like to test if this works on your computer you should first remove the ftdi_sio driver using:
$ sudo rmmod ftdi_sio.

Debugging on your Ubuntu machine:
It’s probably much easier to debug on your workstation. Recently I found pyusb pretty helpful:
$ sudo apt-get install python-usb.
Here is a python script that will get you started: main.py.

64 thoughts on “Creating a Serial to USB driver using the Android USB-Host API”

  1. Hey Manuel,

    Unfortunately, I’m having several problems with this too. :-/

    The application won’t debug. I get the error “No Launcher activity found!”.

    It seems to be related to the manifest file. If I remove the line:

    I can debug, but that line probably matters? Actually I looked at the google documentation and I’m not even sure its needed, since we still request permission later.

    Anyway, thats a minor annoyance. I can still watch LogCat and watch the output even if I can’t debug.

    All I ever get is:
    09-20 19:36:16.780: DEBUG/FTDI_USB(2644): >========<

    I get that even when a functioning USB mouse is plugged in, and I open the app with that mouse.
    My understanding of the code is that it should list every attached USB device at this stage?

    You run usbman.getDeviceList() and then iterate through those, so it looks like it should see every USB device. Correct?

    There is that filter in the XML file, but google says you *can* filter devices. It wasn't entirely clear if you had to. Because for that, I don't know what to put for version or model. I checked windows device manager with an FTDI device plugged in, went to properties, and looked at all the fields associated with it. I didn't see model or version.

    Should the filter matter? Should the app be showing me all the devices connected even if they don't match the filter or the PID VID we're looking for?

    Thanks,
    -Taylor

    1. Hey Taylor!

      You are correct, all connected devices will be listed.
      If you want to start communicating with your device, you need the adjust the VID_PID constant.

      When the activity starts it should request permission for that device…

      The filter should not matter for this purpose, since it only “autostarts” the activity… Other devices will be listed regardless when you start the activity.
      Does your device show up? Or only your mouse when it’s connected? It should list any device …

      If you are using Linux you can find the debug messages a bit easier using the following cmd:

      adb logcat | grep ">==<"

  2. Hey Manuel,

    Unfortunately, the comment got garbled because of the special characters!

    With the special characters removed, what I tried to say was that I can’t debug the app unless I remove the line:
    action android:name=”android.hardware.usb.action.USB_ACCESSORY_ATTACHED”
    From the intent filter in the XML. Is that line needed? It seems like that it just responsible for starting the app when a device is plugged in, but I don’t need that functionality. I’ll go over the documentation more, but let me know if you think it should work without.

    And the debug messages with the characters removed were:

    09-21 11:01:35.300: DEBUG/FTDI_USB(10274): == enumerating ==
    09-21 11:01:35.300: DEBUG/FTDI_USB(10274): == no more devices found ==

    Thats all. I can see in the code that it should show every device connected, but it doesn’t.

    I also modified the app to print those messages onscreen. But either onscreen or in logcat, that’s all I’m seeing, even when I have a USB mouse connected and working!

    My tablet seems to have the worst time with USB!

    I am going to go over the Android USB documentation and see what I find, but let me know if you know anything.

    Thanks so much!

  3. Okay, the tablet is being really weird. Even with google’s example code I’m not getting any devices showing up.

    Looking further, I’m not the only one with this problem:

    http://markmail.org/message/gq7bdx4e6rd3pqe4

    Looks like I’m going to go out and buy an A500. Company pays for it so not a big deal. Better than messing with this all day. Hopefully I can verify the code works and its just some problem with the Gtab!

  4. Alright, everything is working great on the Acer Iconia Tab A500! Your app works, as well as my test code from the google example.

    And I’m able to send characters out from the FTDI device! Hurrah!

    Unfortunately, none of this works on the Galaxy Tab. It must have some wacky issue.

    Anyway, this almost completely solves my problems! The only thing I gotta sort out is how to receive some data. But it looks like its just a matter of me learning how this stuff works. From a technical point of view I think I’m in the clear!

    For receiving data, I tried this:

    byte[] buffer = new byte[]{0};
    conn.bulkTransfer(epIN, buffer, 1, 100);
    if(buffer[0]!=0)
    l(String.valueOf(buffer));

    But it doesn’t work. Of course I was just guessing. I’ll consult some documentation and see what I am even supposed to be doing. But thanks so much! You’ve been super helpful and I appreciate it!

    -Taylor

  5. Grr… I was typing a comment and it just disappeared. Bah.

    Anyway, I got receiving data working!

    Here’s the code:
    byte[] buffer = new byte[4096];

    if(conn.bulkTransfer(epIN, buffer, 4096, 500)>=0)
    {
    StringBuilder sb = new StringBuilder();
    for(int j = 2;j<4096;j++)
    {
    if(buffer[j]!=0)
    {
    sb.append((char)buffer[j]);
    }
    else
    {
    l(sb.toString());
    break;
    }
    }

    }

    Just paste that after:
    conn.bulkTransfer(epOUT, new byte[]{0}, 1, 0);

    And it will dump anything it receives to the debug window.

    Note it looks like the FTDI defaults to 4096 byte packets, and if you try smaller packets it may drop your data, as it seems to throw the buffer away after you transfer anything from it.

    I found that here: http://www.intra2net.com/en/developer/libftdi/documentation/group__libftdi.html#ga65783703baf0828d519ef597bb2855c1

    Also note the app will stop transferring if the tablet goes asleep. Don't let that trip you up!

    Anyway, I'm really excited. Thank you so much for hanging in here Manuel and helping with this! Crazy to think all this time it was just the Galaxy Tab being difficult.

    If I think of anything else, I will post it here.

    I would like to clean up the code if possible and make a little sample app for other people to use that has simple "Transmit" and "Receive" commands in it. What is the license for your sample code?

    Thanks,
    -Taylor

    1. Hey Taylor!

      I am very glad things worked out for you! It’s just sad that some manufacturers get away with not implementing a certain API stack. Btw. the same goes for the A500 USB gadget support… they just stubbed the API part out…
      I expected some “special” behavior from the FTDI device… Maybe have a look of how “libftdi” approaches the issues to be certain.

      As far as the sample goes you can use it under GPL… Feel free to do whatever you like with it :)
      Now that you say it … some kind of “hterm” app would be pretty neat on tablets for in field debugging :D

      I bet from now on your development will be much smoother :)

      Regards,
      Manuel

    1. Hi David

      We are going to work with a serial interface again in the next few weeks, so I can test the issue too. Until now I did not really send information towards the host… I think the fastest way to find out what’s going on is to look at the libftdi implementation.

      Regards
      Manuel

  6. Interesting work.

    However, I have a *related* question. Is an HID device controllable thru the Android USB Host API? You guys seem to have written a complete driver for the FTDI. Can the same be done for an HID Device?

    1. Hi Earlence

      It is possible to implement your own HID driver with the Android USB Java API. However, the kernel on the tablets usually has already the drivers for them. As soon as you plug in your HID you won’t be able to claim the USB interface since the kernel already did that. If you want to suppress that behavior you would have to “blacklist” a module or build the kernel without the HID driver… Maybe there is also some other way I don’t know about :)

      Regards
      Manuel

  7. Hi Manuel,

    Thanks for the reply.
    I think the kernel HID driver will claim devices with known vendor and product IDs right? I am looking at interfacing this -> http://linuxtv.org/wiki/index.php/Radio_devices#radio-si470x

    and writing a simple HID driver with the Java USB Host API. That way, i’ll be able to have plug and play FM.

    (sorry for converting this into a different thread :) but you guys are quite proficient with this API)

    Cheers,
    Earlence

  8. Hi Manuel,

    Thank you for the nice article. I’m testing the Android 3.2 device with USB loop back.

    Refer to your Android source code, I’d like to make serial communication with USB board.

    In addition to the following setting,

    mConnection.controlTransfer(0×40, 0, 0, 0, null, 0, 0);//reset
    mConnection.controlTransfer(0×40, 0, 1, 0, null, 0, 0);//clear Rx
    mConnection.controlTransfer(0×40, 0, 2, 0, null, 0, 0);//clear Tx
    mConnection.controlTransfer(0×40, 0×03, 0×4138, 0, null, 0, 0);//baudrate 9600

    I’d like to add Databit (8bit), Stopbit (1), and Parity (None). Do you know the request IDs and values of these configuration?
    If you know how to set up these values, please share it with me.
    Thank you for your help in advance,

    Best regards,
    Moo Nam Ko

    1. Hi Moo Nam Ko

      The easiest way to find that out is by looking at the source code of libftdi… Though my best guess is that 8N1 is probably already the default (though only guessing :D ). Hope that helps you to look at the right place.

      Regards
      Manuel

  9. Hi Manuel,

    After posting, I found the answer from http://d.hatena.ne.jp/ksksue/20111103/1320347853#c

    I’d like to share the following information with you and visitors.

    mConnection.controlTransfer(0×40, 0, 0, 0, null, 0, 0);//reset mConnection.controlTransfer(0×40, 0, 1, 0, null, 0, 0);//clear Rx
    mConnection.controlTransfer(0×40, 0, 2, 0, null, 0, 0);//clear Tx
    mConnection.controlTransfer(0×40, 0×02, 0×0000, 0, null, 0, 0);//flow control none
    mConnection.controlTransfer(0×40, 0×03, 0×4138, 0, null, 0, 0);//baudrate 9600
    mConnection.controlTransfer(0×40, 0×04, 0×0008, 0, null, 0, 0);//data bit 8, parity none, stop bit 1, tx off
    //mConnection.controlTransfer(0×40, 0×03, 0×0001, 0, null, 0, 0);//baudrate Highest

    /*
    * * Values to use for a 48MHz FPGA (external crystal to the ft232bm is still 6MHz):
    * Value Baud Rate speed
    * 0×2710 300
    * 0×1388 600
    * 0x09C4 1200
    * 0x04E2 2400
    * 0×0271 4800
    * 0×4138 9600
    * 0x809C 19200
    * 0xC04E 38400
    * 0×0034 57600
    * 0x001A 115200
    * 0x000D 230400
    * 0×4006 460800
    * 0×8003 921600
    */

    Thank you for your reply.

    Best regards,

    Moo Nam Ko

  10. Hi Manuel,

    I have a question about asynchronous data transfer. I hope you have experience with this :)
    In synchronous data transfer, I have sent data to the out endpoint and received the response from the in endpoint using the bulkTransfer method. It works fine with two In and out bulk endpoints in my USB board. I saw you also used bulkTransfer method in your android app.

    According to Android developer site, I have to use the usbRequest class to initialize and queue an asynchronous request, then wait for the result with requestWait() to send data asynchronously. So, I sent a data to the out endpoint using queue and waited the requestWith() on the in endpoint but requestWith() did not received any result. If you have any sample code or resource for asynchronous communication, could share it?

    Best regards,

    Moo Nam Ko

  11. Do you need to have libftdi to make this work or can everything be done in Java?
    I’ve been trying to talk to an Arduino device. Anybody else have gotten something like that to work with this solution?

    Thanks

    1. Hi Leon!

      No libftdi needed. We are currently in the process of writing a post of how to fit pieces for the Arduino together. It is possible talking to the Arduino over serial using just Bulk I/O transfers. If you want to try it just set up the serial config using the transfers stated in our recent post Arduino USB transfers and then use the bulk IN/OUT endpoints of the Arduino USB-Serial converter. Note that the Arduino Uno/Mega do not have FTDI chips equipped anymore.

      All the best
      Manuel

  12. Thanks to everybody for the shared infos! With all those information I’ve been able to write a demo communicating using a binary protocol to talk with electronic locks on a half-duplex serial line.

    1. Hey Salmo

      Good to hear the post and comments helped you. At this point I wanna say thank to everyone posting comments here to help the community :)

      All the best
      Manuel

  13. Hi,

    I have been trying for a while now to access a USB serial port on an Android Tablet with no success. I have three tablets – a scroll running 2.1, a scroll running 2.3.3 and a Novo 7 Paladin running 4.0.1.

    I have an FDTI usb to serial cable that plugs in to the usb port on the tablets.

    Has anyone managed to write a simple java android app that is able to send and recieve data over the usb?

    I have downloaded the app D2XXSample from the FTDI site but it gives errors on all three tablets.

    My aim is to be able to send AT commands to a USB Zigbee dongle

    Any help or advice would be gratefully recieved.

    Thanks,

    Ian James

  14. We should setup a recommended hardware (tablet/mobile) list for developers for USB communications.

    Clearly, Samsung Galaxy is out. Acer A500 is a good choice. There are so many choices in China, ARM11/A8/A9, even MIPS based.

    Maybe a test app is needed.

    1. I agree 100% that is a brilliant idea and of high necessity, especially in the future. An Android App that can be downloaded from the market where USB gets tested and results would automatically be sent to a web application that presents results to everyone.

  15. (\O/) Oops! Finally it works! At first, it has not detected the FTDI chip. I found it might be a bug of VirtualBox, then retry to filter it to guest OS twice.
    Check the logcat out as following:

    ……
    D/FTDI_USB( 1703): >============================================================VirtualBox->Android-x86-v3.2-eeepc (Asus oriented)

    USB Device:
    Arduino 2009 / FTDI 232R / VID:PID=0403:6001
    HTC Tattoo as USB device connected to VirtualBox guest Android OS

    Tested app:
    FTDI_USB.apk (from android.serverbox.ch)
    AdbTest.apk (from andoird.com as demo of USB host API)

    Debug:
    adb logcat

    Thanks for your sharing. I am going to share the lab on my blogs (possibly in Chinese) as well.

    BTW, Android sometimes report fatal error during switch apps between FTDI_USB and others. It seems some exit routines are expected.

  16. Hi Manuel,

    This example worked like a charm in my FT232RL chip! thanks for all the help you provide to us!

    I was wondering what would I have to change to make it work with a Prolific chip…
    I tried this example with different VID_PID, and it indeed detected my Prolific 2303 chipset, but it sends only 2 values, and then it stops without giving me any error int the Android app and in the debugger…

    Any clues on this? Thank you!

  17. Hi Manuel,

    i ‘ve USB cable communication between device and tablet using send & receive message via ascii and device & tablet both are FTDI included, it works fine but i want to send & receive data using binary not ascii, so can you send me some sample code

    1. Hi Vipul,

      On the Android side you receive a byte[], so you can read out binary data. On the Arduino side, you can send over binary data by pushing a char* into the serial buffer, if I remember correctly… check the tech data sheet of your atmega controller to find detailed information of how to access the serial interface.

      Hope that helps you to get started :)

      all the best
      Manuel

  18. Hi Manuel
    I am very new with Android programming, I want to connect my tablet to FTDI module and use serial interface. Can I use your program in Windows and Eclipse. If it is possible I just should change VID_PID value in your program??

    1. Hello Mary,

      Yes you can do all that in Windows with Eclipse. Head to developer.android.com to get started with Android programming. When your workspace is set up, you can follow the sample code to implement your serial communication from your tablet.

      All the best
      Manuel

  19. Hi Manuel,

    I used your app on an ICS tablet and it shows only Hello World …… and nothing after that. it does not recognize devices. The device objects are always null and no iteration in enumeration function happens.

    Thanks,
    Bahram

    1. Hi Bahram,

      This means your device does not support the Android USB Host API. Though it is available as API, he framework part is missing.

      All the best
      Manuel

  20. Hi Manuel
    Thanks for your reply. At first I can use your program with Virtual Box it just need to add usb to your VirtualBox( setting -usb -add) after connecting your device. your program work, it recognize my module and ask for user permission to connect but I dont have any signal on my serial output !!!!!
    I had to remove this line from Manifest file because it cause error and didnt let me to run the project.
    In addition my module is FT 22232H and has 2 serial output I dont know I should use which one??
    Can you help me?

    1. Hey Mary,

      interesting that it works with Virtualbox, that’s really cool :)
      What kind of device are you connecting, an Arduino?
      Maybe the setup routing for your FTDI is slightly different.
      Also, how do you mean it has 2 serial outputs? Do you mean there are two interfaces /dev/ttyACM0 and /dev/ttyACM1 ?

      All the best
      Manuel

  21. Hi Manuel,

    Thanks for reply. Would you please let me know how to fix this frameworks thing.
    Actually my device is rooted and the D2XX sample does not recognize the FTDI usb cable and crashes on data or info clicks. but when I run USB Host Diagnostics app from Chainfire, the D2XX works fine and device is enumerated. It also shows:
    Android API class found
    Kernel support yes

    Regards,
    Bahram

    1. Hello Bahram,

      You cannot fix the framework, the OEM builds it for the device. Unless you flash a custom ROM you cannot use the API. Also, API class found appears probably because the API from the SDK is found, not because the framework supports it (Every Android 3.1 device has the API, only few of them actually have the framework part).

      Hope that clears it up for you.

      All the best
      Manuel

  22. Hi Manuel, I using FT2232H Mini Module that has 2 serial output. I can connect these serial ports to my embeded system. I dont know how can I change the setup. can you introduce document to me for changing the set up??
    mConnection.controlTransfer(0×40, 0, 0, 0, null, 0, 0);//reset
    mConnection.controlTransfer(0×40, 0, 1, 0, null, 0, 0);//clear Rx
    mConnection.controlTransfer(0×40, 0, 2, 0, null, 0, 0);//clear Tx
    mConnection.controlTransfer(0×40, 0×03, 0×4138, 0, null, 0, 0);//baudrate 9600

    How these parameters set?
    According to android developer site :
    connection.controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout)
    how these works as a serial setting?
    thanks
    Mary

  23. Mary,
    I am using the DLP_HS_FPGA3 which has a FT 2232 as the interface. I found I needed to use:
    this.usbInterface = this.device.getInterface(0); // DLP_HS_FPGA3
    as compared to:
    this.usbInterface = this.device.getInterface(1); // Arduino

    Hope this helps

  24. Bob,
    Thanks for your guide, it really help me. Now I have problem with receiving data.
    I use this code but I just receive 0.
    conn.bulkTransfer(epOUT, new byte[]{0}, 1, 0);
    byte[] buffer = new byte[4096];

    if(conn.bulkTransfer(epIN, buffer, 4096, 500)>=0)
    {
    StringBuilder sb = new StringBuilder();
    for(int j = 2;j<4096;j++)
    {
    if(buffer[j]!=0)
    {
    sb.append((char)buffer[j]);
    }
    else
    {
    l(sb.toString());
    break;
    }
    }

    }
    did you do it before?

    1. Hey Mary,

      You should loop conn.bulktransfer and react on events where it returns > 0 and concactenate the buffer. Usually the serial converter returns -1 unless there is data available. Try to poll it every 10 ms or so.

      Hope that helps :-)

      All the best
      Manuel

  25. Mary,
    This is the code I am using to receive a transfer from the fpga:

    ByteBuffer buffer = ByteBuffer.allocate(SIZEOF_RCV_BUF);

    if (request.queue(buffer, SIZEOF_RCV_BUF)) {
    sb.append(“usbRequest.queue ok :”);
    } else {
    sb.append(“usbRequest.queue fail :”);
    }

    request = usbDeviceConnection.requestWait();
    if (request.getEndpoint() == epIn) {
    byte[] data = buffer.array();
    int xfrCount = 0xff & data[receiveBufferOffset];
    CRC_CCITT crc_16 = new CRC_CCITT();
    if (xfrCount > 1 && xfrCount < SIZEOF_RCV_BUF) {
    byte[] scratch = new byte[xfrCount];
    System.arraycopy(data, receiveBufferOffset+1, scratch, 0, xfrCount);
    int crc = crc_16.computeCRC(scratch);
    if (crc == 0) {
    sb.append("\n" + xfrCount + " bytes received, crc valid:transfer successful!:");
    } else {
    sb.append("\ncrc invalid, transfer failed!:");
    sb.append("transfer count " + xfrCount + ":");
    for (int i=0; i<Math.min(xfrCount+1+FPGA_RCV_BUF_OFFSET, 16); i++) {
    sb.append("data " + Integer.toHexString(0xff & data[i]) + ":");
    }
    }
    } else {
    sb.append("bad transfer count " + xfrCount + ":");
    for (int i=0; i<8; i++) {
    sb.append("data " + Integer.toHexString(0xff & data[i]) + ":");
    }
    }
    }

    Note that the transfer count must be included with the data.

    Bob

  26. Hi Bob
    Thanks for your help, I use eclipse on windows and your code have some error in my application. Can you guide me? What is the “SIZEOF_RCV_BUF”, as I understand you use Asynchronous receiving and save your data on buffer and the size of buffer is
    SIZEOF_RCV_BUF ??? Beside this I use usb in host mode and I don,t know how to use Usbrequest. finally I changed your code like this:
    ByteBuffer buffer = ByteBuffer.allocate(SIZEOF_RCV_BUF);
    UsbRequest request = conn.requestWait();
    StringBuilder sb = new StringBuilder();
    if (request.queue(buffer, SIZEOF_RCV_BUF)) {
    sb.append(“usbRequest.queue ok :”);
    } else {
    sb.append(“usbRequest.queue fail :”);
    }

    if (request.getEndpoint() == epIN) {
    byte[] data = buffer.array();
    int xfrCount = 0xff & data[receiveBufferOffset];
    CRC_CCITT crc_16 = new CRC_CCITT();
    if (xfrCount > 1 && xfrCount < SIZEOF_RCV_BUF) {
    byte[] scratch = new byte[xfrCount];
    System.arraycopy(data, e+1, scratch, 0, xfrCount);
    int crc = crc_16.computeCRC(scratch);
    if (crc == 0) {
    sb.append("\n" + xfrCount + " bytes received, crc valid:transfer successful!:");
    } else {
    sb.append("\ncrc invalid, transfer failed!:");
    sb.append("transfer count " + xfrCount + ":");
    for (int i=0; i<Math.min(xfrCount+1+FPGA_RCV_BUF_OFFSET, 16); i++) {
    sb.append("data " + Integer.toHexString(0xff & data[i]) + ":");
    }
    }
    } else {
    sb.append("bad transfer count " + xfrCount + ":");
    for (int i=0; i<8; i++) {
    sb.append("data " + Integer.toHexString(0xff & data[i]) + ":");
    }
    }
    }
    I have error in SIZEOF_RCV_BUF, receiveBufferOffset, CRC_CCITT.
    How can I fix them?
    thanks,Marry.

  27. Hi Bob,
    I also met the same problem.
    my code like this:
    conn.bulkTransfer(epOUT, 0xFE, 1, 0);
    byte[] buffer = new byte[4096];
    StringBuilder str = new StringBuilder();
    boolean flag = true;
    while(flag){
    if (conn.bulkTransfer(epIN, buffer, 4096, 500) > 0) {
    for (int i = 0; i 0){
    flag = false;
    }

    }
    }
    Sometimes I could receive the whole buffer, but sometimes I didn’t. Is it about baudrate? How do i set the patameters?
    Thanks for help.

  28. Hi Mary,

    I use 256 bytes as the size of my receive buffer; set the receive buffer offset to zero for a start, and forget the crc calculation for now. My buffer from the fpga has a transfer count, data, and crc. In my case, the FT2232 converts the serial stream to byte wide data for the fpga. I think FTDI has a program for a pc that I used to verify this (FT245 mode, if I remember). The transfer to the fpga is asynchronous and I have not been able to make any sense, or need for, the control parameters. Good luck!

    Bob

  29. Hi Bob, thanks for your guide.
    this is my new code:
    UsbRequest request = new UsbRequest();
    ByteBuffer buffer1 = ByteBuffer.allocate(256);//SIZEOF_RCV_BUF

    StringBuilder sb = new StringBuilder();

    if (request.queue(buffer1,SIZEOF_RCV_BUF )) {
    sb.append(“usbRequest.queue ok :”);
    } else {
    sb.append(“usbRequest.queue fail :”);
    }
    //System.out.println(sb.toString());
    request = conn.requestWait();
    if (request.getEndpoint() == epIN) {
    byte[] data = buffer1.array();
    }

    When I run or debug it I have error that say:
    FATAL EXCEPTION: Thread-9
    java.lang.NullPointerException
    at Android. hardware.usb.usbrequest.queue
    what is this mean?
    Thanks, Marry

  30. Mary,
    One difference between our code is:
    UsbRequest request = new UsbRequest();
    if (request.initialize(usbDeviceConnection, epIn)) {
    sb.append(“usbRequest.initialize ok :”);
    } else {
    sb.append(“usbRequest.initialize fail :”);
    }

    ByteBuffer buffer = ByteBuffer.allocate(SIZEOF_RCV_BUF);
    I don’t know if that is the problem, but it is worth looking at.

  31. Thanks every one for helping me,I can send and receive data correctly :)
    I want to show my buffer on Android screen instead of log file and I dont want to make another Activity.
    How can I do this? I use Intent but It doesn,t work.
    Thanks, Marry

    1. Hello Mary,

      Please can you tell me the procedure on how to see the buffer data in the log file? I am able to list the devices connected to my device which is also acting as USB Host. I am running this app using Android adb. After the device (XStick in my case) is detected what are commands I need to write in adb to read the data from my XStick and how to view it in the log file? It will be very helpful if you can explain it to me.

      Many thanks in advance.

      NKP

  32. Hi,

    I am an Android novice.
    Do I need root permission to use LibUSB?
    I am using Prolific PL2303 USB to Serial cable.

    Thanks.

  33. hello sir ,
    I am making a project where I’m communicating from my android device to adruino (DUEMILANOVE ATMega 328) .Is it possible or do i need an Arduino UNO.
    I tried using the code you provided in the post however of no avail(I even changed the PID,VID as per the board).

  34. Hello Manuel,
    Many thanks for such a usfull information.
    I am facing some problems. I was trying to read XBEE module which I have connected to BeaglboardXM which is running on Pre-built android ICS OS. From you code, to read the usb device first I step is to enumrate the USB devices connected to it. but some how UsbDevice.getDeviceList () provides me empty list eventhough I have connected mouse-keyboard and XBEE module to board.
    I am not sure where is the problem. Is this the problem with the framework? but IMO the pre-built os is distributed by TI and it has required API’s UsbDevices, UsbManager etc.

    do you have any inputs on this issue. I am stuck at this stage for a long time now. :(
    thnaks!!!

    1. Hi Sheryas,

      If you get an empty list of USB devices back that means that some parts of the Android Framework have not been implemented correctly. You should try another build for the Beagleboard XM. Which one are you using currently?

      All the best
      Manuel

  35. Hi Manuel,

    Thanks for sharing the code.
    I tried to test you program on my HTC One X to connect with a FTDI-based sensor gateway via a Micro-to-Mini OTG cable (like this one http://www.amazon.com/Wilson-Electronics-Micro-Charging-Adapter/dp/B003NQ0QES/ref=sr_1_3?ie=UTF8&qid=1349738021&sr=8-3&keywords=micro+to+mini+usb). But the phone seems to be not able to recognize the device and lists out nothing. Do you know what are the possible reasons behind this ?
    Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>