Bug 17523 - unable to capture the - android.hardware.usb.action.USB_ACCESSORY_ATTACHED event in the BroadcastReceiver
Summary: unable to capture the - android.hardware.usb.action.USB_ACCESSORY_ATTACHED ev...
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.10.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-01-30 17:36 UTC by Jon Goldberger [MSFT]
Modified: 2017-06-27 17:29 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project (10.23 KB, application/zip)
2014-01-30 17:36 UTC, Jon Goldberger [MSFT]
Details
Xamarin version information (915 bytes, text/plain)
2014-01-30 17:38 UTC, Jon Goldberger [MSFT]
Details
logcat from device (177.93 KB, text/plain)
2014-01-30 17:39 UTC, Jon Goldberger [MSFT]
Details
Test Project with suggested changes (18.86 KB, application/zip)
2014-02-05 16:09 UTC, Jon Goldberger [MSFT]
Details
Updated MainActivity.cs file with suggested changes. (3.00 KB, application/octet-stream)
2014-02-10 15:04 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Comment 1 Jon Goldberger [MSFT] 2014-01-30 17:36:36 UTC
With the attached code I am able to capture the - android.hardware.usb.action.USB_DEVICE_DETACHED event in the BroadcastReceiver.
However I am unable to capture the - android.hardware.usb.action.USB_ACCESSORY_ATTACHED event in the BroadcastReceiver.
Comment 2 Jon Goldberger [MSFT] 2014-01-30 17:38:05 UTC
From case 59063 (as is comment #1):
Here is my setup –
AndroidMini PC -->Usb to Serial Bridge -->Serial output (third party device)

I have built a (usb to) serial driver in Java. I have also created a C# binding for the Java driver.
Everything associated with reading from the device using this binding works just fine.
Comment 3 Jon Goldberger [MSFT] 2014-01-30 17:38:52 UTC
Created attachment 5952 [details]
Xamarin version information
Comment 4 Jon Goldberger [MSFT] 2014-01-30 17:39:29 UTC
Created attachment 5954 [details]
logcat from device
Comment 5 Jon Goldberger [MSFT] 2014-01-30 19:03:18 UTC
From case:
One more piece of information –
http://code.google.com/p/android/issues/detail?id=25703
It looks like there might be an issue with older version of Android when the actions are registered programmatically.
I would still expect it to work when the actions are registered via the Manifest file.

My Android device is at version 4.0.4.
Comment 6 Jon Goldberger [MSFT] 2014-02-03 12:30:34 UTC
Based on an SO thread, this seems to be the way Android works, that a USB_ACCESSORY_ATTACHED event can not be caught in a broadcast receiver. See SO thread: 
http://stackoverflow.com/questions/11098332/usb-accessory-catch-usb-accessory-attached-by-intent-filter-of-service
Comment 7 Jon Goldberger [MSFT] 2014-02-03 13:16:28 UTC
Or maybe not:
From the case:
"I have tried to capture the event in the OnResume of my Intent that does not work either.

Also please do note that I am able to get all of this work just fine using Java code on the same android device."
Comment 8 Jonathan Pryor 2014-02-04 15:47:48 UTC
@Goldberger: Could we get a copy of his Java code so we can double-check his port?
Comment 9 Jon Goldberger [MSFT] 2014-02-04 16:00:04 UTC
@Jonathan Pryor: I have sent a request for the Java code.
Comment 11 Jonathan Pryor 2014-02-04 17:18:33 UTC
@Goldberger: Thank you. The most obvious difference is that he's using AndroidManifest.xml "improperly":

http://docs.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/

In general, you should use C# custom attributes for <activity/>/etc. instead of using AndroidManifest.xml, lest "duplicate declarations" bite.

Which is precisely what's (probably) happening here; Properties\AndroidManifest.xml contains:

    <activity
    	android:name=".AndroidSerialTest"
    	android:launchMode="singleTask">

i.e. it doesn't use a fully-qualified type name, which is a big "no no" in Xamarin.Android development. As a result, the generated obj\Debug\android\AndroidManifest.xml will have _two_ <activity/> elements, one for ".AndroidSerialTest", and one for androidserialtest.MainActivity.

What should instead be done is _most_ of Properties\AndroidManifest.xml should be removed:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="AndroidSerialTest.AndroidSerialTest">
    <uses-sdk android:minSdkVersion="13" />
    <uses-feature android:name="android.hardware.usb.host" />
</manifest>

Then C# custom attributes should be used on MainActivity, akin to (untested):

    [Activity (Label = "AndroidSerialTest",
            MainLauncher = true,
            LaunchMode=LaunchMode.SingleTask,
            ConfigurationChanges=ConfigChanges.Orientation)]
    [IntentFilter (new[]{"android.hardware.usb.action.USB_DEVICE_ATTACHED"})]
    [MetaData ("android.hardware.usb.action.USB_DEVICE_ATTACHED", Resource="@xml/device_filter")]
    public partial class MainActivity {}

This should allow AndroidManifest.xml to have the correct <activity/> contents, which in turn should allow the sample to work as intended.
Comment 12 Jon Goldberger [MSFT] 2014-02-05 16:09:50 UTC
Created attachment 5982 [details]
Test Project with suggested changes
Comment 13 Jon Goldberger [MSFT] 2014-02-05 16:11:58 UTC
The customer tried the suggested changes and reports that they still can not capture the USB_DEVICE_ATTACHED event. I have posted a new test project with updated code.
Comment 14 Jonathan Pryor 2014-02-05 16:55:13 UTC
@Goldberger: As a guess, they should remove the '+' in their @+xml/devicefilter use, e.g. replace this:

	[MetaData ("android.hardware.usb.action.USB_DEVICE_DETACHED", Resource = "@+xml/devicefilter")]

with:

	[MetaData ("android.hardware.usb.action.USB_DEVICE_DETACHED", Resource = "@xml/devicefilter")]

I don't think that AndroidManifest.xml can contain "@+xml/..." syntax, which was appearing in the generated AndroidManifest.xml:

      <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@+xml/devicefilter" />
Comment 15 Jon Goldberger [MSFT] 2014-02-10 15:03:28 UTC
Response from customer:
When I remove the “+”, the code does not compile.
I get the following errors –
Error: No resource found that matches the given name (at 'resource' with value '@xml/devicefilter'). (AndroidSerialTest)
Error: No resource found that matches the given name (at 'resource' with value '@xml/devicefilter'). (AndroidSerialTest)

Please find attached the code with the suggested changes.
Comment 16 Jon Goldberger [MSFT] 2014-02-10 15:04:16 UTC
Created attachment 6018 [details]
Updated MainActivity.cs file with suggested changes.
Comment 17 Jonathan Pryor 2014-02-10 15:46:56 UTC
> When I remove the “+”, the code does not compile. I get the following errors –
>   Error: No resource found that matches the given name (at 'resource' with value '@xml/devicefilter').

I cannot repro that build error. Taking Attachment #5982 [details] and replacing MainActivity.cs with Attachment #6018 [details] builds for me without error (and without any warnings).

My guess for the above error message is that the project is lacking a Resources\xml\devicefilter.xml file with a Build action of AndroidResource.
Comment 18 Chris Hardy [MSFT] 2017-06-27 17:29:22 UTC
Unfortunately, we’re unable to reproduce this report. If this issue is still occurring for you, please reopen this issue and attach a reproduction to the bug by starting with a clean Xamarin.Android project adding just the code necessary to demonstrate the issue.