javaandroidusbwinusb

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


I would like to know if there is some kind of an API in android studio that enables communication between a device and the android phone through a USB. For example, external camera.

I have used SetupAPI and WINUSB before to accomplish such a task. So something similar to those two would be appreciated.

The company that created the device does not provide an SDK, driver or any extra information.

Thank you very much.


Solution

  • 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!