3

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?

JuJoDi
  • 14,627
  • 23
  • 80
  • 126
  • 1
    ACTION_USB_ACCESSORY_ATTACHED is a bit wonky -- check out this answer: https://stackoverflow.com/questions/6981736/android-3-1-usb-host-broadcastreceiver-does-not-receive-usb-device-attached/9814826#9814826 – theicfire Dec 19 '18 at 22:07

0 Answers0