Forum Xamarin.Android

Android Open Accessory Mode - Help!

I'm developing an Android app in Xamarin using AOA for communication with a single sensor. Basics are this:

  • Plug in the sensor to the tablet. Sensor puts tablet in open accessory mode and launches the app.
  • App main activity launches a background service with a single thread for comm.
  • The thread's Run() method is a basic while loop that calls InputStream.Read() on the sensor.
  • Comm is sampling at 10 Hz, roughly 60 bytes per sample, with the app always initiating the transaction (i.e. Write then Read).

Problem #1:

  • After about a minute, the Android GC kicks in and the OutputStream.Write() returns an error of "device not available". Subsequent InputStream.Read() operations fail too.

Problem #2:

Problem #3:

  • Xamarin does not seem to allow me to attach the debugger to a running process over WiFi. I need this since the USB port is being used obviously for AOA. Am I missing something here, or does Xamarin just not support this?

Anyone out there tackled this beast and won, or battled something similar? Any help is appreciated.



  • MikeIndyMikeIndy USMember

    I'm currently running into a similar issue. Did you have any success or get feedback on how to address the problem?

  • SteveMolbachSteveMolbach USMember ✭✭

    Problem #1 was a coding issue on my part. I was banging the USB bus with Write commands while the Read was blocking.
    Problem #2 is still an issue marked as Defect - Medium.
    Problem #3 is solvable.

    Which one are you having trouble with?

  • MikeIndyMikeIndy USMember

    Problem #1 is the one I'm currently struggling with. I was seeing the issue when trying to write the same time a read was taking place, but now that is all serialized, i.e. I am no longer writing data to the device from a separate thread, it gets written to a memory buffer, then sent out to the device synchronously.

    Now, however, when I send a message to my UI thread to initiate an update to the info on the UI screen, it will work for awhile until garbage collection runs at the right time and then I get the error for the reading thread that the read failed: ENODEV (No such device).

    As long as I don't try to pass data to the UI thread, it appears to run great...

  • p.bielinskip.bielinski USMember

    @SteveMolbach how do you resolve problem #3?

  • SteveMolbachSteveMolbach USMember ✭✭

    @p.bielinski, glad you found it.

    @MikeIndy I had the same issues with GC until I put my communication thread inside a service.

  • HemantaHemanta INMember ✭✭

    Hi All, I am facing similar Issue, I am trying to Read/Write Data to Arduino in Xamarin. But my Application Crashes instantly. Request Permission pop up is coming. Once I click OK to allow, Its crashes.

    Could anyone help me out. I am able to read/write data in Native Android. But problem is in Xamarin.Android.

    Any help is appreciated.

  • SteveMolbachSteveMolbach USMember ✭✭

    @Hemanta, is the problem in your comm thread or when you launch the app? Assuming you have a separate comm thread, what happens when you remove the calls to read/write and just let the thread run?

  • HemantaHemanta INMember ✭✭

    @SteveMolbach Hi Steve, I solved this crash issue. Now I will try to read/read data from Arduino. The problem was like I was not able to get logs as my android device was connected to Arduino. Somehow i pulled logs from device and found that crash was due to System.NullReferenceException in mUsbManager while trying to open accessory like mFileDescriptor = mUsbManager.OpenAccessory(accessory). It got solved now. Thanks a lot.

    I will check Read/Write part to/from Arduino.

Sign In or Register to comment.