Android USB host with HID communication

Source: Internet
Author: User

Front-end time to tinker with a HID hardware, need and Android communication, online search a lap, harvest is not small.

The better articles are:
Android USB host with HID communication

Android service creates USB host communication

Where the code is to be noted in some places, please note:

/*** USB HOST connection HID *@authorIVAN **/ Public classMainactivityextendsActivity {Private Static FinalString TAG = "Usb_host"; PrivateUsbmanager Myusbmanager; PrivateUsbdevice Myusbdevice; PrivateUsbinterface MyInterface; Privateusbdeviceconnection mydeviceconnection; Private Final intVendorID = 8457; //change to your hardware ID here Private Final intProductID = 30264; PrivateTextView Info; PrivateUsbendpoint epout; PrivateUsbendpoint ePIN; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);         Setcontentview (R.layout.main); Info=(TextView) Findviewbyid (r.id.info); //Get UsbmanagerMyusbmanager =(Usbmanager) Getsystemservice (Usb_service);         Enumeratedevice ();         Findinterface ();         Opendevice ();     Assignendpoint (); }     /*** Assign endpoints, in | Out, that is, input and output; Here I use 1 for out end, 0 in, and of course you can judge*/

//usb_endpoint_xfer_bulk
/*
#define USB_ENDPOINT_XFER_CONTROL 0--control transmission
#define USB_ENDPOINT_XFER_ISOC 1--equal-time transmission
#define Usb_endpoint_xfer_bulk 2--Block transfer
#define USB_ENDPOINT_XFER_INT 3--Interrupt transmission
* */

    Private void Assignendpoint () {

if (MyInterface! = null) {// This sentence is easy to error and causes a lot of people to ask in the major forums: Why the error?

the code here is replaced by a bit of your own hardware properties to judge it

for (int i = 0; i < Myinterface.getendpointcount (); i++) {

Usbendpoint EP = Myinterface.getendpoint (i);

if (ep.gettype () = = Usbconstants.usb_endpoint_xfer_int) {
if (ep.getdirection () = = Usbconstants.usb_dir_out) {
Epout = EP;

} else {

ePIN = EP;

}
}
}
}

log.d (TAG, getString (R.string.text)); }     /*** Turn on the device*/    Private voidOpendevice () {if(MyInterface! =NULL) {Usbdeviceconnection conn=NULL; //before open to determine whether there is a connection permission, for connection permissions can be statically assigned, you can also dynamically assign permissions, you can check the relevant data            if(Myusbmanager.haspermission (Myusbdevice)) {conn=Myusbmanager.opendevice (Myusbdevice); }             if(conn = =NULL) {                return; }             if(Conn.claiminterface (MyInterface,true) ) {mydeviceconnection= conn;//to this your Android device is already connected to the HID deviceLOG.D (TAG, "Turn on device Success"); } Else{conn.close (); }        }    }     /*** Find Device Interface*/    Private voidFindinterface () {if(Myusbdevice! =NULL) {log.d (TAG,"Interfacecounts:" +Myusbdevice.getinterfacecount ());  for(inti = 0; I < Myusbdevice.getinterfacecount (); i++) {usbinterface intf=Myusbdevice.getinterface (i); //make some judgments based on the devices in your hand, but this information can be printed when you enumerate to the device .                if(Intf.getinterfaceclass () = = 8 && intf.getinterfacesubclass () = = 6 & amp;& Intf.getinterfaceprotocol () = = 80) {MyInterface=intf; LOG.D (TAG,"Find My Device Interface"); }                 Break; }        }    }     /*** Enumerate Devices*/    Private voidEnumeratedevice () {if(Myusbmanager = =NULL)            return; HashMap<string, usbdevice> devicelist =myusbmanager.getdevicelist (); if(!devicelist.isempty ()) {//devicelist is not emptyStringBuffer SB =NewStringBuffer ();  for(Usbdevice device:deviceList.values ()) {Sb.append (device.tostring ()); Sb.append ("\ n");                Info.settext (SB); //Output Device InformationLOG.D (TAG, "deviceinfo:" + device.getvendorid () + "," +Device.getproductid ()); //Enumerating to Devices                if(Device.getvendorid () = =VendorID&& Device.getproductid () = =ProductID) {Myusbdevice=device; LOG.D (TAG,"Enumerate device Success"); } }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

Get the code for the data:

ncount = Mydeviceconnection.bulktransfer (epin, buffer, buffer.length,                              100);

Note the length of the data read here, which is very important for some hardware, some hardware is small or big immediately crash restart, or always return-1

The length of this data needs to be based on the hardware properties,

One way is to get through int inmax = Epin.getmaxpacketsize ();

Another way is to detect the device's data length through the HID program under Windows;

As for the problem of not reading hardware I think the following methods will not help, or hardware does not support, android4.0 above seems to contain the following file information

Place the Android.hardware.usb.host.xml file under/system/etc/permissions, and the second is to add a sentence to the handheld_core_hardware.xml in the same directory < Feature name= "Android.hardware.usb.host" >

Android USB host with HID communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.