יום שבת, 1 ביוני 2013

Controlling the USB FX device from Android tablet

In order to control a usb device I used the USB host api I started to learn the topic using the android missilelauncher demo .

Changing the device_filter.xml file to describe the USB FX2 device.

  1: <resources>
  2:     <usb-device vendor-id="1351" product-id="4098" />
  3: </resources>
  4: 

I want to get a callback notification when  the USB FX 2 device get connected to the android device.


Add to the manifest


  1: <intent-filter>
  2:   <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
  3: </intent-filter>
  4: <intent-filter>
  5:  <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
  6: </intent-filter>
  7: <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
  8:     android:resource="@xml/device_filter" />
  9: <meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
 10:     android:resource="@xml/device_filter" />

Register the broadcast activities in the onCreate method


  1: //register for USBFX2 attachment
  2: IntentFilter attachedFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);     
  3: IntentFilter detachedFilter = new IntentFilter(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
  4: registerReceiver(mUsbFX2AttachedReceiver, attachedFilter);
  5: registerReceiver(mUsbFX2AttachedReceiver, detachedFilter);

declaring the BroadcastReceiver for the attached and detached events:


  1: private final BroadcastReceiver mUsbFX2AttachedReceiver = new BroadcastReceiver()
  2:     {
  3:     @Override
  4:     public void onReceive(Context context, Intent intent)
  5:         {
  6:        String action = intent.getAction();
  7:       
  8:          if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
  9:         Toast.makeText(context, "UsbFX2 has connected", Toast.LENGTH_SHORT).show();                  
 10:          }
 11:          if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
 12:            Toast.makeText(context, "UsbFX2 has detached", Toast.LENGTH_SHORT).show();                  
 13:          }
 14:         }
 15:     };

Sources:


http://stackoverflow.com/questions/11191835/receive-intent-action-usb-device-attached-through-code
http://www.ezequielaceto.com.ar/techblog/?p=396 
http://developer.android.com/guide/topics/connectivity/usb/host.html
http://stackoverflow.com/questions/11638216/how-to-make-an-basic-android-usb-host-application

אין תגובות:

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