mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.82k stars 1.58k forks source link

onRunError : java.lang.NullPointerException #450

Closed si5haoa closed 2 years ago

si5haoa commented 2 years ago

crash error:onRunError : java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.hardware.usb.UsbRequest.queue(java.nio.ByteBuffer, int)' on a null object reference

onRunError : java.io.IOException: Queueing USB request failed

I use :public void openDevice() { try{ lastOpenTime = System.currentTimeMillis(); // refresh(); UsbManager usbManager = (UsbManager) MyApplication.getInstance().getSystemService(Context.USB_SERVICE); List availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager); if (availableDrivers.isEmpty()) { return; }

    UsbSerialDriver driver = availableDrivers.get(0);
    usbSerialPort = driver.getPorts().get(0);
    if(usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
            usbPermission = UsbPermission.Requested;
            PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(MyApplication.getInstance(), 0,
                    new Intent(INTENT_ACTION_GRANT_USB), 0);
            usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
            return;
        }

        UsbDeviceConnection usbConnection = usbManager.openDevice(driver.getDevice());
    if(usbConnection == null && usbPermission == UsbPermission.Unknown && !usbManager.hasPermission(driver.getDevice())) {
        usbPermission = UsbPermission.Requested;
        PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(MyApplication.getInstance(), 0,
                new Intent(INTENT_ACTION_GRANT_USB), 0);
        usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
        return;
    }

    if(usbConnection == null) {
        if (!usbManager.hasPermission(driver.getDevice()))
            ToastUtil.makeWarning(MyApplication.getInstance(),"connection failed: permission denied");
        else
            ToastUtil.makeWarning(MyApplication.getInstance(),"connection failed: open failed");
        return;
    }

        usbSerialPort.open(usbConnection);
        usbSerialPort.setParameters(BAUD_RATE, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1,
                UsbSerialPort.PARITY_NONE);
        usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
        usbIoManager.start();
        setConnectCallBacks(true);
    } catch (Exception e) {
        e.printStackTrace();
        disconnect();
    }
}

It can be used normally. When I swipe on the touch screen with my finger, the software does not handle touch screen events. Then it gives an error。I use usbSerialPort.write(data, 0); to read the value.I have a problem with my current Android device. I keep sliding my finger on the screen and the screen will not respond to touch events. Does this have something to do with it?