How to define this variable

0

I am using the code of this SerialPortExample and I want to define a variable (mConfigDataBit) so that I can modify it later but I can not do it correctly .

The possible values that this variable mConfigDataBit can take is: DATA_BITS_8, DATA_BITS_7, DATA_BITS_6, DATA_BITS_5

public static int mConfigDataBit = DATA_BITS_8;

private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        if (arg1.getAction().equals(ACTION_USB_ATTACHED)) {
            boolean ret = builder.openSerialPorts(context, mConfigBaudRate,
                    UsbSerialInterface.mConfigDataBit,
                    UsbSerialInterface.STOP_BITS_1,
                    UsbSerialInterface.PARITY_NONE,
                    UsbSerialInterface.FLOW_CONTROL_OFF);
            if(!ret)
                Toast.makeText(context, "Couldnt open the device", Toast.LENGTH_SHORT).show();
        } else if (arg1.getAction().equals(ACTION_USB_DETACHED)) {

            UsbDevice usbDevice = arg1.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            boolean ret = builder.disconnectDevice(usbDevice);

            if(ret)
                Toast.makeText(context, "Usb device disconnected", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(context, "Usb device wasnt a serial port", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(ACTION_USB_DISCONNECTED);
            arg0.sendBroadcast(intent);

        }
    }
};

The error I get is: "Can not resolve the Symbol 'mConfigDataBit'"

Could someone tell me what my mistake is?

    
asked by W1ll 20.12.2018 в 00:40
source

1 answer

1

Actually DATA_BITS_8 is a constant but it is inside the class UsbSerialInterface therefore you must refer to this class to get its value:

UsbSerialInterface.DATA_BITS_8

therefore it would be:

public static int mConfigDataBit = UsbSerialInterface.DATA_BITS_8;

and you should only use this variable:

  boolean ret = builder.openSerialPorts(context, mConfigBaudRate,
                    mConfigDataBit,
                    UsbSerialInterface.STOP_BITS_1,
                    UsbSerialInterface.PARITY_NONE,
                    UsbSerialInterface.FLOW_CONTROL_OFF);

Since UsbSerialInterface.mConfigDataBit is not really in class UsbSerialInterface .

But actually I recommend you take the value directly from class UsbSerialInterface :

  boolean ret = builder.openSerialPorts(context, mConfigBaudRate,
                    //mConfigDataBit,
                    UsbSerialInterface.DATA_BITS_8, /* Aquí el cambio */
                    UsbSerialInterface.STOP_BITS_1,
                    UsbSerialInterface.PARITY_NONE,
                    UsbSerialInterface.FLOW_CONTROL_OFF);
    
answered by 20.12.2018 / 01:19
source