יום שני, 3 ביוני 2013

Getting USB device Interfaces

 

The following code is part of the MissileLauncherActivity of the MissileLauncher api Demo.
The onResume method
The onResume method may be called due to intend invocation caused by the USB device plugin or disconnecting.
The method retrieve the UsbDevice object from the calling intend data. the USBDevice Repents a connected USB device and contains methods to access its identifying information, interfaces, and endpoints. .

  1:     @Override
  2:     public void onResume() {
  3:         super.onResume();
  4:         mSensorManager.registerListener(mGravityListener, mGravitySensor,
  5:                 SensorManager.SENSOR_DELAY_NORMAL);
  6: 
  7:         Intent intent = getIntent();
  8:         Log.d(TAG, "intent: " + intent);
  9:         String action = intent.getAction();
 10: 
 11:         UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
 12:         if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
 13:             setDevice(device);
 14:         } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
 15:             if (mDevice != null && mDevice.equals(device)) {
 16:                 setDevice(null);
 17:             }
 18:         }
 19:     }

The setDevice method
The setDevice method tries to pitch the USB interface out of the usbdevice and tries to fetch the USB interface type interrupt  out of this endpoint.
The method tries to open connection to the device using the UsbManager.

  1:  private void setDevice(UsbDevice device) {
  2:         Log.d(TAG, "setDevice " + device);
  3:         if (device.getInterfaceCount() != 1) {
  4:             Log.e(TAG, "could not find interface");
  5:             return;
  6:         }
  7:         UsbInterface intf = device.getInterface(0);
  8:         // device should have one endpoint
  9:         if (intf.getEndpointCount() != 1) {
 10:             Log.e(TAG, "could not find endpoint");
 11:             return;
 12:         }
 13:         // endpoint should be of type interrupt
 14:         UsbEndpoint ep = intf.getEndpoint(0);
 15:         if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
 16:             Log.e(TAG, "endpoint is not interrupt type");
 17:             return;
 18:         }
 19:         mDevice = device;
 20:         mEndpointIntr = ep;
 21:         if (device != null) {
 22:             UsbDeviceConnection connection = mUsbManager.openDevice(device);
 23:             if (connection != null && connection.claimInterface(intf, true)) {
 24:                 Log.d(TAG, "open SUCCESS");
 25:                 mConnection = connection;
 26:                 Thread thread = new Thread(this);
 27:                 thread.start();
 28: 
 29:             } else {
 30:                 Log.d(TAG, "open FAIL");
 31:                 mConnection = null;
 32:             }
 33:          }
 34:     }

Sources
http://developer.android.com/guide/topics/connectivity/usb/host.html

אין תגובות:

הוסף רשומת תגובה