Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Android USB Host API to read my USB game controller/Or other USB device data
    primarykey
    data
    text
    <p>I am trying to use Android USB Host API to read my USB game controller data, once I get this to work, I will connect other device to test. My game controller is connected to my Android phone using OTG cable. I am now able to get device, endpoints information, but I don't know how to read the raw data and display it. </p> <p>Can someone please help me? Some example codes will be appreciated.</p> <pre><code>TextView countDisplay; ArrayList&lt;String&gt; listItems = new ArrayList&lt;String&gt;(); ArrayAdapter&lt;String&gt; adapter; String values = ""; UsbManager mManager; UsbDevice device = null; private byte[] bytes; private static int TIMEOUT = 0; private boolean forceClaim = true; static PendingIntent mPermissionIntent; UsbDeviceConnection connection = null; UsbEndpoint InputEndpoint = null; UsbEndpoint OutputEndpoint = null; private Handler mHandler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mManager = (UsbManager) getSystemService(Context.USB_SERVICE); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent( "com.android.example.USB_PERMISSION"), 0); IntentFilter filter = new IntentFilter(); filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); registerReceiver(mUsbReceiver, filter); HashMap&lt;String, UsbDevice&gt; deviceList = mManager.getDeviceList(); values = values + "deviceListSize:" + deviceList.size() + ",tostring:" + deviceList.toString(); Iterator&lt;UsbDevice&gt; deviceIterator = deviceList.values().iterator(); while (deviceIterator.hasNext()) { device = deviceIterator.next(); values = values + ",device id:" + device.getDeviceId() + ",device name:" + device.getDeviceName(); values = values + ",Protocol:" + device.getDeviceProtocol() + ",ProductId:" + device.getProductId(); values = values + ",DeviceClass:" + device.getDeviceClass() + ",VendorId:" + device.getVendorId(); } if (device != null) { values = values + ",getInterfaceCount:" + device.getInterfaceCount(); UsbInterface intf = device.getInterface(0); values = values + ",intf.getEndpointCount():" + intf.getEndpointCount(); UsbEndpoint endpoint1 = intf.getEndpoint(0); UsbEndpoint endpoint2 = intf.getEndpoint(1); mManager.requestPermission(device, mPermissionIntent); if (mManager.hasPermission(device)) { values = values + ",has permission over device!"; connection = mManager.openDevice(device); if (connection == null) { values = values + ",connection null"; } else { values = values + ",getFileDescriptor:" + connection.getFileDescriptor(); if (endpoint1.getDirection() == UsbConstants.USB_DIR_IN) { InputEndpoint = endpoint1; } else { OutputEndpoint = endpoint1; } if (endpoint2.getDirection() == UsbConstants.USB_DIR_IN) { InputEndpoint = endpoint2; } else { OutputEndpoint = endpoint2; } } if (InputEndpoint == null) { countDisplay.setText(values + ",InputEndpoint is null"); } if (OutputEndpoint == null) { countDisplay.setText(values + ",OutputEndPoint is null"); } connection.claimInterface(intf, forceClaim); mHandler.postDelayed(runnable, 1); } else { values = values + ",Do not have permission over device!"; } } setContentView(R.layout.activity_main); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.activity_main, null); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); int counter = 1; countDisplay = new TextView(this); ll.addView(countDisplay); countDisplay.setText(values + ",counter here"); final Button button = new Button(this); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (device != null &amp;&amp; mManager.hasPermission(device)) { values = values + ",device id:" + device.getDeviceId() + ",device name:" + device.getDeviceName(); values = values + ",Protocol:" + device.getDeviceProtocol() + ",ProductId:" + device.getProductId(); values = values + ",DeviceClass:" + device.getDeviceClass() + ",VendorId:" + device.getVendorId(); countDisplay.setText(values + ",okok"); } else { if (device != null) mManager.requestPermission(device, mPermissionIntent); } } }); ll.addView(button); setContentView(ll); } </code></pre> <p>And Runnable:</p> <pre><code>private Runnable runnable = new Runnable() { public void run() { if (connection != null) { int count = connection.bulkTransfer(InputEndpoint, bytes, bytes.length, TIMEOUT); countDisplay.setText(values + ",bultTransferNo:" + count); countDisplay.setText(values + "bulkValue:" + bytes); } else { countDisplay.setText(values + ",connection is null"); } } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload