Android USB host read from device Android USB host read from device android android

Android USB host read from device


You can use UsbSerial Lib of from https://github.com/mik3y/usb-serial-for-android

My example code:

    UsbManager usbManager = null;    UsbDeviceConnection connection = null;    UsbSerialDriver driver = null;    UsbSerialPort port = null;    usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);    List<UsbSerialDriver> availableDrivers =  UsbSerialProber.getDefaultProber().findAllDrivers(manager);    // Open a connection to the first available driver.    for (UsbSerialDriver usd : availableDrivers) {        UsbDevice udv = usd.getDevice();         if (udv.getVendorId()==0x067B || udv.getProductId()==2303){            driver = usd;            break;        }    }        connection = usbManager.openDevice(driver.getDevice());        port = driver.getPorts().get(0);        driver.getDevice().    }        if (connection == null) return;        try{            port.open(connection);            port.setParameters(4800, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);                try{                    byte buffer[] = new byte[250];                    //Log.d("GPS_REQ", "->");                    int numBytesRead = port.read(buffer, 500); //5000;                 }catch (Exception e) {                    Log.d("GPS_ERR", e.getLocalizedMessage());                }