USB广播接收器被多次调用

时间:2018-11-01 12:08:15

标签: android arduino usb

我正在创建一个通过USB连接到Arduino的应用程序。连接工作正常,我可以正确发送和接收数据。我的问题是连接和断开USB时。

由于某种原因,每次我通过USB连接或断开Arduino时,BroadcastReceives被调用2至4次,有时Arduino甚至根本不连接。

我的BroadcastReceiver实现:

public static class ArduinoConnectionReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                switch(intent.getAction()) {
                    case ArduinoConfigurations.ACTION_USB_PERMISSION:
                        if (intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED)) {
                            connection = usbManager.openDevice(device);
                            serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection);
                            if (serialPort != null) {
                                if (serialPort.open()) { //Set Serial Connection Parameters.
                                    serialPort.setBaudRate(115200);
                                    serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8);
                                    serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1);
                                    serialPort.setParity(UsbSerialInterface.PARITY_NONE);
                                    serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
                                    serialPort.read(new ArduinoCallback());
                                    Log.d("ARDUINO", "Arduino connected!");
                                    ((MainActivity)context).enableButtons(true);

                                } else
                                    Log.e("ARDUINO", "Port not open, not possible to connect.");
                            } else
                                Log.e("ARDUINO", "Port is null, not possible to connect.");
                        } else
                            Log.e("ARDUINO", "Permission not granted, not possible to connect.");
                        break;
                    case UsbManager.ACTION_USB_DEVICE_ATTACHED:
                        openConnection((MainActivity)context);
                        break;
                    case UsbManager.ACTION_USB_DEVICE_DETACHED:
                    default:
                        closeConnection((MainActivity)context);
                        break;
                }
                ...

有时它还会抛出:

exception in UsbManager.openDevice
    java.lang.IllegalArgumentException: device /dev/bus/usb/003/012 does not exist or is restricted
        at android.os.Parcel.readException(Parcel.java:1688)
        at android.os.Parcel.readException(Parcel.java:1637)
        at android.hardware.usb.IUsbManager$Stub$Proxy.openDevice(IUsbManager.java:411)
        at android.hardware.usb.UsbManager.openDevice(UsbManager.java:330)
...

Error on connecting Arduino: Attempt to invoke virtual method 'boolean android.hardware.usb.UsbDeviceConnection.claimInterface(android.hardware.usb.UsbInterface, boolean)' on a null object reference

我读到USB连接器有时可能真的不稳定。只是吗?我可以解决这个问题,以使Arduino仅连接和断开一次吗?

0 个答案:

没有答案