I have my App's manifest set to use the com.android.future.usb.accessory library and filter for USB_ACCESSORY_ATTACHED:
<application
...
<uses-library android:name="com.android.future.usb.accessory" />
<activity
android:name="com.example.usb.MainActivity"
...
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</activity>
...
</application>
I then receive the intent in my MainActivity via
protected void onNewIntent(Intent intent) {
String action = intent.getAction();
Log.d(TAG, "New Intent Action " + action);
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
Toast.makeText(getBaseContext(), "Device Attached", Toast.LENGTH_LONG).show();
}
super.onNewIntent(intent);
}
For some reason I only receive this intent if the application I'd like to connect to is not running on the other device. Does anybody know why, or have a workaround?