Tuesday, February 18, 2014

USB Host exercise: get type and direction of UsbEndpoint

TO get type of a Endpoint, call its getType() method. The return int will be USB_ENDPOINT_XFER_CONTROL(endpoint zero), USB_ENDPOINT_XFER_ISOC(isochronous endpoint), USB_ENDPOINT_XFER_BULK(bulk endpoint) or USB_ENDPOINT_XFER_INT(interrupt endpoint).

To get direction of the Endpoiny, call its getDirection() method. It return USB_DIR_OUT(host to device), or USB_DIR_IN(device to host).

Update the last exercise of USB Host Mode "Use intent filter to detect a specified USB device and auto start application", modify endpointOnItemSelectedListener in MainActivity.java to read type and direction.

 OnItemSelectedListener endpointOnItemSelectedListener = 
   new OnItemSelectedListener(){

  @Override
  public void onItemSelected(AdapterView<?> parent, 
    View view, int position, long id) {
   
   UsbEndpoint selectedEndpoint = listUsbEndpoint.get(position);
   
   String sEndpoint = "\n" + selectedEndpoint.toString() + "\n"
    + translateEndpointType(selectedEndpoint.getType());
   
   //Read Endpoint direction 
   String sEndpointDir = "";
   switch(selectedEndpoint.getDirection()){
   case UsbConstants.USB_DIR_OUT:
    sEndpointDir = "USB_DIR_OUT";
    break;
   case UsbConstants.USB_DIR_IN:
    sEndpointDir = "USB_DIR_IN";
    break;
   default:
    sEndpointDir = "unknown!";
   }
   
   textEndPoint.setText(sEndpoint + "\n" + sEndpointDir);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {}
  
 };


Tested with Arduino Esplora connected:
- There are 3 UsbInterface.
- Both the 1st and 3rd UsbInterface have one Endpoint, of type USB_ENDPOINT_XFER_INT (interrupt endpoint), with direction of USB_DIR_IN.
- The 2nd UsbInterface have two Endpoint, both have type of USB_ENDPOINT_XFER_BULK (bulk endpoint), the 1st one have direction of USB_DIR_OUT, and the 2nd one have type of USB_DIR_IN. It will be used send and receive data in future exercise.





download filesDownload the files.



Step-by-step: Android USB Host Mode programming

No comments: