Reputation: 14489
Is there a way to programatically identify the currently USB working mode?
I mean, some function that would return either if the device is at Host
, Device
or Accessory
mode.
Upvotes: 6
Views: 180
Reputation: 1965
Not the best answer, but once you have a UsbManager you might be able to figure it out. Usually this UsbManager is created using a Context I believe, but it looks like you are switching modes so you hopefully can get a UsbManager instance, m
in this case:
UsbManager m = createManagerSomehow
For accessory mode, this only has one callback. If this returns one then you know it is Accessory.
m.getAccessoryList()
So I'm thinking somehting like this might work:
if(m.getAccessoryList().size() > 0)
accessoryMode = true;
And for Host, if you have a UsbDevice device
, or String deviceName
you could use the same UsbManager m
function to see if it contains that device.
if(m.getDeviceList().containsValue(device))
hostMode = true;
or
if(m.getDeviceList().containsKey(deviceName))
hostMode = true;
and I don't know about what Device
is, but if none of the above are true then you know it is just a Device. You don't really need this boolean variable below, because you have the other two. It's just here to help with my explanation via state logic.
if(!hostMode && !accessoryMode)
deviceMode = true
Hope this helps. Check out UsbManager for more documentation and just search the page for Host and Accessory.
http://developer.android.com/reference/android/hardware/usb/UsbManager.html
NOTE: I am a little confused when you say you switch the modes in your comment. It conflicts with the Accessory call I make above and might not work, but what I would do then (if you don't need deviceMode
) then just check if the usb device is a HOST mode and if not you know it is in application mode...
Upvotes: 1