Error read/write USB host Android

452 views Asked by At

I try for a week to run the usb host on android. i try with this two solution :

I try so many possibilities but the application return a error when i try to write or it's "lock" when i try to read...

My last try :

 UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    ProbeTable customTable = new ProbeTable();
    customTable.addProduct(0x15A2, 0x300, CdcAcmSerialDriver.class);    //0x15A2  0x300

    UsbSerialProber prober = new UsbSerialProber(customTable);
    List<UsbSerialDriver> availableDrivers = prober.findAllDrivers(manager);

    if (availableDrivers.isEmpty()) {
        Toast.makeText(MainActivity.this,"No device detected", Toast.LENGTH_SHORT).show();
        return;
    }

    // Open a connection to the first available driver.
    UsbSerialDriver driver = availableDrivers.get(0);
    UsbDeviceConnection connection = manager.openDevice(driver.getDevice());
    if (connection == null) {
        return;
    }

    // Read some data! Most have just one port (port 0).
    UsbSerialPort port = driver.getPorts().get(0);
    try {

        port.open(connection);
        port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

        final TextView textViewToChange = (TextView) findViewById(R.id.textView);
        textViewToChange.setText("port open and parameters up");

        byte buffer[] = new byte[32];

        byte bufferwrite[] = new byte[1];
        buffer[0] = 1;

        int numBytesRead = port.write(bufferwrite, 1000);

        final TextView textViewToChange2 = (TextView) findViewById(R.id.textView2);
        textViewToChange2.setText("port read/write : "+numBytesRead);

    } catch (IOException e) {

        final TextView textViewToChange2 = (TextView) findViewById(R.id.textView2);
        textViewToChange2.setText("error : "+e);

    } finally {
        try {
            port.close();

            final TextView textViewToChange4 = (TextView) findViewById(R.id.textView4);
            textViewToChange4.setText("Port close");

        } catch (IOException e) {
            e.printStackTrace();
            final TextView textViewToChange4 = (TextView) findViewById(R.id.textView4);
            textViewToChange4.setText("Error close port : "+e);
        }
    }

This line return all time a error :

int numBytesRead = port.write(bufferwrite, 1000);

And i can't solve it. Thanks in advance for your help.

0

There are 0 answers