Android Apps, Communicating with a Device Plugged in the Usb Port

Android apps, communicating with a device via USB port

USB support is limited in Android currently and only available on > 3.0. Look at the online developer's docs.

USB Docs

Communicating with the USB device plugged into Android Phone (for instance, an external camera)

It depends on what you want to do BUT short answer is yes.
To detect an external camera you may try this:

public String getExternalCamera(){

CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

String exCamId = null;

for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);

//LENS_FACING_EXTERNAL will return Value: 2
if (facing != null && facing.equals(CameraCharacteristics.LENS_FACING_EXTERNAL)) {
exCamId = cameraId;
}
}

return exCamId;

}

LENS_FACING_EXTERNAL

added in API level 23

public static final int LENS_FACING_EXTERNAL
The camera device is an external camera and has no fixed facing relative to the device's screen.

You can also use:

INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL

added in API level 28

public static final int INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL

This camera device is backed by an external camera connected to this Android device.

The device has capability identical to a LIMITED level device, with some exceptions.

For more info see Android documentation here!

Android: USB Communication Android - External Device

I'm basically doing the same you are describing. I'm still at the early stages but I've managed such communication under some constraints:

  1. You need a device supporting the USB Host API. The API itself has been around since Android 3.2 or so, but not every device seems to support it. It looks like most of the devices running >4.0 should work, but still is a per device check you should do. I'm currently using an Acer Iconia Tab A510, which so far seems to work but has some glitches: for instance, you cannot charge the device while using the USB port as it is shared with the charger but not compatible with USB.
  2. As Neil says the USB API is low level, so I think you will need a driver for your USB chipset (the one at your side, i.e. in your device). I've been quite lucky here as there's an open source project working on FTDI chipsets that happen to be the ones I've chosen. If that's your case too, you should check the projects:
    • FTDriver: https://github.com/ksksue/FTDriver
    • Android USB Serial Monitor Lite: https://github.com/ksksue/Android-USB-Serial-Monitor-Lite This one has even an app in Google Play, so you can install it and start "playing" ;-)

I hope this helps. As I've said, I'm starting to work in this ecosystem and I still have to find my way around many "places".

Cheers,
Asier.

Android: Communicating with a USB device which acts as host

This is already technically possible
because it can connect to the PC in a
similar configuration.

Only for things baked into the firmware. Your SDK application cannot invent new Linux device drivers, nor does it have any access to the Android device side of the adb connection.

Can we do the same via an application?

Since you have not said what you are trying to do, this is impossible to answer in a definitive fashion.

Say, for example, the "device which can act as an USB host or slave and processes the data it receives" wants screenshots off of the Android device. That is eminently possible via adb, because adb has a protocol for that built in. All you would need to do is have your device connect to the Android device via the adb protocol and request screenshots, no different than does DDMS or hierarchyviewer.

So, I would look at the problem from a different perspective: if you can accomplish it via DDMS, you can do it via your custom device. If you can accomplish it via adb shell commands, you can do it via your custom device. If you cannot accomplish your goals via existing interfaces, though, since you have no way to invent new ones over USB, you will be stuck.

Conceivably, you could plug your custom device into a Bluetooth or WiFi dongle, then use those on Android for communication.



Related Topics



Leave a reply



Submit