Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android to PC USB Read/Write

I have a program on PC taking in string input from USB ( old program) I have a Android (4.X) tablet which needs to provide string input on USB to the program running on PC. When I used the sample code on Android, following code gives empty hashmap. The PC( tried on 32 bit XP and 64 bit Windows 7) has Android driver.

    mManager = (UsbManager)getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> devices =  mManager.getDeviceList();

Any real working code example talking to PC over USB will help, pl. also point out if any driver etc. needed on Android to talk to PC.

I have tried both the Accessory mode and the host mode( just in case)

like image 667
Sawant Avatar asked Dec 30 '12 23:12

Sawant


1 Answers

I'm not sure I exactly follow what you're doing here, but if I understand you correctly - it just won't work this way. The UsbManager.getDeviceList() is meant to be used with Android devices with USB host port, to which some USB devices are connected. But, as far as I understand, you connect Android tablet acting as a device to your PC acting as a host (I guess so, cause you wrote about driver installation).

If you want to communicate between Android USB device and some USB host (e.g. because your Android device has no USB host capabilities), you need to use accessory mode (I suggest you start with this Android Developers Blog post). But this mode requires special support on the USB host side (it must talk to the device with Android Open Accessory Protocol). Note, that getDeviceList() makes no sense in Accessory mode - first of all, connected accessory is a USB host, not a USB device, and there can be only one USB host on a USB bus.

If you want to communicate with PC using Accessory mode, you may want to try this AOAP implementation for PC. If all you need is to talk to Android device for some debugging needs, you may want to use ADB port forwarding and TCP connection instead.

like image 119
Code Painters Avatar answered Oct 23 '22 20:10

Code Painters