Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

usb devices android, c# Xamarin

I recently started writing on Xamarin, and I've got some problems with writing code to look at the devices attached to my host.

I've read the SDK documentations, and all the docs are written in java. I'm trying to change java into C#, and I coded this to find the devices attached:

UsbManager manager = (UsbManager)GetSystemService (Context.UsbService);
display.Text += " /n inizializzo il manager";
display.Text += "numero dispositivi: ";
display.Text += manager.DeviceList.Count;

With "usb manager" app I can see all the usb attached, there are 3. But with this code it appears to be 0 usb attached.

I don't understand why.

like image 497
Marco Avatar asked Nov 04 '13 20:11

Marco


People also ask

What devices use USB Type-C?

USB-C, explained This means that it can be a standard connector for both computers and phones, as well as other devices like game consoles. Some popular devices that use USB-C cables are the Nintendo Switch, MacBook Pro, and Samsung Galaxy line of phones.

How do I change USB settings on Android?

To change USB preferencesConnect a USB cable to your device. Drag down the status bar, and then tap Android System next to (USB icon). Tap Tap for more options, and then select an option.

What is USB Type-C in mobile phones?

USB Type-C is a connector for delivering data and power to and from the phone. Most of the USB ports are built on a second-generation USB 3.1 standard that can deliver data at speeds of up to 10Gbps. In USB Type-C, both ends of the USB cable are the same, thereby enabling the reverse plug orientation.


1 Answers

Well it looks like you've already solved this one, I may have some additional tips for you though. First of all like you've found out USB-host is only supported in Android 3.1 and higher versions. Second of all, you need to add the "uses-feature " to the android manifest.

<uses-feature android:name="android.hardware.usb.host" android:required="false" />

Be sure to include required="false" to make sure that your app can be installed and started on device that do not support USB-host (if usb-host is optional for your app).

Third you'd be surprised how many modern mobile devices do not support host mode yet and fourth, the USB-port needs to supply enough power for the attached usb-device. Fifth and last point: Android is not really suited for high-speed "real-time" usb-communications. Android has a nasty habit of pausing the whole system and collecting garbage before "unblocking" and moving on. If you're trying to send a lot of data via usb at a constant rate this can cause the buffer to fill up when Android pauses and GC's, and at the next read to the buffer you may find corrupted data. So be sure to watch your memory management when building an app that includes usb-communications! I've found that configuring the Xamarin GC bridge to "Tarjan" mode helps quite a bit in reducing GC-induced pauses, this might be specific to my app though.

Maybe not a great fit as an answer to your question, but I was feeling chatty ;). Good luck!

  • Edit: Did not notice that OP didn't specify whether he wants to use a usb accessory or a usb-host device. For an accessory, use Mike Stonis' tip!
like image 179
PeeGee85 Avatar answered Sep 21 '22 08:09

PeeGee85